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
How to Open the Parent App on iPhone from My Watchkit App
Po Swift String "Unresolved Identifier"
How to Set Uisegmentedcontrol Tint Color for Individual Segment
What Does <> (Angle Brackets) Do on Class Names in Swift
Why Does My Version of Filter Perform So Differently Than Swifts
How to Build a Workout App on Watchos with Audio Feedback
"Cannot Inherit from Non-Open Class" Swift
Difference Between Println and Print in Swift
Why Is Forwarding Variadic Parameters Invalid
Given a Hexadecimal String in Swift, Convert to Hex Value
Local Notification While App Not Running
Uisplitviewcontroller Displaymodebuttonitem()
Timedmetadata' Deprecated. Another Method? <Updated>
Lazy Readonly Property in Swift
Pause an Skaction in Spritekit with Swift
When Should I Use Optionals and When Should I Use Non-Optionals with Default Values