How is a 1000ft or 1/2 mile distance determined with mapkit? Either a radius from some pin or the distance between two pins.
For example, I center the map on pin A. Pins B, C, and D are also on the map at various distances from pin A. B and C are within 1/2 mile from A but D is 1 mile away. I'd like to know that B and C are within 1/2 mile from A. How can I calculate that?
getDistanceFrom
is now deprecated so here's an alternative answer for anyone looking to do this.As above, you could use
float
instead ofdouble
and you could cast the results to an int if you don't require the precision of thefloat
like so:The
int
is handy if you wanted to give a rough idea of distance in the annotation like so:The subtitle of the annotation would read "Distance: 50m" for example.
Since you've stated that the two different points are "pins", I'm going to assume you're using MKPinAnnotationView (or some other annotation view). If not, you're going to have to get the location some other way.
If you have pointers to the annotation objects for these locations, then you can easily call
-coordinate
on them, create CLLocations from these coordinates (using-initWithLatitude:longitude:
), and then use the method-getDistanceFrom
to retrieve the distance in meters. From there, it's an easy conversion to miles. All told, the code would look something like this:You'll end up with the distance in miles, and can compare it from there. Hope this helps.
This is Swift 3 equivalent of the given answer: