Search code examples
swiftmapkitpolygonmkpolygon

Swift MapKit - create a polygon


I am trying to create a polygon on my map to show a boundry. But when I run my app nothing appears. I can see the map but not the polygon What's my error? thanks.

import UIKit    
import MapKit    
import CoreLocation

class MapScreen : UIViewController {

    @IBOutlet var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        mapView.delegate = self as! MKMapViewDelegate

        // center the location on the screen
        var location = CLLocationCoordinate2DMake(41.308792, -72.928641)
        var span = MKCoordinateSpanMake(0.01, 0.01)
        var region = MKCoordinateRegionMake(location, span)
        mapView.setRegion(region, animated: true)

        //calling the method
        addBoundry()


    }

    func addBoundry(){ //creation of a polygon

        var points = [CLLocationCoordinate2DMake(41.311674, -72.925506),
                      CLLocationCoordinate2DMake(41.307308, -72.928694),
                      CLLocationCoordinate2DMake(41.307108, -72.928324),
                      CLLocationCoordinate2DMake(41.307892, -72.930285),
                      CLLocationCoordinate2DMake(41.307892, -72.931223),
                      CLLocationCoordinate2DMake(41.307227, -72.932494),
                      CLLocationCoordinate2DMake(41.308452, -72.931663),
                      CLLocationCoordinate2DMake(41.308730, -72.932773),
                      CLLocationCoordinate2DMake(41.308496, -72.931614),
                      CLLocationCoordinate2DMake(41.308496, -72.931614),
                      CLLocationCoordinate2DMake(41.311288, -72.931630),
                      CLLocationCoordinate2DMake(41.311659, -72.930945),
                      CLLocationCoordinate2DMake(41.312893, -72.932153),
                      CLLocationCoordinate2DMake(41.313433, -72.930542),
                      CLLocationCoordinate2DMake(41.313324, -72.929963),
                      CLLocationCoordinate2DMake(41.312758, -72.929027),
                      CLLocationCoordinate2DMake(41.312373, -72.927167),
                      CLLocationCoordinate2DMake(41.311674, -72.925506)]

        let polygon = MKPolygon(coordinates: &points, count: points.count)

        mapView.add(polygon)
    }

    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        if overlay is MKPolygon {
            let polygonView = MKPolygonRenderer(overlay: overlay)
            polygonView.fillColor = UIColor(red: 0, green: 0.847, blue: 1, alpha: 0.25)

            return polygonView
        }
        return nil    
    }
}

Solution

  • Your signature for mapView(_:rendererFor:) is incorrect. In Swift 3, it would be:

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        ...
    }
    

    If you formally declare your conformance to protocols like MKMapViewDelegate, it will often warn you about these things. For example, best practice would be do so in a class extension rather than in the class definition itself:

    extension MapScreen: MKMapViewDelegate {
    
        func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
            ...
        }
    
    }
    

    By the way, if you do that, not only do you get warnings about protocol conformance, but you also don't need to cast it when setting the delegate:

    mapView.delegate = self