开发者

Finding closest location using CLLocationManager

开发者 https://www.devze.com 2023-01-23 06:33 出处:网络
I have a bunch of airports with geocoordinates stored in a glist. I am then using CLLocationManager to g开发者_StackOverflowet current location.

I have a bunch of airports with geocoordinates stored in a glist.

I am then using CLLocationManager to g开发者_StackOverflowet current location.

I now want to go through each item in the glist and compare it to the current location and order the airports from closest to furthest.

My algorithm sucks - any ideas?


If you have CLLocation instances, you can get their relative distance using:

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location

Now you write:

My algorithm sucks - any ideas?

Well, what is your algorithm? Why does it suck? Any code you have?

From the high level perspective, I would do it like this:

  1. compute the distace for each airport coordinate from the curent distance in O(n), where n is number of coordinates
  2. sort the items using some stable algorithm like QuickSort (...NSArray's sort should work OK...) which takes you O(n*log n), or make it work so that you can use radix sort that work in O(n)

Is there a reason why this wouldn't work? :)

  • CLLocation class reference
  • Radix sort
0

精彩评论

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