使用MapKit获取道路距离 - Swift

时间:2020-12-07 21:18:12

What are the classes I should look into if I want to get road distance?

如果我想获得公路距离,我应该考虑哪些课程?

All I want to be able to get is the road-distance from point A to point B, I don't need to show step by step directions nor the map.

我想要的就是从A点到B点的道路距离,我不需要显示一步一步的指示,也不需要显示地图。

Any suggestions?

有什么建议么?

Thanks

谢谢

1 个解决方案

#1


11  

You need to make a MKDirections request. From calculateDirectionsWithCompletionHandler you will get a MKDirectionsResponse. This has a routes array of MKRoutes. Each route has a distance (i.e. road distance) property.

您需要发出MKDirections请求。从calculateDirectionsWithCompletionHandler您将获得MKDirectionsResponse。这有一个MKRoutes的路由数组。每条路线都有一个距离(即道路距离)属性。

    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")
    }

#1


11  

You need to make a MKDirections request. From calculateDirectionsWithCompletionHandler you will get a MKDirectionsResponse. This has a routes array of MKRoutes. Each route has a distance (i.e. road distance) property.

您需要发出MKDirections请求。从calculateDirectionsWithCompletionHandler您将获得MKDirectionsResponse。这有一个MKRoutes的路由数组。每条路线都有一个距离(即道路距离)属性。

    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")
    }