路线未显示在 MKMapView 中? [英] Route not showing in MKMapView?

查看:19
本文介绍了路线未显示在 MKMapView 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 func addRoutesOverLayForMapView(){var 来源:MKMapItem?var 目的地:MKMapItem?println("(pickUpDistanceLocation)")println("(dropOffDistanceLocation)")//我也用这些位置进行了测试//让sourcelocation = CLLocation(纬度:40.7141667,经度:-74.0063889)//让目的地位置 = CLLocation(纬度:38.89,经度:77.03)CLGeocoder().reverseGeocodeLocation(pickUpDistanceLocation, completionHandler: {(placemarks,error)-> Void in如果(错误!= nil){println("反向地理编码失败,错误" + error.localizedDescription)返回}如果 placemarks.count >0 {if let placemark: MKPlacemark = placemarks![0] as?MK地标{源 = MKMapItem(地标:地标)println("(来源)")}} 别的 {println("从地理编码器接收到的数据有问题")}})CLGeocoder().reverseGeocodeLocation(dropOffDistanceLocation, completionHandler: {(placemarks,error)-> Void in如果(错误!= nil){println("反向地理编码失败,错误" + error.localizedDescription)返回}如果 placemarks.count >0 {if let placemark: MKPlacemark = placemarks![0] as?MK地标{目的地 = MKMapItem(地标:地标)println("(目的地)")}} 别的 {println("从地理编码器接收到的数据有问题")}})让请求:MKDirectionsRequest = MKDirectionsRequest()request.setSource(来源)request.setDestination(目的地)request.transportType = MKDirectionsTransportType.Automobilerequest.requestsAlternateRoutes = false让方向 = MKDirections(请求:请求)Directions.calculateDirectionsWithCompletionHandler ({(响应:MKDirectionsResponse?,错误:NSError?)在如果错误 == 零 {self.showRoute(响应!)}})}

这是在地图中添加路线叠加的方法

 func showRoute(response:MKDirectionsResponse){用于 response.routes as 中的路由![MKRoute]{mapView.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)}}

<块引用>

我收到此错误,因为响应返回错误:400

打印错误显示为

<块引用>

Optional("操作无法完成.(NSURLErrorDomain错误 -1011.)")

解决方案

实际上源变量和目标变量都是 nil.. 所以我从服务器得到了不好的响应.如果你需要试试下面的代码

func addRoutesOverLayForMapView(){var 来源:MKMapItem?var 目的地:MKMapItem?var sourcePlacemark = MKPlacemark(坐标:pickUpDistanceLocation!.coordinate,addressDictionary:nil)源 = MKMapItem(地标:源地标)var desitnationPlacemark = MKPlacemark(coordinate: dropOffDistanceLocation!.coordinate, addressDictionary: nil)目的地 = MKMapItem(地标:目的地地标)让请求:MKDirectionsRequest = MKDirectionsRequest()request.setSource(来源)request.setDestination(目的地)request.transportType = MKDirectionsTransportType.Walking让方向 = MKDirections(请求:请求)Directions.calculateDirectionsWithCompletionHandler ({(响应:MKDirectionsResponse?,错误:NSError?)在如果错误 == 零 {self.showRoute(响应!)}别的{println("跟踪错误(error?.localizedDescription)")}})}func showRoute(response:MKDirectionsResponse){用于 response.routes as 中的路由![MKRoute]{mapView.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)var routeSeconds = route.expectedTravelTime让 routeDistance = route.distanceprintln("两点之间的距离是(routeSeconds) 和(routeDistance)")}}

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

 func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) ->MKOverlayRenderer!{如果叠加是 MKPolyline {var polylineRenderer = MKPolylineRenderer(叠加:叠加)polylineRenderer.lineDashPattern = [14,10,6,10,4,10]polylineRenderer.strokeColor = UIColor(红色:0.012,绿色:0.012,蓝色:0.012,alpha:1.00)polylineRenderer.lineWidth = 2.5返回 polylineRenderer}返回零}

I have these method that adds routes for MKMapView between two CLLocation. i have both valid 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!)
            }
        })
     }

This is the method that adds the route overlay in the 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

On printing error it shows as

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

解决方案

Actually both source and destination variables were nil.. So i got bad response from the server.If you need just try the below code

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)")


    }

}

And you should implement this delegate method,dont forget to set the 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

    }

这篇关于路线未显示在 MKMapView 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