How can I calculate the distance between two points in MkMapview?
Asked Answered
I

3

8

In an iPhone App how can you calculate distance between two points in MKMapView as shown in the image below?

The first point would be the center point of the visible map in the mapview.

The 2nd point would be any of corner of the visible rectangle of the mapview (here, for example I have taken the top left point).

enter image description here

I want to calculate this distance in meters. How can I achieve that?

My goal is to calculate the ratios of the visible Map rectangle in MKMapview.

Identic answered 3/11, 2011 at 10:51 Comment(0)
A
32

You can get lat/lon of the center with:

convertPoint:toCoordinateFromView:

loc1 and loc2 are both CLLocation objs.

CLLocationDistance dist = [loc1 distanceFromLocation:loc2];

So these two tips should help you. if you need some code, let me know :-)

Antitoxic answered 3/11, 2011 at 10:56 Comment(3)
Thanks for reply can u tell me how to get lat-long or coordinate of corner point (2nd point) I could get the coordinate of center point (1st point)Identic
This answer tells how to get the lat/lon of the corner points.Carmoncarmona
When I cross check with google. find distance using mapKit is not correct.Corticosteroid
R
3

Swift 3+

let distance: CLLocationDistance = location1.distance(from: location2)
Roup answered 30/1, 2018 at 12:34 Comment(0)
W
2

Here is how you can calculate the wanted distance :

// You first have to get the corner point and convert it to a coordinate
MKMapRect mapRect = self.mapView.visibleMapRect;
MKMapPoint cornerPointNW = MKMapPointMake(mapRect.origin.x, mapRect.origin.y);
CLLocationCoordinate2D cornerCoordinate = MKCoordinateForMapPoint(cornerPointNW);

// Then get the center coordinate of the mapView (just a shortcut for convenience)
CLLocationCoordinate2D centerCoordinate = self.mapView.centerCoordinate

// And then calculate the distance
CLLocationDistance distance = [cornerCoordinate distanceFromLocation:centerCoordinate];
Washday answered 3/11, 2011 at 12:15 Comment(1)
centerCoordinate and cornerCoordinate need be of type CLLocation for distanceFromLocation to workDuumvir

© 2022 - 2024 — McMap. All rights reserved.