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

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

问题描述

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

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)

        }

    }




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

I get this error as the response returned error: 400

在打印错误时显示为


可选(操作无法执行完成。(NSURLErrorDomain
error -1011。))

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


    }

}

你应该实现这个委托方法,别忘了设置mapview委托

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天全站免登陆