没有获得" MAP路线"

时间:2018-03-12 05:49:52

标签: ios swift mapkit

为什么我没有获得" 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)
}

1 个答案:

答案 0 :(得分:1)

我添加了此方法以在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)
    }

这里的完整代码https://github.com/rmelian2014/MapRouteQuestion

enter image description here