Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone MKMapView - MKPolygon Issues

I am trying to plot a MKPolygon on a MKMapView in iOS 4.0. I have an NSArray which contains custom objects that include properties for latitude/longitude. I have a code sample below:

- (void)viewDidLoad {
    [super viewDidLoad];
    dataController = [[DataController alloc] initWithMockData];
    coordinateData = [dataController getCordData];

    CLLocationCoordinate2D *coords = NULL;
    NSUInteger coordsLen = 0;

    /* How do we actually define an array of CLLocationCoordinate2d? */

    MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coords count:coordsLen];
    [mapView addOverlay: polygon];

}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolygonView *polygonView = [[MKPolygonView alloc] initWithPolygon: routePolygon]; 
    NSLog(@"Attempting to add Overlay View");   
    return polygonView;
}

The way I understand it is that:

  1. I need to create the MKPolygon
  2. Ddd an overlay to MapView
  3. This will turn will trigger the creation of the MKPolygonView.

My question is how do i take my custom object contained in NSArray (coordinateData) and convert these object into an array of CLLocationCoordinate2d so that the Polygon can interpret and render? I'm not sure how CLLocationCoordinate2d is even an array? Can someone shed some clarity on this.

like image 259
Evan Klein Avatar asked Mar 29 '11 14:03

Evan Klein


2 Answers

Code in Swift 4

For coordinates in json:

{
"coordinates": [
    [-73.947676,40.660297],
    [-73.947264,40.656437],
    [-73.947159,40.655594],
    [-73.946479,40.6491],
    [-73.947467,40.649039]
}

Read the coordinates:

let coordinates = json["coordinates"] as! [[Double]] 

Create points array:

var locationCoordinates = [CLLocationCoordinate2D]()    
for coordinate in coordinates{
   locationCoordinates.append(CLLocationCoordinate2DMake(coordinate.last!, coordinate.first!))
}

Create a polygon and add it to the map:

map.addOverlay(MKPolyline(coordinates: locationCoordinates, 
                                count: locationCoordinates.count))

Make sure your VC confronts to MKMapViewDelegate

class ViewController: UIViewController, MKMapViewDelegate { ... }

And add this method:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if overlay is MKPolygon {
        let polygonView = MKPolygonRenderer(overlay: overlay)
        polygonView.fillColor = .black
        polygonView.strokeColor = .red
        polygonView.lineWidth = 2.0

        return polygonView

    return MKOverlayRenderer()
}
like image 179
Gal Avatar answered Sep 30 '22 14:09

Gal


For iOS 7.0 and later we should use MKPolygonRenderer instead of MKPolygonView,

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
   MKPolygonRenderer * polygonView = [[MKPolygonRenderer alloc] initWithPolygon:overlay];
   polygonView.fillColor   = [UIColor greenColor];
   polygonView.strokeColor = [UIColor redColor] ;
   polygonView.lineWidth   = 1.0;
   return polygonView;
}
like image 23
Nazik Avatar answered Sep 30 '22 12:09

Nazik