How to use MKPolylineView in Swift
Asked Answered
B

1

11

I want to draw polyline in my Swift app.

Swift code

class MapViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet var theMapView: MKMapView

    override func viewDidLoad() {
        super.viewDidLoad()
        setMapView()
    }

    func setMapView() {
        //theMapView.zoomEnabled = false
        //theMapView.scrollEnabled = false 
        theMapView.rotateEnabled = false  

        // 
        var lat: CLLocationDegrees = 37.586601
        var lng: CLLocationDegrees = 127.009381

        // 
        var latDelta: CLLocationDegrees = 0.008
        var lngDelta: CLLocationDegrees = 0.008

        var theSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lngDelta)

        var initLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, lng)
        var theRegion: MKCoordinateRegion = MKCoordinateRegionMake(initLocation, theSpan)
        self.theMapView.setRegion(theRegion, animated: true)

        var locations = [CLLocation(latitude: 37.582691, longitude: 127.011186), CLLocation(latitude: 37.586112,longitude: 127.011047), CLLocation(latitude: 37.588212, longitude: 127.010438)]
        var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate})
        var polyline = MKPolyline(coordinates: &coordinates, count: locations.count)

        var myPolylineView : MKPolylineView
        /* error */
        myPolylineView.polyline = polyline // #1
        myPolylineView.strokeColor = UIColor.blueColor() // #2
        myPolylineView.lineWidth = 5; // #3
        self.theMapView.addOverlay(myPolylineView) // #4
        /* ----- */    
    }
}

Errors:

// #1 <br>
Cannot assign to 'polyline' in 'myPolylineView' <br>
// #2 <br>
'strokeColor' is unvailable: APIs deprecated as iOS 7 and earlier are unavailable in Swift <br>
// #3 <br>
'lineWidth' is unvailable: APIs deprecated as iOS 7 and earlier are unavailable in Swift <br>
// #4 <br>
Missing argument for parameter 'level' in call <br>

I cannot find a solution to this.

Barroom answered 17/7, 2014 at 8:40 Comment(0)
A
21

First, instead of MKPolylineView, you must create an MKPolylineRenderer.

MKPolylineView has been deprecated since iOS 7 and, although you can still use it in Objective-C if necessary, it's not supported in Swift.

Second, you must create and return an MKPolylineRenderer in the rendererForOverlay delegate method (not pass it to addOverlay).

In the addOverlay method, you pass the MKPolyline object (not the MKPolylineView or MKPolylineRenderer).

(See Adding MKOverlayPathRenderer as overlay to MKMapView gets exception for an explanation of the difference between what object you pass to addOverlay vs. what object you return in rendererForOverlay.)


So in the setMapView method, remove the lines that create and set myPolylineView and change the addOverlay line to:

self.theMapView.addOverlay(polyline)

Then implement the rendererForOverlay method:

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
    if overlay is MKPolyline {
        var polylineRenderer = MKPolylineRenderer(overlay: overlay)
        polylineRenderer.strokeColor = UIColor.blueColor()
        polylineRenderer.lineWidth = 5
        return polylineRenderer
    }

    return nil
}

Make sure the map view's delegate is set otherwise the delegate method won't get called and the overlay will not appear. If theMapView is an IBOutlet, connect the delegate outlet or set it in code (eg. in viewDidLoad after calling super):

self.theMapView.delegate = self
Armorial answered 17/7, 2014 at 18:44 Comment(2)
@VanDuTran, make sure of the following: 1) Map view outlet is connected, 2) Map view delegate is connected/set, 3) rendererForOverlay method is spelled correctly. If still not called, please ask a new Question with your details.Armorial
It's because my geojson has some weird coordinates: "coordinates":[[294636.0501,5039009.536],[294572.3566,5039049.88]]Trask

© 2022 - 2024 — McMap. All rights reserved.