ios - 没有得到 "MAP Route"

标签 ios swift mapkit

为什么我没有获取“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

之间的 map 路线
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

关于ios - 没有得到 "MAP Route",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49228863/

相关文章:

ios - Titanium中tableview行中的多行文本

ios - 如何使用 indexesOfObjectsPassingTest : in Swift

swift - Xcode 6 或 iOS 8 错误 : UIAlertController Not Showing Message?

swift - 收到通知后不会调用通知内容扩展

ios - MKMapView 显示额外的缩放区域。如何?

iphone - 奇怪的错误 : wait_fences: failed to receive reply: 10004003

ios - 使用 json 数据填充 tableView

ios - UITableview 问题的动态高度

ios - 在 iOS 应用程序中使用 Apple Maps 是免费的吗?

ios - 右标注附件方法及实现