iOS Swift获取两点之间的方向并绘制成地图

时间:2021-07-24 23:42:39

I want to get the directions between two points and draw them to my MKMapView. This is my implementation:

我想获取两点之间的方向并将它们绘制到我的MKMapView中。这是我的实现:

First, at my controller's viewDidLoad callback, I set map's view delegate to self

首先,在我的控制器的viewDidLoad回调中,我将map的视图委托设置为self

map.delegate = self

Then, I request all routes:

然后,我请求所有的路线:

func showDirections() {
        let currentPlace: MKPlacemark = MKPlacemark(coordinate: location.coordinate, addressDictionary: nil)
        let place: MKPlacemark = MKPlacemark(coordinate: annotation.coordinate, addressDictionary: nil)



    println(currentPlace.coordinate.latitude)
    println(currentPlace.coordinate.longitude)
    println(place.coordinate.latitude)
    println(place.coordinate.longitude)

    var request: MKDirectionsRequest = MKDirectionsRequest.new()
    request.setSource(MKMapItem(placemark: currentPlace))
    request.setDestination(MKMapItem(placemark: place))
    request.transportType = MKDirectionsTransportType.Any
    request.requestsAlternateRoutes = true
    var directions: MKDirections = MKDirections(request: request)
    directions.calculateDirectionsWithCompletionHandler() {
        (response, error) in
        println(error)
        if(error == nil && response != nil) {
            println("Numero de rutas: " + String(response.routes.count))
            println(response.routes[0].coordinate)
            for(var i = 0;i < response.routes.count; i++) {
                self.map.addOverlays(response.routes[i].overlays, level: MKOverlayLevel.AboveRoads)
                self.map.setNeedsDisplay()
            }
            for r in response.routes { println("route = \(r)") }
        }
    }

}

And implement this two callbacks too:

并实现这两个回调:

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        println("redenrerForOverlay")
        if(overlay.isKindOfClass(MKPolyline)) {
            var renderer: MKPolylineRenderer = MKPolylineRenderer(overlay: overlay)
            renderer.strokeColor = Colors.psnGreen
            renderer.lineWidth = 5
            return renderer
        }
        return nil
    }

func mapView(mapView: MKMapView!, viewForOverlay overlay: MKOverlay!) -> MKOverlayView! {
        println("ViewForOverlay")
        if (overlay.isKindOfClass(MKPolyline)) {
            var lineView: MKPolylineView = MKPolylineView(overlay: overlay)
            lineView.backgroundColor = Colors.psnGreen

            return lineView;
        }
        return nil;
    }

When i run my application, I get two routes to draw, but when I call map.addOverlay, nothing happens. My rendererForOverlay/viewForOverlay are never called. Somenone know how to make it work?

当我运行我的应用程序时,我有两条路径要绘制,但是当我调用map时。addOverlay,什么也不会发生。我的rendererForOverlay/viewForOverlay不会被调用。有人知道如何让它工作吗?

Thanks in advance

谢谢提前

1 个解决方案

#1


2  

I finnaly got the solution

我终于找到了解决的办法。

For those who are interested, only change the directions callback to this:

对于感兴趣的人,只更改回调方向:

directions.calculateDirectionsWithCompletionHandler() {
            (response, error) in
            if(error == nil && response != nil) {
                for route in response.routes {
                    var r: MKRoute = route as! MKRoute
                    self.map.addOverlay(r.polyline, level: MKOverlayLevel.AboveRoads)
                }
            }
        }

#1


2  

I finnaly got the solution

我终于找到了解决的办法。

For those who are interested, only change the directions callback to this:

对于感兴趣的人,只更改回调方向:

directions.calculateDirectionsWithCompletionHandler() {
            (response, error) in
            if(error == nil && response != nil) {
                for route in response.routes {
                    var r: MKRoute = route as! MKRoute
                    self.map.addOverlay(r.polyline, level: MKOverlayLevel.AboveRoads)
                }
            }
        }