开发者

IPhone - SQLite distance by Latitude and Longitude

开发者 https://www.devze.com 2023-03-23 03:02 出处:网络
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).

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...).开发者_JS百科 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!


Do it in Objective C.

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消