How To Detect A User Is Within Range Of An Annotation

1.3k Views Asked by At

So I am working on a project that includes many uses placing annotations all around a map. The annotation, (which is a custom image with a much larger circular range) appears on the screen and, ideally, I would like for a user to be:

  1. Notified if they are within the range of a annotation and
  2. Not be allowed to place another annotation within the range of another one if the circular pins overlap by, say, more than 25%

I think this is a pretty unique question and should be fun for somebody to help out with, so have fun! Thanks everybody!

2

There are 2 best solutions below

12
AtWork On BEST ANSWER

You can check the distance from each annotation using

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

This method measures the distance between the two locations by tracing a line between them that follows the curvature of the Earth. The resulting arc is a smooth curve and does not take into account specific altitude changes between the two locations.

For more details refer Link

2
Pranav Wadhwa On

Try this:

let location = CLLocation(latitude: 1, longitude: 1)//Or user's location
let distance = location.distance(from: anotherLocation)

Edit:

As mentioned in the comments, you wanted to create an equidistant point. I suggest manually doing that:

Subtract the annotation's location from he user's location. Then add your distance back to the original one. For example:

The user's location = (1, 1)

The annotation's location = (3, 2)

Vertical difference would be 2

Horizontal difference would be 1

Then:

(3 + 2, 2 + 1)

Your result: (5, 3)

Now you would have two points (the one you just created and the user's location) at each end with a center point (original annotation)