sqlite - iOS 5 中 SQLite 的距离函数非常慢

标签 sqlite ios5 distance latitude-longitude

我一直在使用一个 C 函数来计算 SQLLite 中坐标之间的距离。

问题是,对于相同的数据库和不同的 iOS 设备(4 和 5),在运行 iOS 5 时执行相同查询的时间要长 5 倍。

有人知道为什么吗?

这是距离的代码:

static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
// check that we have four arguments (lat1, lon1, lat2, lon2)
assert(argc == 4);

// check that all four arguments are non-null
if (sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL || sqlite3_value_type(argv[2]) == SQLITE_NULL || sqlite3_value_type(argv[3]) == SQLITE_NULL) {
    sqlite3_result_null(context);
    return;
}

// get the four argument values
double lat1 = sqlite3_value_double(argv[0]);
double lon1 = sqlite3_value_double(argv[1]);
double lat2 = sqlite3_value_double(argv[2]);
double lon2 = sqlite3_value_double(argv[3]);

// convert lat1 and lat2 into radians now, to avoid doing it twice below
double lat1rad = DEG2RAD(lat1);
double lat2rad = DEG2RAD(lat2);

// apply the spherical law of cosines to our latitudes and longitudes, and set the result appropriately
// 6378.1 is the approximate radius of the earth in kilometres
sqlite3_result_double(context, (acos(sin(lat1rad) * sin(lat2rad) + cos(lat1rad) * cos(lat2rad) * cos(DEG2RAD(lon2) - DEG2RAD(lon1))) * 6378.1) * 1000); }

这是我如何使用它的示例:
Select distance(Latitude, Longitude,-22.89095,-47.05134) from TABLE

请,任何帮助将不胜感激

谢谢

最佳答案

好的。
我发现这是 iOS 5.0 上的问题
即使在最终版本中,缓慢的查询仍在发生。

我不得不删除“order by distance(Latitude, Longitude,-22.89095,-47.05134) ”,查询再次变得更快。

然后我使用代码进行了排序...
不是最好的办法。但...

关于sqlite - iOS 5 中 SQLite 的距离函数非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7236955/

相关文章:

algorithm - 如何找出两个点中哪一个最接近一组点?

c# - 记录整个 SOAP 请求还是只记录其中的参数?

SQLite - 根据列值重复行

go - 你能在 Golang 中使用 SQLite ATTACH 和 DETACH 吗?

ios - 通过编程方式制作的 UINavigationController 不显示导航栏

iphone - 将 iPad 应用程序转换为 iPhone 5

python - 如何使用python中的return方法计算两点之间的距离?

database - 如何保存 SQLite3 工作

iphone - 单击 iphone 中的选项卡栏项目时如何在不同类之间移动

java - 为什么下面的代码没有绘制一条最靠近左上角的线?