2

I have around 800 geo co-ordinates in my iPhone app as a flat file. I am searching for an effective way to find an algorithm which will take the current user location, loop through all these 800 coordinates and pull only the coordinates which are in 10 miles vicinity. How effectively this can be done? Also please share links which will get me the basic understanding on the maths behind this.

rmtheis
  • 5,992
  • 12
  • 61
  • 78
thndrkiss
  • 4,515
  • 8
  • 57
  • 96
  • 1
    You can probably see this question http://stackoverflow.com/questions/913576/finding-the-closest-point-to-a-given-point they provide a simple example although it is not written in Objective-C. – El Developer Feb 18 '12 at 00:30
  • I think you've got a different problem than the linked question, in that it is trying to find _the closest_ and you're trying to find _all_ within a specified distance, right? – sarnold Feb 18 '12 at 00:46
  • It has been years. Have you found a good solution? – Dpedrinha Jun 09 '22 at 17:23

2 Answers2

4

Here is a link for a question where the final code of OP may help you understand the how to create locations from coordinates and how to compute the distance between them.

Here is how to create a location:

CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

And here is how to find the distance between two locations:

CLLocationDistance distance = [locationA distanceFromLocation:locationB]; //CLLocationDistance is a double

However you don't have to sort the locations. Just loop through them and add the near locations to an array.

Community
  • 1
  • 1
sch
  • 27,436
  • 3
  • 68
  • 83
3

First, I think everyone agrees that to compute distance, you need to use the Haversine function.

Finding the closest point to a given point

If the search time is an issue (iterating over the 800 data points you mentioned) then how about a 2D hash? Simply load the dataset into buckets or regions based on lat/long - then, you won't have to search through the whole data set - only the possible buckets that may contain matches.

Good hash function for a 2d index

Community
  • 1
  • 1
Steve
  • 31,144
  • 19
  • 99
  • 122
  • If I have 10 points on a map. And I want to find the nearest point to a given `target` point. And I get my ruler out and calculate the distance from `target` to each point. Won't I find the closest point ? Why would I need haversine ? – dimitris93 May 18 '16 at 23:28