iphone — convert MKMapPoint distances to meters

浪子不回头ぞ 提交于 2019-11-30 19:34:08

问题


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?


回答1:


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."




回答2:


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!



来源:https://stackoverflow.com/questions/7395925/iphone-convert-mkmappoint-distances-to-meters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!