使用 MapKit 获取道路距离 - Swift

Get road distance with MapKit - Swift

如果我想获得道路距离,我应该研究哪些类?

我只想得到从A点到B点的道路距离,我不需要显示详细的路线和地图。

有什么建议吗?

谢谢

您需要MKDirections request. From calculateDirectionsWithCompletionHandler you will get a MKDirectionsResponse. This has a routes array of MKRoutes. Each route has a distance(即道路距离)属性。

    let source = MKMapItem( placemark: MKPlacemark(
        coordinate: CLLocationCoordinate2DMake(-41.27, 173.28),
        addressDictionary: nil))
    let destination = MKMapItem(placemark: MKPlacemark(
        coordinate: CLLocationCoordinate2DMake(-41.11, 173),
        addressDictionary: nil))

    let directionsRequest = MKDirectionsRequest()
    directionsRequest.source = source
    directionsRequest.destination = destination

    let directions = MKDirections(request: directionsRequest)

    directions.calculateDirectionsWithCompletionHandler { (response, error) -> Void in
        print(error)
        let distance = response!.routes.first?.distance // meters
        print("\(distance! / 1000)km")
    }