Need help retrieving a result set from a SQLite table using latitude and longitude variables as criteria

Dear fellow iPhone and Objective-C developers:

I need to build a SQLite table that will consist of the following columns:

  1. Name
  2. Address
  3. City
  4. ProvinceState
  5. PostalZipCode
  6. Country
  7. PhoneNumber
  8. Latitude
  9. Longitude

I need to send a request to this table, where I am passing latitude, and longitude variables that have been calculated from the user's position using CLLocation, and retrieve rows that are closest to the one's I am passing in. However, my problem is:

  1. What column type should Latitude, and Longitude be, so that I can pull the relevant rows? Should the columns be Strings, or something else? What type should my variables that I am passing in should be? Should they be Strings, doubles, or something else? At present, I have them as Strings in my code, but I'm not sure if this is the best approach. This is important because in the case of the longitudinal coordinates, the values will be negative. How would I take this into account when I make my sql call?

  2. At present, the table is quite small. However, this table will definitely become quite large in the future, and I don't want to retrieve ALL values from the table, but rather, filter out the irrelevant values, and only retrieve those that are necessary. The way I figure I would do this would be to retrieve those rows whose latitude/longitudinal pairs were numerically closest to the ones I am passing in. How would I construct a sql query that would accommodate this in Objective-C?

I hope my question makes sense. Thanks in advance to all who reply.


Besides storing latitude and longitude as double, you'd definitely want to index those fields. However you should be aware of the fact that index operations are most efficient in case of integer fields - and this is an important consideration when it comes to large data volumes.

I guess the following algorithm might help you:

  • introduce additional columns:
    • LatitudeIdx int = round(Latitude * ScaleFactor)
    • LongitudeIdx int = round(Longitude * ScaleFactor)
  • define value for ScaleFactor which will suite your needs
  • define an index (LatitudeIdx, LongitudeIdx)
  • query you data using LatiduteIdx and LongitudeIdx values (this will be an fast index lookup operation)

What you achieve with *Idx columns and ScaleFactor is actually breaking your data into cells of equal size. Size of the cell is actually 1/ScaleFactor geo degrees, i.e. roughly 111 km/ScaleFactor.

So when you retrieve the data with particular LatitudeIdx and LongitudeIdx values you're actually getting all points that fall into the cell defined by those values, i.e. filter your data points. After that you calculate distances to the point of interest and select closest one (you can actually do that directly in query and sort by distance to get the point you need in a single pass).

Need Your Help

add body class to wordpress based on user role

php wordpress

can't find any plugin to help out for my needs

Optimizing a nested for-loop which uses the indices of an array for function

python arrays numpy lambda vectorization

Let's imagine an empty NumPy array of 3x4 where you've got the coordinate of the top-left corner and the step size in horizontal and vertical direction.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.