是否可以在 Objective-C 中实现半正矢公式并从 SQLite 调用它?
据我了解,SQLite 没有数学函数来正确实现直接 SQL 中的 Haversine 公式。我认为这应该可以使用 外部函数 实现,并用 C 语言实现目标
是在 iPhone 中拥有一个 SQLite 数据库,并能够根据到用户当前位置的距离进行排序。我已经搜索过,但找不到任何已完成此操作的示例。我认为困难的部分是使函数声明正确。我希望的最终结果是能够执行如下 SQL 语句:
SELECT * FROM LOCATION loc ORDER BY distance(loc.lat, loc.long, ?, ?)
我有一个 C 半正弦公式。函数定义如下:
float distance( float nLat1, float nLon1, float nLat2, float nLon2 );
有谁知道这是否可能和/或有一些示例代码可以开始?
As I understand, SQLite doesn't have the math functions to properly implement the Haversine formula in straight SQL. I'm thinking this should be possible using an external function, with the implementation being in C.
The goal is to have a SQLite database in an iPhone, and to be able to sort by the distance to the user's current location. I've searched, but I can't find an example of any examples of this being done. I think the difficult parts would be getting the function declarations correct. The end result I'm hoping for, is to be able to execute a SQL statement like:
SELECT * FROM LOCATION loc ORDER BY distance(loc.lat, loc.long, ?, ?)
I have a C Haversine formula. The function definition is as follows:
float distance( float nLat1, float nLon1, float nLat2, float nLon2 );
Does anyone know if this is possible and/or have some example code to start from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对这篇文章很幸运: http://www.thismuchiknow.co.uk/ ?p=71
I just had good luck with this post: http://www.thismuchiknow.co.uk/?p=71
这演示了一个 sqlite 函数,它接受一个字符串参数并返回一个字符串结果。
在您的情况下,您需要一个读取四个浮点数并返回一个浮点数的函数,但原理是相同的(您可以将 sqlite3_value_text 替换为 sqlite3_value_double 和 sqlite3_result_text em> 与 sqlite3_result_double):
This demonstrates a sqlite function that takes in one string parameter and returns a string result.
In your case you would need a function that reads four floats and returns a float but the principle is the same (you would replace sqlite3_value_text with sqlite3_value_double and sqlite3_result_text with sqlite3_result_double):