【问题标题】:How to get driving distance between two point?如何获得两点之间的行驶距离?
【发布时间】:2013-07-18 06:02:47
【问题描述】:

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

谢谢

【问题讨论】:

  • 因为保密协议不能讨论,但是看看iOS7 MapKit的变化。

标签: iphone objective-c core-location


【解决方案1】:

斯威夫特 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)
        }
    }
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 不幸的是,你必须为这个 api 付费(如果你有体面的使用)
    猜你喜欢
    • 2019-07-26
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 2012-08-06
    相关资源
    最近更新 更多