为什么我没有获取“MAP Route”,这段代码有什么问题。或者,请提供“在 map 上获取路线”的代码
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
let selectedLoc = view.annotation
print("Annotation '\(String(describing: selectedLoc?.title!))' has been selected")
let currentLocMapItem = MKMapItem.forCurrentLocation()
let selectedPlacemark = MKPlacemark(coordinate: (selectedLoc?.coordinate)!, addressDictionary: nil)
let selectedMapItem = MKMapItem(placemark: selectedPlacemark)
let mapItems = [selectedMapItem, currentLocMapItem]
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
MKMapItem.openMaps(with: mapItems, launchOptions:launchOptions)
}
最佳答案
我添加了这个方法来创建 2 个 MKMapItem
func createRouteTo(fromMapItem: MKMapItem,selectedMapItem: MKMapItem) {
if(CLLocationCoordinate2DIsValid(self.mapview.userLocation.coordinate) && CLLocationCoordinate2DIsValid(selectedMapItem.placemark.coordinate))
{
self.showLoading(message: "Creando Ruta...")
let directionRequest = MKDirectionsRequest()
directionRequest.source = MKMapItem(placemark: MKPlacemark(coordinate: fromMapItem.placemark.coordinate, addressDictionary: nil))
directionRequest.destination = MKMapItem(placemark: MKPlacemark(coordinate: selectedMapItem.placemark.coordinate, addressDictionary: nil))
directionRequest.transportType = .automobile
directionRequest.requestsAlternateRoutes = false
// Calculate the direction
let directions = MKDirections(request: directionRequest)
directions.calculate { [unowned self] (directionResponse, error) in
self.hideLoading(withDelay: 0.1)
guard let response = directionResponse else {
if let error = error {
print("Error: \(error)")
let alert = UIAlertController.init(title:"Error Creando Ruta",
message:error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "Aceptar", style: .destructive, handler:nil))
self.present(alert, animated:true , completion:nil)
}
return
}
if(self.currentRouteOverlay != nil)
{
self.mapview.remove(self.currentRouteOverlay!)
}
let route = response.routes[0]
self.currentRouteOverlay = route.polyline
self.mapview.add(route.polyline, level: .aboveRoads)
let rect = route.polyline.boundingMapRect
self.mapview.setVisibleMapRect(rect, edgePadding: UIEdgeInsetsMake(15, 15, 15, 15), animated: true)
}
}
}
这是你修改的方法
// This method will be called when we will tap on the annotation for Details.Then it will open in new View and make a route from current location to selected location.
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
let selectedLoc = view.annotation
print("Annotation '\(String(describing: selectedLoc?.title!))' has been selected")
let currentLocMapItem = MKMapItem(placemark: MKPlacemark(coordinate: mapView.userLocation.coordinate))
let selectedPlacemark = MKPlacemark(coordinate: (selectedLoc?.coordinate)!, addressDictionary: nil)
let selectedMapItem = MKMapItem(placemark: selectedPlacemark)
self.createRouteTo(fromMapItem:currentLocMapItem, selectedMapItem: selectedMapItem)
}
关于ios - 没有得到 "MAP Route",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49228863/