ios - 路线未显示在 MKMapView 中?

标签 ios swift routes mkmapview

我有这些方法可以在两个 CLLocation 之间为 MKMapView 添加路由。我有两个有效的 pickUpDistanceLocation & dropOffDistanceLocation

 func addRoutesOverLayForMapView(){

        var source:MKMapItem?
        var destination:MKMapItem?
        println("\(pickUpDistanceLocation)")
        println("\(dropOffDistanceLocation)")

        //i also tested with these locations
        //let sourcelocation = CLLocation(latitude:  40.7141667, longitude: -74.0063889)
        //let destinationLocation = CLLocation(latitude: 38.89, longitude: 77.03)

        CLGeocoder().reverseGeocodeLocation(pickUpDistanceLocation, completionHandler: {(placemarks,error)-> Void in

            if (error != nil) {
                println("Reverse geocoder failed with error" + error.localizedDescription)
                return
            }

            if placemarks.count > 0 {
                if let placemark: MKPlacemark = placemarks![0] as? MKPlacemark {

                    source =  MKMapItem(placemark: placemark)
                    println("\(source)")

                }

            } else {
                println("Problem with the data received from geocoder")
            }
        })


        CLGeocoder().reverseGeocodeLocation(dropOffDistanceLocation, completionHandler: {(placemarks,error)-> Void in

            if (error != nil) {
                println("Reverse geocoder failed with error" + error.localizedDescription)
                return
            }

            if placemarks.count > 0 {
                if let placemark: MKPlacemark = placemarks![0] as? MKPlacemark {

                    destination =  MKMapItem(placemark: placemark)
                    println("\(destination)")

                }



            } else {
                println("Problem with the data received from geocoder")
            }
        })

        let request:MKDirectionsRequest = MKDirectionsRequest()
        request.setSource(source)
        request.setDestination(destination)
        request.transportType = MKDirectionsTransportType.Automobile
        request.requestsAlternateRoutes = false

        let directions = MKDirections(request: request)
        directions.calculateDirectionsWithCompletionHandler ({
            (response: MKDirectionsResponse?, error: NSError?) in

            if error == nil {

                self.showRoute(response!)
            }
        })
     }

这是在 map 中添加路线覆盖的方法

 func showRoute(response:MKDirectionsResponse){
        for route in response.routes as! [MKRoute]{

            mapView.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)

        }

    }

I get this error as the response returned error: 400

打印错误显示为

Optional("The operation couldn’t be completed. (NSURLErrorDomain error -1011.)")

最佳答案

实际上源变量和目标变量都为零。所以我从服务器得到了错误的响应。如果您需要,只需尝试下面的代码

func addRoutesOverLayForMapView(){

    var source:MKMapItem?
    var destination:MKMapItem?
    var sourcePlacemark = MKPlacemark(coordinate: pickUpDistanceLocation!.coordinate, addressDictionary: nil)
    source = MKMapItem(placemark: sourcePlacemark)

    var desitnationPlacemark = MKPlacemark(coordinate: dropOffDistanceLocation!.coordinate, addressDictionary: nil)
    destination = MKMapItem(placemark: desitnationPlacemark)
    let request:MKDirectionsRequest = MKDirectionsRequest()
    request.setSource(source)
    request.setDestination(destination)
    request.transportType = MKDirectionsTransportType.Walking

    let directions = MKDirections(request: request)
    directions.calculateDirectionsWithCompletionHandler ({
        (response: MKDirectionsResponse?, error: NSError?) in

        if error == nil {

            self.showRoute(response!)
        }
        else{

        println("trace the error \(error?.localizedDescription)")

        }
    })
 }

func showRoute(response:MKDirectionsResponse){
    for route in response.routes as! [MKRoute]{
        mapView.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)
        var routeSeconds = route.expectedTravelTime
        let routeDistance = route.distance
        println("distance between two points is \(routeSeconds) and \(routeDistance)")


    }

}

你应该实现这个委托(delegate)方法,不要忘记设置 mapview 委托(delegate)

  func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {

        if overlay is MKPolyline {
            var polylineRenderer = MKPolylineRenderer(overlay: overlay)
            polylineRenderer.lineDashPattern = [14,10,6,10,4,10]
            polylineRenderer.strokeColor = UIColor(red: 0.012, green: 0.012, blue: 0.012, alpha: 1.00)
            polylineRenderer.lineWidth = 2.5
            return polylineRenderer
        }
        return nil

    }

关于ios - 路线未显示在 MKMapView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30775154/

相关文章:

swift - 如何检查用户的电子邮件是否已验证?

iOS 应用未收到 Firebase 通知

ios - 在 iOS 中生成 SHA256

ios - 在不禁用 subview 的情况下禁用 UITextField 的编辑

iOS自动转换调用的号码

ruby-on-rails-3 - 从 URL 隐藏的 Rails 3.2.12 Controller 操作

ruby-on-rails - 为什么 rails_admin 路由只适用于 link_to 而不是 'render'

asp.net-mvc - 如果经过身份验证,MVC 中的默认页面会有所不同

iphone - 使用 dequeueReusableCellWithIdentifier 会导致用作重复背景的图像泄漏出单元格

ios - Swift - 关于导入包与类