1

我想创建可以计算 A 点到 B 点之间的行驶距离的应用程序。我知道CLLocation必须distanceFromLocation:计算 2 点之间的距离,但它只计算从 A 点到 B 点的直线。它有办法计算两点之间的行驶距离?如何?有样品吗?

谢谢

4

2 回答 2

3

斯威夫特 3:

func routingDistance(userNotation: CLLocation, destinationLocation: CLLocation, completion: @escaping (CLLocationDistance) -> Void) {
    let request:MKDirectionsRequest = MKDirectionsRequest()

    // source and destination are the relevant MKMapItems
    let sourceS = CLLocationCoordinate2D(latitude: userNotation.coordinate.latitude, longitude: userNotation.coordinate.longitude)
    let destinationD = CLLocationCoordinate2D(latitude: destinationLocation.coordinate.latitude, longitude: destinationLocation.coordinate.longitude)
    let sourcePM = MKPlacemark(coordinate: sourceS)
    let destinationPM = MKPlacemark(coordinate: destinationD)
    request.source = MKMapItem(placemark: sourcePM)
    request.destination = MKMapItem(placemark: destinationPM)

    // Specify the transportation type
    request.transportType = MKDirectionsTransportType.automobile;

    // If you're open to getting more than one route,
    // requestsAlternateRoutes = true; else requestsAlternateRoutes = false;
    request.requestsAlternateRoutes = true

    let directions = MKDirections(request: request)

    directions.calculate { (response, error) in
        if let response = response, let route = response.routes.first {
            completion(route.distance)
        }
    }
}
于 2017-05-02T04:25:03.873 回答
2

我认为iOS中没有任何功能,distanceFromLocation会给出两个坐标之间的空气距离。

但是是的,您可以使用google APIS计算道路距离。

于 2013-07-18T06:16:12.950 回答