iPhone geo radius search; Use Core Data or SQLite?
Asked Answered
H

2

5

I am writing an application which has ca. 7000 european restaurants in the database and I want to show a list of the nearest ones to the user, for example all of them which are in a radius of 5km to the user.

I could do the search on the server but it then requires internet. Is it possible to save the data on the iPhone and query the database? I can't find any references for something like that in core data or iPhones SQLite.

Do I really need to program it myself with Pythagoras and stuff and calculate the distance to every restaurant on every query? Or is there some other way?

[update] I'd like to use it like I do already on the server (but to do it on the iPhone): SELECT * FROM restaurants WHERE ST_Distance_Sphere(geo, ST_GeomFromText(POINT(55.98767 57.12345), -1)) < 5000

I want the user to be able to find a restaurant even if she has no internet connection, for example when you're in a foreign country.

Heedful answered 14/7, 2011 at 9:24 Comment(4)
You may consider - if possible and depending on the quality of your dataset - computing the distances using the latitude/longitude if you have this information, to the point where your user is.Chaing
Yes, that is what I was thinking about. But I don't want to get all the 7k rows and compare every each and one with help of a Pythagoras computation, I want to use something like SELECT * FROM restaurants WHERE ST_Distance_Sphere(geo, ST_GeomFromText(POINT(55.98767 57.12345), -1)) < 5000Heedful
One method that I have achieved this was with a dataset of around 5,000 entries except that these were in binary format and therefore did not use Core Data / SQL. The processing time on device to compute the distance between a lat,long and all entries was less than 2 seconds - it can be done - but sadly I cannot advise on the SQL side as I have not worked with this within iOS. You could also split locations into continents or blocks of lat,long to assist with the computing side of this task.Chaing
Ah ok thanks for your comments about the time on your dataset, it is very valuable.Heedful
L
9

Read up on latitude and longitude calculations. Latitude an longitude are themselves distances expressed as arcs upon a spherical surface. If you have a database of locations expressed by latitude and longitude, then you can perform a fetch to find only those records that fall within a few degrees latitude north and south and a few degrees longitude east and west of the users current location.

So you would end up with a predicate that would be something like (psuedo-code):

latitude >= (currentLatitude-aFewMinutesOfArc)
AND
latitude <= (currentLatitude+aFewMinutesOfArc)
AND
longitude >= (currentLongitude-aFewMinutesOfArc)
AND
longitude >= (currentLongitude+aFewMinutesOfArc)

... this would create a logical box which would return all restaurant records that fell within the box. Then if you needed to calculate the exact distances you would only have to perform calculation on a handful of records out of the 7,000 you have.

I would recommend reading up on Location Services because it provides a lot of tools for handling location calculations.

As to whether to use plain SQL or Core Data, check out this previous answer on the subject. In brief, if you already know the C SQL api and your data is simple, large and static, then SQL is a good choice. If you can take time to learn Core Data and your data is complex and dynamic, then Core Data is the better choice regardless of the size of the data.

Lulalulea answered 15/7, 2011 at 18:45 Comment(0)
T
1

You could use GeoFire for iOS. It uses Firebase to store the data, but since Firebase maintains a local cache, it will still work without a network connection. It will handle both the GeoQueries as well as notifying you in realtime as items enter and leave your query.

[disclaimer: I work at Firebase]

Torrefy answered 26/8, 2014 at 22:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.