问题描述
我有一个带有长商店的sqlite数据库,我想找出最近的5家商店。
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.
-
将距离放回sqlite行是否更好,当我逐步通过时,我有一行数据库。我怎么做?我是否使用UPDATE语句?有人有一段代码可以帮助我。
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.
我可以将sqlite读入一个数组,然后对数组进行排序。你是否推荐上述方法?这样效率更高吗?
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?
最后,如果有人有更好的方法来获得最近的5家商店,那就爱听它。
Finally, if someone has a better way to get the closest 5 shops, love to hear it.
推荐答案
在SQL中查找附近位置的最快方法是在SQL查询中使用Haversine公式。谷歌搜索sqlite和Haversine,你会发现一个实现。
The fastest way to find nearby locations in SQL is to use the Haversine formula in an SQL query. Do a Google search for sqlite and Haversine and you'll find an implementation.
这是我以前用过的一个:
Here's one I've used before:
这篇关于使用SQLITE的Long Lat coord之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!