iPhone MKMapView - MKPolygon Issues

2019-01-18 05:46发布

问题:

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.

回答1:

The polygonWithCoordinates method wants a C array of CLLocationCoordinate2D structs. You can use malloc to allocate memory for the array (and free to release the memory). Loop through your NSArray and set it each element in the struct array.

For example:

coordsLen = [coordinateData count];
CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * coordsLen);
for (int i=0; i < coordsLen; i++)
{
    YourCustomObj *coordObj = (YourCustomObj *)[coordinateData objectAtIndex:i];
    coords[i] = CLLocationCoordinate2DMake(coordObj.latitude, coordObj.longitude);
}
MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coords count:coordsLen];
free(coords);
[mapView addOverlay:polygon];

The viewForOverlay method should look like this:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolygonView *polygonView = [[[MKPolygonView alloc] initWithPolygon:overlay] autorelease]; 
    polygonView.lineWidth = 1.0;
    polygonView.strokeColor = [UIColor redColor];
    polygonView.fillColor = [UIColor greenColor];
    return polygonView;
}


回答2:

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;
}


回答3:

coordinatesArray; //Your array containing the coordinates

for (int i=0; i <[coordinatesArray count]; i++) 
{
   float latitude  = [coordinatesArray[i][@"latitude"] floatValue];
   float longitude  = [coordinatesArray[i][@"longitude"] floatValue];
   MKPolygon *polygon;
   CLLocationCoordinate2D coordinates[[coordinatesArray count]];
   coordinates[i] = CLLocationCoordinate2DMake(latitude , longitude);
   polygon = [MKPolygon polygonWithCoordinates:coordinates count:[coordinatesArray count]];
   [self.mapView addOverlay:polygon];
}

//Your "coordinatesArray" is an array containing the dictionary with multiple values of latitude and longitude keys. //Hope this helps you.