IPhone - SQLite Distance Latitude and Longitude

I have a built-in database in my application that has all the latitude and longitude of some interesting points, and I need to know my distance from these points (get my position using GPS). My problem: I just realized that SQLite cannot calculate the distance using the query that I have, because it does not calculate trigonometry functions (SIN, COS ...). I tried to avoid calculating these distances programmatically with 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 anyone have a solution?

Hello!

+6
source share
2 answers

Do it in Objective C.

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

+9
source

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

The advantage is that you can use indexes in a SQLite table.

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

0
source

Source: https://habr.com/ru/post/893580/


All Articles