Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

ios - Route not showing in MKMapView?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...