iPhone MKMapView - MKPolygon Issues

耗尽温柔 提交于 2019-11-27 22:25:33

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

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

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.

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()
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!