Distance between Long Lat coord using SQLITE
- by munchine
I've got an sqlite db with long and lat of shops and I want to find out the closest 5 shops.
So the following code works fine.
if(sqlite3_prepare_v2(db, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
while (sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSString *branchStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSNumber *fLat = [NSNumber numberWithFloat:(float)sqlite3_column_double(compiledStatement, 1)];
NSNumber *fLong = [NSNumber numberWithFloat:(float)sqlite3_column_double(compiledStatement, 2)];
NSLog(@"Address %@, Lat = %@, Long = %@", branchStr, fLat, fLong);
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:[fLat floatValue] longitude:[fLong floatValue]];
NSLog(@"Distance i meters: %f", [location1 getDistanceFrom:location2]);
[location1 release];
[location2 release];
}
}
I know the distance from where I am to each shop. My question is.
Is it better to put the distance back into the sqlite row, I have the row when I step thru the database. How do I do that? Do I use the UPDATE statement? Does someone have a piece of code to help me.
I can read the sqlite into an array and then sort the array. Do you recommend this over the above approach? Is this more efficient?
Finally, if someone has a better way to get the closest 5 shops, love to hear it.