我有一个sqlite数据库与长和lat的商店,我想找出最近的5个商店。使用SQLITE的Long Lat coord之间的距离
所以下面的代码工作正常。
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];
}
}
我知道我从哪里到每个商店的距离。我的问题是。
是更好地把距离放回源码行,我也行,当我踏上直通数据库。我怎么做?我使用UPDATE语句吗?有人有一段代码可以帮助我吗?
我可以将sqlite读入数组,然后对数组进行排序。你是否推荐使用上述方法?这是否更有效率?
最后,如果有人有更好的方法获得最近的5家店铺,喜欢听它。
nevan,我喜欢你的答案。我已经成功地使用数组实现了这个解决方案,但是我将来肯定会使用你的建议。 – munchine 2010-06-02 02:44:08
AAA答案! 我已经使用以下查询来检索距离结果以及: “SELECT Hotel。*,distance(Hotel.Latitude,Hotel.Longitude,123.45,123.45)AS距离酒店ORDER BY距离” – 2011-12-14 05:30:25