【问题标题】:How to plot Directions between two locations in iOS Swift using mapView and CLLocationManager如何使用 mapView 和 CLLocationManager 在 iOS Swift 中的两个位置之间绘制方向
【发布时间】:2017-03-19 01:04:59
【问题描述】:

我正在使用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 获取方向和地点信息的最佳选择。

标签: ios swift swift2 mkmapview cllocationmanager


【解决方案1】:

最好使用 google api 从源和目的地绘制折线。你可以使用api

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

然后你得到 lat long(s) 的 json 数据,从中可以绘制路径。解码json数据并在MKMapkit中绘制折线

【讨论】:

  • 谷歌地图和苹果地图没有相同的基础数据。如果您这样做,您的路线可能会覆盖 Apple 地图中不存在的道路,或者位于距离 Apple 地图中道路绘制位置几米的位置。
  • 如果您打算这样做,您应该使用 Google Maps SDK 而不是使用 Apple Maps。
【解决方案2】:
Error Domain=MKErrorDomain Code=5 "Directions Not Available"

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

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

iOS 模拟器 -> '调试' 选项卡 -> 位置 -> {Choose}

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

所以我建议你使用这个。 GoogleMaps SDK 并绘制路线。

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

【讨论】:

  • 谢谢!那真的很有帮助。只是想知道,如果我将该代码从 C# 移植到 Swift,除了语法之外是否还需要进行任何更改?
  • 是的,如果您将代码从 c# 更改为 swift,那么语法及其方法都会发生变化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多