快速计算地图的距离和预计到达时间 [英] Calculate distance and ETA from maps in swift
问题描述
我目前正在下面使用此代码,该代码将打开包含特定目的地驾驶指南的地图.
I am currently using this code below that will open maps with driving guide to a certain destination.
let lat1 : NSString = "57.619302"
let lng1 : NSString = "11.954928"
let latitute:CLLocationDegrees = lat1.doubleValue
let longitute:CLLocationDegrees = lng1.doubleValue
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitute, longitute)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
let options = [
MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span),
MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving
]
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = "Timo's Crib"
dispatch_async(dispatch_get_main_queue()) {
mapItem.openInMapsWithLaunchOptions(options)
}
}))
现在,打开地图时,您可以看到诸如ETA和到目的地的总距离之类的信息.如何提取或计算该确切信息?我仅在目标c中看到了示例,但没有很快看到.
Now when the maps are opened you can see information such as ETA and total distance to destination. How can i extract or calculate that exact information? I only saw examples in objective c but never in swift.
推荐答案
您可以使用MKDirectionsRequest
进行操作.首先,我们需要当前的位置,因为从您的代码中我们可以看到您已到达目的地.我正在使用CLLocationManagerDelegate
方法locationManager(_:didUpdateLocations:)
.
You can do it using MKDirectionsRequest
. First we need our current location, since from your code we see that you have the destination. I'm using CLLocationManagerDelegate
method locationManager(_:didUpdateLocations:)
.
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let sourcePlacemark = MKPlacemark(coordinate: locations.last!.coordinate, addressDictionary: nil)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
..
}
完美.现在我们有了源项目和目标项目.现在只是请求(为简化起见,我将您的代码放在方法中,但是目标位置后面的逻辑可能应该从此方法中提取):
Perfect. Now we have source item and destination item. Now just the request (to simplify things, I put your code inside the method, but the logic behind destination place should be probably extracted from this method):
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Get current position
let sourcePlacemark = MKPlacemark(coordinate: locations.last!.coordinate, addressDictionary: nil)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
// Get destination position
let lat1: NSString = "57.619302"
let lng1: NSString = "11.954928"
let destinationCoordinates = CLLocationCoordinate2DMake(lat1.doubleValue, lng1.doubleValue)
let destinationPlacemark = MKPlacemark(coordinate: destinationCoordinates, addressDictionary: nil)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
// Create request
let request = MKDirectionsRequest()
request.source = sourceMapItem
request.destination = destinationMapItem
request.transportType = MKDirectionsTransportType.Automobile
request.requestsAlternateRoutes = false
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler { response, error in
if let route = response?.routes.first {
print("Distance: \(route.distance), ETA: \(route.expectedTravelTime)")
} else {
print("Error!")
}
}
}
这篇关于快速计算地图的距离和预计到达时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!