【问题标题】:How can I calculate the distance between two points in MkMapview?如何计算 MkMapview 中两点之间的距离?
【发布时间】:2011-12-21 01:44:55
【问题描述】:

在 iPhone 应用程序中,如何计算 MKMapView 中两点之间的距离 如下图所示?

第一个点将是地图视图中可见地图的中心点。

第二个点可以是地图视图可见矩形的任意角(例如,我取了左上角)。

我想以米为单位计算这个距离。我怎样才能做到这一点?

我的目标是计算MKMapview 中可见地图矩形的比率。

【问题讨论】:

    标签: objective-c cocoa-touch ios4 mkmapview


    【解决方案1】:

    您可以通过以下方式获得中心的纬度/经度:

    convertPoint:toCoordinateFromView:
    

    loc1 和 loc2 都是 CLLocation 对象。

    CLLocationDistance dist = [loc1 distanceFromLocation:loc2];
    

    因此,这两个提示应该对您有所帮助。如果您需要一些代码,请告诉我:-)

    【讨论】:

    • 感谢您的回复,您能告诉我如何获取角点的经纬度或坐标(第 2 点)吗?我可以获得中心点的坐标(第 1 点)
    • This answer 告诉如何获取角点的纬度/经度。
    • 当我与谷歌交叉检查时。使用 mapKit 查找距离不正确。
    【解决方案2】:

    以下是计算所需距离的方法:

    // 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];
    

    【讨论】:

    • centerCoordinate 和cornerCoordinate 需要是 CLLocation 类型才能使 distanceFromLocation 起作用
    【解决方案3】:

    Swift 3+

    let distance: CLLocationDistance = location1.distance(from: location2)
    

    【讨论】:

      猜你喜欢
      • 2010-10-30
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 2011-04-23
      • 2014-11-14
      相关资源
      最近更新 更多