tags:

views:

640

answers:

3

Is there an easy way to display a sorted dropdown list on the iPhone by "closest to current location"?

I.e. if I have a list of cities with their GPS locations, can I quickly sort a dropdown to show you city selections in order of closest to you?

Will this be fast? Or will I have to presort (before you select the dropdown) in the background?

+1  A: 

Seems like you'd want to do this in the background, if you can. After all, the majority of the time, it's going to be in the same order (unless the cities are really close together, and you travel a lot, I suppose).

If you do need to sort them at display time, you can probably sort through a few thousand points in no time at all. For many applications, treating the lattitude & longitude as rectangular coordinates is probably good enough. Calculating the actual distance isn't all that hard, but it'll be many times more computation than the sorting.

Oh, and if you've got your data points in an NSArray, look at sortedArrayUsingSelector: and sortedArrayUsingFunction:context:

Mark Bessey
To be more specific I would actually want it to tell me what casino I was in, not which city... which in Las Vegas could change from a couple hundred feet.
Michael Pryor
Oh, okay. In that case, you probably want to sort the list more often. Depending on whether this is the kind of application that people will leave running, or start and stop, it might make sense to update the sorting on startup.
Mark Bessey
Oh, and if distances are < 100 miles, and you're not near the poles, treating Lat. and Long. as rectangular coordinates is definitely just fine.
Mark Bessey
+1  A: 

Depends on the number of cities you have in your database. If it's a lot then you may want to use pre-pruning techniques to limit the search scope, like using pre-calculated 'zones' or limit search to 'cities of size X or larger,' etc. Since the cities don't move and populations and sizes don't change suddenly, think about how you can do as much pre-calculation as possible.

Another common technique is to have a separate 'nearby' table with distances from one city to those nearby. So all you have to do is find a nearby city to your current position and just show the cities that are nearest to that city.

Another trick worth emulating is that of UITableView, where it pulls info on-demand so at any given time, there are only the number of visible cells that need to be calculated. So instead of getting data on every city that is nearby, just try to limit the view to the 5-6 that are visible on the first screen and if you're allowing scrolling, fetch the rest as the user navigates up and down.

On a related note, here's a list of formulae for calculating distances between geopoints: http://www.movable-type.co.uk/scripts/latlong.html

Ramin
CLLocation supports calculating distances.
Roger Nolan
+2  A: 

You can sort an array using one of the sortedArrayUsing... messages to your array of locations.

Assuming each of your locations is represented using a CLLocation, you can use CLLocation's 'getDistanceFrom:' with the current location as the argument as part of the sort.

Roger Nolan