Reputation: 2562
How can I add a radius to a CLLocation
coordinate? What I want is to see what clients are near a specific coordinate.
What I need is to add 2000 meters, or 2km to a coordinate.
How do I do that?
Upvotes: 0
Views: 1612
Reputation: 16827
You can't "add a radius" to a coordinate. What you can do if you have CLLocation
objects (say loc1 and loc2), is calculate the distance between them:
CLLocationDistance dist = [loc1 distanceFromLocation:loc2];
and see if dist (which is in meters) is more or less than 2000.0.
Converting between coordinate latitudes/longitudes (which are polar coordinates) and distances is complicated, which is why the SDK provides you with this functionality.
Upvotes: 1
Reputation: 3437
Since the distance is small, you can use the equirectangular distance approximation. This approximation is faster than using the Haversine formula. So, to get the distance from your reference point (lat1/lon1) to the point your are testing (lat2/lon2), use the formula below. Important Note: you need to convert all lat/lon points to radians:
R = 6371 // radius of the earth in km
x = (lon2 - lon1) * cos( 0.5*(lat2+lat1) )
y = lat2 - lat1
d = R * sqrt( x*x + y*y )
If d is less than 2, then your point is within 2km of your reference point.
To efficiently march through your points, order the points on longitude. Longitudes that are 2 degrees away will be greater than 2km (unless you are near a pole), therefore you don't need to loop through those.
Upvotes: 1