如何使用mapView和CLLocationManager在iOS Swift中绘制两个位置之间的路线指示。

9

我正在使用mapView.showUserLocation = true来标记用户位置,并在我的MKMapView对象上显示它作为注释。

现在,当我已经获取了用户的位置,我想从该坐标集绘制到我的目的地的路径。我尝试使用MKDirections来实现这个目标。

下面是我尝试这样做的方式:

func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
    mapView.showsUserLocation = true
    print("test")
    //Setting Up Source Location
    let sourceLocation = self.mapView.userLocation.coordinate
    let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
    let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
    let sourceAnnotation = MKPointAnnotation()
    sourceAnnotation.title = "You are Here"

    if let location = sourcePlacemark.location {
        sourceAnnotation.coordinate = location.coordinate
    }

    //Setting Up Destination Location
    let destinationLocation = CLLocationCoordinate2D(latitude: 28.6621292, longitude: 77.30198310000003)
    let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
    let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
    let destinationAnnotation = MKPointAnnotation()
    destinationAnnotation.title = "Conclave is Here"

    if let location = destinationPlacemark.location {
        destinationAnnotation.coordinate = location.coordinate
    }


    self.mapView.removeAnnotations(self.mapView.annotations)
    self.mapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true)


    //Plotting a course from Source to Destination
    let directionRequest = MKDirectionsRequest()
    directionRequest.source = sourceMapItem
    directionRequest.destination = destinationMapItem
    directionRequest.transportType = .Automobile

    // Calculate the direction
    let directions = MKDirections(request: directionRequest)

    // 8.
    directions.calculateDirectionsWithCompletionHandler {
        (response, error) -> Void in

        guard let response = response else {
            if let error = error {
                //print("Error: \(error)")
            }

            return
        }

        let route = response.routes[0]
        self.mapView.addOverlay((route.polyline), level: MKOverlayLevel.AboveRoads)

        let rect = route.polyline.boundingMapRect
        self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
    }

}

然而,当我尝试调试时,我遇到了以下错误:Error: Error Domain=MKErrorDomain Code=5 "Directions Not Available" UserInfo=0x1742f7600 {NSLocalizedFailureReason=A route could not be determined between these locations., MKErrorGEOError=-403, MKDirectionsErrorCode=1, NSLocalizedDescription=Directions Not Available} 我做了一些研究,发现在印度MKDirections不可用,所以我想知道如何绕过向用户显示导航方向的问题。
谢谢帮助!

最佳选择是使用GOOGLEPLACEAPI来获取方向和地点信息。 - Himanshu Moradiya
2个回答

4

谷歌地图和苹果地图没有相同的基础数据。如果你这样做,你的路线可能会在苹果地图上画出不存在的道路,或者距离道路在苹果地图上绘制的位置有几米之遥。 - Jordan Smith
如果你要做这个,你应该使用Google Maps SDK而不是使用Apple Maps。 - Jordan Smith

4
Error Domain=MKErrorDomain Code=5 "Directions Not Available"

如果位置不属于以下任何国家列表中的国家,则最可能出现此错误:http://www.apple.com/ios/feature-availability/#maps-directions

在iOS模拟器上,您可以轻松自定义当前位置。两种方法:

iOS模拟器 ->“调试”选项卡 -> 位置 -> {选择}

Xcode ->“调试”选项卡 -> 模拟位置 -> {选择}

因此我建议使用此方法。 GoogleMaps SDK并绘制路线。

https://gist.github.com/himanshu-benzatine/10670936c8f16ea1ae482bc6bb684adc


谢谢!那真的很有帮助。我只是想知道,如果我要将那段C#代码移植到Swift,除了语法之外还需要做哪些更改吗? - Ayush Gupta
如果您将代码从C#更改为Swift,则语法及其方法将发生变化。 - Himanshu Moradiya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接