Determining distance using mapkit

How is a distance of 1000 feet or 1/2 mile determined using mapkit? Either the radius from a pin, or the distance between two contacts.

For example, I center the map on pin A. Pins B, C and D are also on the map at different distances from pin A. B and C are within 1/2 mile of A, but D is 1.6 km from, I I would like to know that B and C are within 1/2 mile of A. How can I calculate this?

+5
source share
3 answers

Since you stated that the two different points are “contacts,” I assume that you are using MKPinAnnotationView (or some other kind of annotation view). If not, you will have to find a place in another way.

If you have pointers to annotation objects for these locations, you can easily call -coordinateonto them, create CLLocations from these coordinates (using -initWithLatitude:longitude:), and then use the method -getDistanceFromto extract the distance in meters. From there it easily turns into miles. Everything says that the code will look something like this:

CLLocationCoordinate2D pointACoordinate = [pointAAnnotation coordinate];
CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude];
CLLocationCoordinate2D pointBCoordinate = [pointBAnnotation coordinate];
CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude];
double distanceMeters = [pointALocation getDistanceFrom:pointBLocation];
double distanceMiles = (distanceMeters / 1609.344);

As a result, you get the distance in miles and can compare it from there. Hope this helps.

+13
source

getDistanceFrom now deprecated, so an alternative answer for those who want to do this.

CLLocationCoordinate2D pointACoordinate = [pointAAnnotation coordinate];
CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude];  

CLLocationCoordinate2D pointBCoordinate = [pointBAnnotation coordinate];
CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude];  

float distanceMeters = [pointALocation distanceFromLocation:pointBLocation];
float distanceMiles = (distanceMeters / 1609.344);  

[pointALocation release];
[pointBLocation release];  

, float double, int, float, :

int distanceCastToInt = (int) [pointALocation distanceFromLocation:pointBLocation];

int , , :

pointAAnnotation.title = @"Point A";
pointAAnnotation.subtitle = [NSString stringWithFormat: @"Distance: %im",distanceCastToInt];

, , ": 50 ".

+12

This is Swift 3 equivalent of this answer:

let pointACoordinate = pointAAnnotation.coordinate
let pointALocation = CLLocation(latitude: pointACoordinate.latitude, longitude: pointACoordinate.longitude)
let pointBCoordinate = pointBAnnotation.coordinate
let pointBLocation = CLLocation(latitude: pointBCoordinate.latitude, longitude: pointBCoordinate.longitude)
let distanceMeters = pointALocation.distance(from: pointBLocation)
let distanceMiles = (distanceMeters / 1609.344)
0
source

All Articles