java Android 上的 Sqlite:如何创建 sqlite dist db 函数 - 在应用程序中使用经纬度计算距离
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2352320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sqlite on Android: How to create a sqlite dist db function - to be used in the app for distance calculation using lat, long
提问by Puneet
We are building an Android App that will use user's current location (lat, long) and show top 50 venues around the current location, sorted by distance.
我们正在构建一个 Android 应用程序,它将使用用户的当前位置(纬度、经度)并显示当前位置周围的前 50 个场地,按距离排序。
We have these venues stored in an SQLite DB. We plan to ship with the sqlite DB with the app.
我们将这些场地存储在 SQLite 数据库中。我们计划随应用程序一起提供 sqlite DB。
In order to fetch only the relevant top 50 closest venues, we want to define a db function DIST (to calculate distance between two points) and use it in our query.
为了仅获取相关的前 50 个最近的场地,我们想定义一个 db 函数 DIST(计算两点之间的距离)并在我们的查询中使用它。
How can I define a custom SQLite function for Android Apps? What will be the Java API call to do this?
如何为 Android 应用程序定义自定义 SQLite 函数?执行此操作的 Java API 调用是什么?
We have successfully implemented this approach in our iPhone App - using Objective C.
我们已经在我们的 iPhone 应用程序中成功实现了这种方法 - 使用目标 C。
回答by Joshua Smith
Update: The answer is that you can not do this.
更新:答案是你不能这样做。
from: Android. Is it possible to write custom function on C/C++ and use it in SQL query?
回答by Graham Borland
A workaround might be to fetch all records where one of the values (say, longitude) is close to the current location. If you use this column as an index then fetching these will be very fast.
一种解决方法可能是获取其中一个值(例如经度)接近当前位置的所有记录。如果您将此列用作索引,则获取这些将非常快。
Then, you have a much reduced subset to iterate over comparing the latitudes and doing your full distance calculations.
然后,您有一个大大减少的子集来迭代比较纬度并进行全距离计算。
回答by Chrispix
I know this is an old question, but there is a little bit easier solution I think.
我知道这是一个老问题,但我认为有一个更简单的解决方案。
This is an example, but it fails when crossing 0 degrees, although you could shift all the latitude/longitudes to adjust.
这是一个示例,但在跨越 0 度时失败,尽管您可以移动所有纬度/经度进行调整。
The overall solution works pretty well for ordering, but just uses the Pythagorean theorem to calculate the relative distance (note: no square root function in sqlite, but that is not relevant).
整体解决方案对于排序非常有效,但仅使用勾股定理来计算相对距离(注意:sqlite 中没有平方根函数,但这无关紧要)。
String calcDistance = "( ( (abs(" + cLat + ") - " + Math.abs(lat) + ") * (abs(" + cLat + ") - " + Math.abs(lat) + ") )" +
"+ ( (abs(" + cLng + ") - " + Math.abs(lng) + ") * (abs(" + cLng + ") - " + Math.abs(lng) + ") ) ) as calc_distance";
If you needed to fix this distance, you would want to adjust by adding 90 to all latitudes and 180 to all longitude. Then you could get rid of that nasty abs functions.
如果您需要修复此距离,您需要通过向所有纬度添加 90 和向所有经度添加 180 来进行调整。然后你可以摆脱那些讨厌的 abs 功能。
回答by andy.xyz
It can be done but you cannot load this into the database from the Java API. You will need to provide your own implementation of the sqlite library that is extended to provide the functions you wish at the C level. This is not fun but it works well. Be aware that you may have issues with Android devices using non-standard architectures.
它可以完成,但您不能将其从 Java API 加载到数据库中。您将需要提供您自己的 sqlite 库实现,该库被扩展以在 C 级别提供您希望的功能。这并不有趣,但效果很好。请注意,您可能会遇到使用非标准架构的 Android 设备的问题。

