IPhone - SQLite distance by Latitude and Longitude

五迷三道 提交于 2019-12-21 04:42:22

问题


I have a embedded database in my app that has all Latitude and Longitude of some interested points and I need to know my distance from these points(get my position using GPS). My problem is: I've just figured out that SQLite can't calculate distance using the query that I have because it doesn't calculate trigonometry functions(SIN, COS...). I was trying to avoid calculating these distances programatically using this query:

NSLog(@"SELECT ((ACOS(SIN(%@ * PI() / 180) * SIN(lat * PI() / 180) + COS(%@ * PI() / 180) * COS(lat * PI() / 180) * COS((%@ – lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance` FROM `members` HAVING `distance`<=’1000′ ORDER BY `distance`", Latitude,Latitude, Longitude);

Does anybody has a solution?

Regards!


回答1:


Do it in Objective C.

this guy had the same problem: http://www.thismuchiknow.co.uk/?p=71




回答2:


Yeah, you can make a simple query based on a rectangle around the location, then use the haversine formula to compute the exact distance.

The advantage of that is that you get to use indexes on the SQLite table.

More about the haversine formula: http://en.wikipedia.org/wiki/Haversine_formula



来源:https://stackoverflow.com/questions/6828553/iphone-sqlite-distance-by-latitude-and-longitude

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!