Paul
Paul

Reputation: 269

iphone -- convert MKMapPoint distances to meters

Say I have a square which consists of four CLLocationCoordinate2D points, which are in lat, lon, and I want to find the area of the square in meters. I convert the CLLocationCoordinate2D points into MKMapPoints, and I find the area in X-Y space. However, the area I find is in the units of MKMapPoint, which don't directly translate to meters. How can I translate this area in MKMapPoint-space back into meters?

Upvotes: 2

Views: 4491

Answers (2)

user467105
user467105

Reputation:

The MapKit function MKMetersBetweenMapPoints makes this easier.

For example, if you wanted to get the area of the currently displayed region:

MKMapPoint mpTopLeft = mapView.visibleMapRect.origin;

MKMapPoint mpTopRight = MKMapPointMake(
    mapView.visibleMapRect.origin.x + mapView.visibleMapRect.size.width, 
    mapView.visibleMapRect.origin.y);

MKMapPoint mpBottomRight = MKMapPointMake(
    mapView.visibleMapRect.origin.x + mapView.visibleMapRect.size.width, 
    mapView.visibleMapRect.origin.y + mapView.visibleMapRect.size.height);

CLLocationDistance hDist = MKMetersBetweenMapPoints(mpTopLeft, mpTopRight);
CLLocationDistance vDist = MKMetersBetweenMapPoints(mpTopRight, mpBottomRight);

double vmrArea = hDist * vDist;

The documentation states that the function takes "into account the curvature of the Earth."

Upvotes: 8

vfn
vfn

Reputation: 6066

You can use the Haversine formula to calculate it, assuming that the earth is a perfect sphere.

To understand how lat/lon vs meters works in the context of the earth, you may find it interesting to read about Nautical miles.

You can find some more resources and some sample code by googling objective-c Haversine formula.

Enjoy!

Upvotes: 0

Related Questions