在iPhone应用程序中,如何计算在MKMapView中两个点之间的距离,如下图所示?
第一个点将是mapview中可见地图的中心点。
第二个点将是地图视图可见矩形的任何角落(在这里,例如我已经选取了左上角)。
我想以米为单位计算这段距离。我该如何实现呢?
我的目标是计算MKMapview中可见地图矩形的比率。
你可以通过以下方式获取中心的纬度/经度:
convertPoint:toCoordinateFromView:
loc1和loc2都是CLLocation对象。
CLLocationDistance dist = [loc1 distanceFromLocation:loc2];
那么这两个提示应该对你有所帮助。如果你需要一些代码,请告诉我:-)
Swift 3+
let distance: CLLocationDistance = location1.distance(from: location2)
// 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];