3

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    
    }
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
l.b.dev
  • 151
  • 3
  • 10
  • You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. – herrbischoff Jul 11 '17 at 16:30

1 Answers1

4

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
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • this worked for me .. however can we further improve the map zoom so it adjusts to the size of the polygon? – Saurabh Nov 06 '19 at 07:26
  • Right, but i need to manually zoom in to that polygon, and in my scenario there are polygons of different shape and size (like area that can even cover the whole city or certain small places) that i need to display on map. So is there a way that i can center adjust the polygon automatically irrespective of shape and size? I have also tried to set static parameters for the zooming portion as per the smaller area, however that didnt help for bigger polygons and vice versa. Also had no luck trying 'setVisibleMapRect'. I posted this issue yesterday here https://stackoverflow.com/q/58707909/5255016 – Saurabh Nov 06 '19 at 09:10