【发布时间】:2011-12-21 01:44:55
【问题描述】:
在 iPhone 应用程序中,如何计算 MKMapView 中两点之间的距离
如下图所示?
第一个点将是地图视图中可见地图的中心点。
第二个点可以是地图视图可见矩形的任意角(例如,我取了左上角)。
我想以米为单位计算这个距离。我怎样才能做到这一点?
我的目标是计算MKMapview 中可见地图矩形的比率。
【问题讨论】:
标签: objective-c cocoa-touch ios4 mkmapview
在 iPhone 应用程序中,如何计算 MKMapView 中两点之间的距离
如下图所示?
第一个点将是地图视图中可见地图的中心点。
第二个点可以是地图视图可见矩形的任意角(例如,我取了左上角)。
我想以米为单位计算这个距离。我怎样才能做到这一点?
我的目标是计算MKMapview 中可见地图矩形的比率。
【问题讨论】:
标签: objective-c cocoa-touch ios4 mkmapview
您可以通过以下方式获得中心的纬度/经度:
convertPoint:toCoordinateFromView:
loc1 和 loc2 都是 CLLocation 对象。
CLLocationDistance dist = [loc1 distanceFromLocation:loc2];
因此,这两个提示应该对您有所帮助。如果您需要一些代码,请告诉我:-)
【讨论】:
以下是计算所需距离的方法:
// 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];
【讨论】:
Swift 3+
let distance: CLLocationDistance = location1.distance(from: location2)
【讨论】: