Calculate Area of Mkpolygon in an Mkmapview

MKPolygon area calculation

The final step for i = N-1 and i+1 = 0 (wrap around) is missing in your loop.

MKpolygon based on tap coordinates

1) Get a coordinate (longitude, latitude) from a tap
Get the coordinates of a point from mkmapview on iphone

2) Create a moveable object on that coordinate - Create draggable MKAnnotation

3) Use the object's coordinates to create a polygon - Draw MKPloygon with annotation as center coordinate.

Create Square Overlay that covers current region in MKMapView

You can use the centerCoordinate and region properties of MKMapView and then create a MKPolygon overlay by extracting the four corners, as in the code below:

    vertex[0]=CLLocationCoordinate2DMake(map.centerCoordinate.latitude+map.region.span.latitudeDelta/2.,map.centerCoordinate.longitude-map.region.span.longitudeDelta/2.);
vertex[1]=CLLocationCoordinate2DMake(map.centerCoordinate.latitude+map.region.span.latitudeDelta/2.,map.centerCoordinate.longitude+map.region.span.longitudeDelta/2.);
vertex[2]=CLLocationCoordinate2DMake(map.centerCoordinate.latitude-map.region.span.latitudeDelta/2.,map.centerCoordinate.longitude+map.region.span.longitudeDelta/2.);
vertex[3]=CLLocationCoordinate2DMake(map.centerCoordinate.latitude-map.region.span.latitudeDelta/2.,map.centerCoordinate.longitude-map.region.span.longitudeDelta/2.);
MKPolygon *square = [MKPolygon polygonWithCoordinates:vertex count:4];

Then you add the polygon as an overlay:

[map addOverlay:square]

Finally in your mapView:rendererForOverlay: you define your square rendered based on the polygon overlay:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay {  
if([overlay isKindOfClass:[MKPolygon class]]) {
MKPolygonRenderer *renderer = [[MKPolygonRenderer alloc] initWithPolygon:(MKPolygon *)overlay];
renderer.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.25];
return renderer;
} else {
return nil;
}
}

How to get area for MKPolygon in iOS

I figured this out by doing a little loop through the points in the polygon. For every 3 points, I check if the center of that triangle is in the polygon. If it is continue, if not, connect the polygon so that there are no dips in the polygon. Once done, get the triangles in the polygon and do the math to get the area. Then subtract the triangles that were removed.

Hope this helps someone.



Related Topics



Leave a reply



Submit