快速计算两个位置之间的距离

时间:2017-09-03 18:28:18

标签: swift macos mapkit

我想获得两个地点之间的距离。 我试过这段代码:

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
   CLGeocoder().geocodeAddressString("ADDRESS OF LOC 2") {    (placemarks, error) in
   guard let placemarks = placemarks, let loc2 = placemarks.first?.location
      else {
         return
      }


    let loc1 = CLLocation(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)

    var distance = loc1.distance(from: loc2)
    print((distance/1000).rounded(), " km")
     }
}

我的问题是我的距离不对。 打印结果为“2 km”

如果我计算“地图”中相同位置之间的距离,我会得到两条路线选项,长度为“2,7 km”和“2,9 km”

我错了什么?

3 个答案:

答案 0 :(得分:2)

来自the documentation

  

此方法通过跟踪两个位置之间的曲线来测量两个位置之间的距离。得到的弧线是平滑的曲线,不考虑两个位置之间的特定高度变化。

所以你正在计算直线距离,“随着乌鸦飞行”,而不是跟随道路,这将导致更长的路线,就像你在地图中看到的那样。你需要像MapKit MKDirectionsRequest这样的东西来匹配你在地图应用中看到的路线。 Ray Wenderlich has a good tutorial.

这是一个我刚刚敲了一个在macOS Playground中运行的例子:

//: Playground - noun: a place where people can play

import Cocoa
import MapKit
import CoreLocation

// As we're waiting for completion handlers, don't want the playground
// to die on us just because we reach the end of the playground.
// See https://stackoverflow.com/questions/40269573/xcode-error-domain-dvtplaygroundcommunicationerrordomain-code-1
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true

let geocoder = CLGeocoder()
geocoder.geocodeAddressString("1 Pall Mall East, London SW1Y 5AU") { (placemarks: [CLPlacemark]? , error: Error?) in
    if let placemarks = placemarks {
        let start_placemark = placemarks[0]
        geocoder.geocodeAddressString("Buckingham Palace, London SW1A 1AA", completionHandler: { ( placemarks: [CLPlacemark]?, error: Error?) in
            if let placemarks = placemarks {
                let end_placemark = placemarks[0]

                // Okay, we've geocoded two addresses as start_placemark and end_placemark.
                let start = MKMapItem(placemark: MKPlacemark(coordinate: start_placemark.location!.coordinate))
                let end = MKMapItem(placemark: MKPlacemark(coordinate: end_placemark.location!.coordinate))

                // Now we've got start and end MKMapItems for MapKit, based on the placemarks. Build a request for 
                // a route by car.
                let request: MKDirectionsRequest = MKDirectionsRequest()
                request.source = start
                request.destination = end
                request.transportType = MKDirectionsTransportType.automobile

                // Execute the request on an MKDirections object
                let directions = MKDirections(request: request)
                directions.calculate(completionHandler: { (response: MKDirectionsResponse?, error: Error?) in
                    // Now we should have a route.
                    if let routes = response?.routes {
                        let route = routes[0]
                        print(route.distance) // 2,307 metres.
                    }
                })
            }
        })
    }
}

答案 1 :(得分:2)

如果您想要旅行距离而不是"当乌鸦飞过"时,请使用MKDirections,例如:

func routes(to item: MKMapItem, completion: @escaping ([MKRoute]?, Error?) -> Void) {
    let request = MKDirections.Request()
    request.source = MKMapItem.forCurrentLocation()
    request.destination = item
    request.transportType = .automobile

    let directions = MKDirections(request: request)
    directions.calculate { response, error in
        completion(response?.routes, error)
    }
}

如果你想格式化到一个小数位,我建议使用NumberFormatter,因此它适用于小数分隔符不是.的国际用户的格式:

