使用 SQLITE 的长纬度坐标之间的距离

发布于 2024-09-02 22:29:55 字数 1317 浏览 6 评论 0原文

我有一个带有长和纬度商店的 sqlite 数据库,我想找出最近的 5 家商店。

所以下面的代码工作正常。

    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];
    }       
}

我知道我所在的位置到每家商店的距离。我的问题是。

  1. 将距离放回到sqlite行中是否更好,当我单步遍历数据库时我有该行。我该怎么做?我是否使用 UPDATE 语句?有人有一段代码可以帮助我吗。

  2. 我可以将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.

  1. 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.

  2. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2024-09-09 22:29:55

在 SQL 中查找附近位置的最快方法是在 SQL 查询中使用半正矢公式。在 Google 上搜索 sqlite 和 Haversine,您会找到一个实现。

这是我以前使用过的一个:

http://www.thismuchiknow.co.uk/?p =71

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:

http://www.thismuchiknow.co.uk/?p=71

黑寡妇 2024-09-09 22:29:55

您只需运行每个元素一次,因此复杂性相当不错。
你需要一种固定大小的双端队列容器,一种链表或固定大小的数组,大小固定为5,即你想要从每个点获取的商店数量。始终添加距 Deque 最短距离的商店。运行完你的数据库后,你的双端队列中有最近的 5 个商店。

you have to run through every element only once so the complexity is fairly good.
you need a kind of fixed size Deque-container, a kind of linked list or a fixed size array, fixed by the size of 5, the number of shops you want to get from every point. add always the shop with the most minimal distance to the Deque. after running through your db you are have the nearest 5 shops in your deque.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文