首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iOS Swift中使用mapView和CLLocationManager绘制两个位置之间的方向

如何在iOS Swift中使用mapView和CLLocationManager绘制两个位置之间的方向
EN

Stack Overflow用户
提问于 2016-11-05 06:39:59
回答 2查看 2.1K关注 0票数 9

我使用mapView.showUserLocation = true绘制用户位置,并将其显示为MKMapView对象上的注释。

现在,当我获得用户的位置时,我想要绘制一条从这组和弦到我的目的地的路径。我试着用MKDirections来做这个

我就是这么做的:

代码语言:javascript
复制
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印度中是不可用的,所以我想知道如何向用户显示导航指南。

谢谢你的帮助!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-16 12:37:55

代码语言:javascript
复制
Error Domain=MKErrorDomain Code=5 "Directions Not Available"

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

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

iOS模拟器-> 'Debug‘选项卡-> Location -> {Choose}

Xcode -> 'Debug‘选项卡->模拟位置-> {Choose}

,所以我建议您使用这个. GoogleMaps SDK并绘制路径。

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

票数 4
EN

Stack Overflow用户

发布于 2016-11-05 13:56:57

更好地使用google从源和目的地绘制polyline。您可以使用api

https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=%@&destinations=%@&key=

然后,从可以绘制路径的位置获取lat long的json数据。在MKMapkit中解码json数据并绘制折线

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40435508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档