self.routes(to: item) { routes, error in
    guard let routes = routes, error == nil else {
        print(error?.localizedDescription ?? "Unknown error")
        return
    }

    let formatter = NumberFormatter()
    formatter.numberStyle = .decimal
    formatter.minimumFractionDigits = 1
    formatter.maximumFractionDigits = 1

    for route in routes {
        let distance = route.distance / 1000
        print(formatter.string(from: NSNumber(value: distance))!, "km")
    }
}

答案 2 :(得分:0)

使用MapKit和Swift 5

计算两个位置之间的距离,它将为任何人提供帮助

示例功能:我已经在Google Map和Apple Map中进行了测试

        let startLocation : CLLocation = CLLocation.init(latitude: 23.0952779, longitude: 72.5274129)
        let endLocation : CLLocation = CLLocation.init(latitude: 23.0981711, longitude: 72.5294229)
        let distance = startLocation.distance(from: endLocation)
        self.getDistance(departureDate: Date().adjust(hour: 8, minute: 0, second: 0, day: 0, month: 0), arrivalDate: Date().adjust(hour: 8, minute: 10, second: 0, day: 0, month: 0), startLocation: startLocation, endLocation: endLocation) { (distanceInMeters) in

            print("fake distance: \(distance)")
            let fakedistanceInMeter = Measurement(value: distance, unit: UnitLength.meters)
            let fakedistanceInKM = fakedistanceInMeter.converted(to: UnitLength.kilometers).value
            let fakedistanceInMiles = fakedistanceInMeter.converted(to: UnitLength.miles).value
            print("fakedistanceInKM :\(fakedistanceInKM)")
            print("fakedistanceInMiles :\(fakedistanceInMiles)")


            print("actualDistance : \(distanceInMeters)")

            let distanceInMeter = Measurement(value: distanceInMeters, unit: UnitLength.meters)
            let distanceInKM = distanceInMeter.converted(to: UnitLength.kilometers).value
            let distanceInMiles = distanceInMeter.converted(to: UnitLength.miles).value
            print("distanceInKM :\(distanceInKM)")
            print("distanceInMiles :\(distanceInMiles)")
        }

功能使用

                    self.getDistance(departureDate: trip.departure.dateTime, arrivalDate: trip.arrival.dateTime, startLocation: startLocation, endLocation: endLocation) { (actualDistance) in
                        print("actualDistance : \(actualDistance)")
                    }

我对上述功能进行了改进,并在此处添加了代码,希望对您有所帮助。

func calculateDistancefrom(departureDate: Date, arrivalDate: Date, sourceLocation: MKMapItem, destinationLocation: MKMapItem, doneSearching: @escaping (_ distance: CLLocationDistance) -> Void) {

        let request: MKDirections.Request = MKDirections.Request()

        request.departureDate = departureDate
        request.arrivalDate = arrivalDate

        request.source = sourceLocation
        request.destination = destinationLocation

        request.requestsAlternateRoutes = true
        request.transportType = .automobile

        let directions = MKDirections(request: request)
        directions.calculate { (directions, error) in
            if var routeResponse = directions?.routes {
                routeResponse.sort(by: {$0.expectedTravelTime <
                    $1.expectedTravelTime})
                let quickestRouteForSegment: MKRoute = routeResponse[0]

                doneSearching(quickestRouteForSegment.distance)
            }
        }
    }

    func getDistance(departureDate: Date, arrivalDate: Date, startLocation : CLLocation, endLocation : CLLocation, completionHandler: @escaping (_ distance: CLLocationDistance) -> Void) {

        let destinationItem =  MKMapItem(placemark: MKPlacemark(coordinate: startLocation.coordinate))
        let sourceItem      =  MKMapItem(placemark: MKPlacemark(coordinate: endLocation.coordinate))
        self.calculateDistancefrom(departureDate: departureDate, arrivalDate: arrivalDate, sourceLocation: sourceItem, destinationLocation: destinationItem, doneSearching: { distance in
            completionHandler(distance)
        })
    }