How to get area for MKPolygon in iOS - ios

How to you get the area of a MKPolygon or MKOverlay in iOS?
I have been able to breakup the Polygon into triangles and do some math to get the area. But, doesn't work well with irregular polygons.
I was thinking about doing something like the "A more complex case" here: http://www.mathopenref.com/coordpolygonarea2.html
I was hoping there is a simpler solution with MapKit.
Thanks,
Tim

Here's the implementation I'm using.
#define kEarthRadius 6378137
#implementation MKPolygon (AreaCalculation)
- (double) area {
double area = 0;
NSMutableArray *coords = [[self coordinates] mutableCopy];
[coords addObject:[coords firstObject]];
if (coords.count > 2) {
CLLocationCoordinate2D p1, p2;
for (int i = 0; i < coords.count - 1; i++) {
p1 = [coords[i] MKCoordinateValue];
p2 = [coords[i + 1] MKCoordinateValue];
area += degreesToRadians(p2.longitude - p1.longitude) * (2 + sinf(degreesToRadians(p1.latitude)) + sinf(degreesToRadians(p2.latitude)));
}
area = - (area * kEarthRadius * kEarthRadius / 2);
}
return area;
}
- (NSArray *)coordinates {
NSMutableArray *points = [NSMutableArray arrayWithCapacity:self.pointCount];
for (int i = 0; i < self.pointCount; i++) {
MKMapPoint *point = &self.points[i];
[points addObject:[NSValue valueWithMKCoordinate:MKCoordinateForMapPoint(* point)]];
}
return points.copy;
}
double degreesToRadians(double radius) {
return radius * M_PI / 180;
}
In Swift 3:
let kEarthRadius = 6378137.0
extension MKPolygon {
func degreesToRadians(_ radius: Double) -> Double {
return radius * .pi / 180.0
}
func area() -> Double {
var area: Double = 0
var coords = self.coordinates()
coords.append(coords.first!)
if (coords.count > 2) {
var p1: CLLocationCoordinate2D, p2: CLLocationCoordinate2D
for i in 0..<coords.count-1 {
p1 = coords[i]
p2 = coords[i+1]
area += degreesToRadians(p2.longitude - p1.longitude) * (2 + sin(degreesToRadians(p1.latitude)) + sin(degreesToRadians(p2.latitude)))
}
area = abs(area * kEarthRadius * kEarthRadius / 2)
}
return area
}
func coordinates() -> [CLLocationCoordinate2D] {
var points: [CLLocationCoordinate2D] = []
for i in 0..<self.pointCount {
let point = self.points()[i]
points.append(MKCoordinateForMapPoint(point))
}
return Array(points)
}
}

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

Find closest point on line from a particular point

I'm using Google Maps iOS to set up Geofencing around a building complex. I've created a polyline around the complex and if the user taps outside of the polyline it will move the marker to the closest point that's on the polyline, otherwise it will just place the marker. This seems to work relatively well using this method.
However I've noticed that this method only seems to work when the point in question is perpendicular to a point on the line, otherwise it comes up with strange results. I've posted my code and some screenshots below.
-(CLLocationCoordinate2D) findClosestPointWithinFence:(CLLocationCoordinate2D) pointToTest {
CLLocationDistance smallestDistance = 0;
CLLocationCoordinate2D closestPoint = pointToTest;
for(int i = 0; i < [geoFencePoints count] - 1; i++) {
CGPoint point = [[geoFencePoints objectAtIndex:i] CGPointValue];
CGPoint point2 = [[geoFencePoints objectAtIndex:i + 1] CGPointValue];
CLLocationCoordinate2D locationA = CLLocationCoordinate2DMake(point.x, point.y);
CLLocationCoordinate2D locationB = CLLocationCoordinate2DMake(point2.x, point2.y);
CLLocationCoordinate2D myLoc = [self findClosestPointOnLine:locationA secondPoint:locationB fromPoint:pointToTest];
if(GMSGeometryIsLocationOnPath(myLoc, dealershipParameters.path, YES)) {
if(smallestDistance == 0) {
smallestDistance = GMSGeometryDistance(myLoc, pointToTest);
closestPoint = myLoc;
} else {
if(smallestDistance > GMSGeometryDistance(myLoc, pointToTest)) {
smallestDistance = GMSGeometryDistance(myLoc, pointToTest);
closestPoint = myLoc;
}
}
}
}
return closestPoint;
}
-(CLLocationCoordinate2D) findClosestPointOnLine:(CLLocationCoordinate2D)locationA secondPoint:(CLLocationCoordinate2D)locationB fromPoint:(CLLocationCoordinate2D) pointToTest {
CGPoint aToP = CGPointMake(pointToTest.latitude - locationA.latitude, pointToTest.longitude - locationA.longitude);
CGPoint aToB = CGPointMake(locationB.latitude - locationA.latitude, locationB.longitude - locationA.longitude);
float atb2 = (aToB.x * aToB.x) + (aToB.y * aToB.y);
float atp_dot_atb = (aToP.x * aToB.x) + (aToP.y * aToB.y);
float t = atp_dot_atb / atb2;
CLLocationCoordinate2D myLoc = CLLocationCoordinate2DMake(locationA.latitude + aToB.x * t, locationA.longitude + aToB.y * t);
return myLoc;
}
-(BOOL)testIfInsideGeoFence:(CLLocationCoordinate2D) pointToTest {
return GMSGeometryContainsLocation(pointToTest, dealershipParameters.path, YES) || GMSGeometryIsLocationOnPath(pointToTest, dealershipParameters.path, YES);
}
Below the first screenshot shows the marker successfully finding the closest point, the marker off the blue line is where I initially tapped, and the marker on the blue line is the point it found. The second shows the marker failing to find the closest point. The marker on the screen is where I initially tapped, since it is unable to find a proper solution it doesn't place a second marker.
Screenshot 1
Screenshot 2
I ran into a similar issue. I think what is happening is that you are treating the line segment as a line. Since the segment does not extend to a point that would be perpendicular to the point, the closest point on the segment would be one of it endpoints, not an extension of the segment.
Here is a method I am using. It takes the endpoint of the segment and returns a struct containing the nearest point on the segment and the distance from the giving point. The key difference being the if-else statements that check whether the solution is on the segment or not. You may need to rework a few things for your purposes.
The other thing to note is that I have had more accurate results performing the math on MKMapPoints rather than CLLocationCoordinate2D objects. I think it has something to do with the earth being round or some such nonsense.
+ (struct TGShortestDistanceAndNearestCoordinate)distanceFromPoint:(CLLocationCoordinate2D)p
toLineSegmentBetween:(CLLocationCoordinate2D)l1
and:(CLLocationCoordinate2D)l2 {
return [[self class] distanceFromMapPoint:MKMapPointForCoordinate(p)
toLineSegmentBetween:MKMapPointForCoordinate(l1)
and:MKMapPointForCoordinate(l2)];
}
+ (struct TGShortestDistanceAndNearestCoordinate)distanceFromMapPoint:(MKMapPoint)p
toLineSegmentBetween:(MKMapPoint)l1
and:(MKMapPoint)l2 {
double A = p.x - l1.x;
double B = p.y - l1.y;
double C = l2.x - l1.x;
double D = l2.y - l1.y;
double dot = A * C + B * D;
double len_sq = C * C + D * D;
double param = dot / len_sq;
double xx, yy;
if (param < 0 || (l1.x == l2.x && l1.y == l2.y)) {
xx = l1.x;
yy = l1.y;
}
else if (param > 1) {
xx = l2.x;
yy = l2.y;
}
else {
xx = l1.x + param * C;
yy = l1.y + param * D;
}
struct TGShortestDistanceAndNearestCoordinate result;
MKMapPoint nearestPoint = MKMapPointMake(xx, yy);
result.shortestDistance = MKMetersBetweenMapPoints(p, nearestPoint);
result.nearestCoordinate = MKCoordinateForMapPoint(nearestPoint);
return result;
}
A very elegant solution. But I'm not sure about your test in the line "if param < 0 ... ". l1.x == l2.x iff the segment is vertical, and l1.y == l2.y iff it is horizontal. So how can this conjunction ever be true? (except when l1, l2 are identical)

Find the precise centroid (as an MKMapPoint) of an MKPolygon

This means excluding the area(s) of any interiorPolygons.
Once one has the centroid of the outer points polygon, how does one (i.e., in the form of an Objective-C example) adjust the centroid by the subtractive interiorPolygons? Or is there a more elegant way to compute the centroid in one go?
If you help get the code working, it will be open sourced (WIP here).
Might be helpful:
http://www.ecourses.ou.edu/cgi-bin/eBook.cgi?topic=st&chap_sec=07.2&page=case_sol
https://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
Thinking about it today, it makes qualitative sense that adding each interior centroid weighted by area to the exterior centroid would arrive at something sensible. (A square with an interior polygon (hole) on the left side would displace the centroid right, directly proportional to the area of the hole.)
Not to scale:
- (MKMapPoint)calculateCentroid
{
switch (self.pointCount) {
case 0: return MKMapPointMake(0.0,
0.0);
case 1: return MKMapPointMake(self.points[0].x,
self.points[0].y);
case 2: return MKMapPointMake((self.points[0].x + self.points[1].x) / 2.0,
(self.points[0].y + self.points[1].y) / 2.0);
}
// onward implies pointCount >= 3
MKMapPoint centroid;
MKMapPoint *previousPoint = &(self.points[self.pointCount-1]); // for i=0, wrap around to the last point
for (NSUInteger i = 0; i < self.pointCount; ++i) {
MKMapPoint *point = &(self.points[i]);
double delta = (previousPoint->x * point->y) - (point->x * previousPoint->y); // x[i-1]*y[i] + x[i]*y[i-1]
centroid.x += (previousPoint->x + point->x) * delta; // (x[i-1] + x[i]) / delta
centroid.y += (previousPoint->y + point->y) * delta; // (y[i-1] + y[i]) / delta
previousPoint = point;
}
centroid.x /= 6.0 * self.area;
centroid.y /= 6.0 * self.area;
// interiorPolygons are holes (subtractive geometry model)
for (MKPolygon *interiorPoly in self.interiorPolygons) {
if (interiorPoly.area == 0.0) {
continue; // avoid div-by-zero
}
centroid.x += interiorPoly.centroid.x / interiorPoly.area;
centroid.y += interiorPoly.centroid.y / interiorPoly.area;
}
return centroid;
}
in Swift 5
private func centroidForCoordinates(_ coords: [CLLocationCoordinate2D]) -> CLLocationCoordinate2D? {
guard let firstCoordinate = coordinates.first else {
return nil
}
guard coords.count > 1 else {
return firstCoordinate
}
var minX = firstCoordinate.longitude
var maxX = firstCoordinate.longitude
var minY = firstCoordinate.latitude
var maxY = firstCoordinate.latitude
for i in 1..<coords.count {
let current = coords[i]
if minX > current.longitude {
minX = current.longitude
} else if maxX < current.longitude {
maxX = current.longitude
} else if minY > current.latitude {
minY = current.latitude
} else if maxY < current.latitude {
maxY = current.latitude
}
}
let centerX = minX + ((maxX - minX) / 2)
let centerY = minY + ((maxY - minY) / 2)
return CLLocationCoordinate2D(latitude: centerY, longitude: centerX)
}

Best way to calculate the nearest point from another point?

I have four point say _pointA _pointB _pointC _pointD. I want to find the nearest point from the current point. I had something like this but it sometimes gives wrong reasult.
Problem : When i near to pointA it gives pointC
CGPoint neastPoint=CGPointZero;
for (int i=0; i<3; i++) {
CGFloat x;
CGFloat y;
CGFloat currntDistance;
if (i==0){
x=(pointA.x-currentPoint.x) *(pointA.x-currentPoint.x);
y=(pointA.y-currentPoint.y)*(pointA.y-currentPoint.y);
currntDistance =sqrtf(x+y);
distance=currntDistance;
neastPoint=pointA;
}
else if (i==1){
x=(pointB.x-currentPoint.x) *(pointB.x-currentPoint.x);
y=(pointB.y-currentPoint.y)*(pointB.y-currentPoint.y);
currntDistance =sqrtf(x+y);
if (distance>currntDistance) {
distance=currntDistance;
neastPoint=pointB;
}
}
else if (i==2){
x=(pointC.x-currentPoint.x) *(pointC.x-currentPoint.x);
y=(pointC.y-currentPoint.y)*(pointC.y-currentPoint.y);
currntDistance =sqrtf(x+y);
if (distance>currntDistance) {
distance=currntDistance;
neastPoint=pointC;
}
}
else {
x=(pointD.x-currentPoint.x) *(pointD.x-currentPoint.x);
y=(pointD.y-currentPoint.y)*(pointD.y-currentPoint.y);
currntDistance =sqrtf(x+y);
if (distance>currntDistance) {
distance=currntDistance;
neastPoint=pointD;
}
}
CurrentPoint : {44, 33.140846}
Point A : {71, 178}
Point B : {134, 178}
Point C : {133, 71}
Point D : {75, 67}
Nearast Point : {133, 71}
With the points that you have given, pointD is the closest to currentPoint.
It is not found by your code because
for (int i=0; i<3; i++) {
should be
for (int i=0; i<4; i++) {
to check all 4 points. As others have noticed, you can simplify your code,
for example
CGFloat distance = FLT_MAX; // start with some large value
CGPoint nearestPoint;
CGPoint points[] = { pointA, pointB, pointC, pointD };
for (int i = 0; i < 4; i++) {
CGFloat currentDistance = hypotf(points[i].x - currentPoint.x, points[i].y - currentPoint.y);
if (currentDistance < distance) {
distance = currentDistance;
nearestPoint = points[i];
}
}
Pseudo :
Point[] allPoints = {poinA,pontB,pointC,....,pointN}
int distance = Int.MAX_VALUE;
Point nearestPoint = null;
for(int i = 0 ; i < allPoints.count;i++){
if(pointA != allPoints[i]){
int currentDistance = getDistance(pointA,allPoints[i]);
if(currentDistance < distance){
distance = currentDistance;
nearestPoint = allPoints[i];
}
}
}
print("Nearest point is " + nearestPoint);

How to calculate the center coordinates of CLLocationDistance

I want calculate the center point between my location and some annotation. So far I have done this:
CLLocation *myLoc = self.locMgr.location;
MKPointAnnotation *middleAnnotation = [locationV.annotations objectAtIndex:locationV.annotations.count/2];
CLLocation *someStuiodLoc = [[CLLocation alloc] initWithLatitude:middleAnnotation.coordinate.latitude longitude:middleAnnotation.coordinate.longitude];
CLLocationDistance dist = [myLoc distanceFromLocation:someStuiodLoc];
How can I calculate the center point/cordinate of "dist" ??
#define ToRadian(x) ((x) * M_PI/180)
#define ToDegrees(x) ((x) * 180/M_PI)
+ (CLLocationCoordinate2D)midpointBetweenCoordinate:(CLLocationCoordinate2D)c1 andCoordinate:(CLLocationCoordinate2D)c2
{
c1.latitude = ToRadian(c1.latitude);
c2.latitude = ToRadian(c2.latitude);
CLLocationDegrees dLon = ToRadian(c2.longitude - c1.longitude);
CLLocationDegrees bx = cos(c2.latitude) * cos(dLon);
CLLocationDegrees by = cos(c2.latitude) * sin(dLon);
CLLocationDegrees latitude = atan2(sin(c1.latitude) + sin(c2.latitude), sqrt((cos(c1.latitude) + bx) * (cos(c1.latitude) + bx) + by*by));
CLLocationDegrees longitude = ToRadian(c1.longitude) + atan2(by, cos(c1.latitude) + bx);
CLLocationCoordinate2D midpointCoordinate;
midpointCoordinate.longitude = ToDegrees(longitude);
midpointCoordinate.latitude = ToDegrees(latitude);
return midpointCoordinate;
}
I have written library function in Swift to calculate the midpoint between multiple coordinates as following:
// /** Degrees to Radian **/
class func degreeToRadian(angle:CLLocationDegrees) -> CGFloat{
return ( (CGFloat(angle)) / 180.0 * CGFloat(M_PI) )
}
// /** Radians to Degrees **/
class func radianToDegree(radian:CGFloat) -> CLLocationDegrees{
return CLLocationDegrees( radian * CGFloat(180.0 / M_PI) )
}
class func middlePointOfListMarkers(listCoords: [CLLocationCoordinate2D]) -> CLLocationCoordinate2D{
var x = 0.0 as CGFloat
var y = 0.0 as CGFloat
var z = 0.0 as CGFloat
for coordinate in listCoords{
var lat:CGFloat = degreeToRadian(coordinate.latitude)
var lon:CGFloat = degreeToRadian(coordinate.longitude)
x = x + cos(lat) * cos(lon)
y = y + cos(lat) * sin(lon);
z = z + sin(lat);
}
x = x/CGFloat(listCoords.count)
y = y/CGFloat(listCoords.count)
z = z/CGFloat(listCoords.count)
var resultLon: CGFloat = atan2(y, x)
var resultHyp: CGFloat = sqrt(x*x+y*y)
var resultLat:CGFloat = atan2(z, resultHyp)
var newLat = radianToDegree(resultLat)
var newLon = radianToDegree(resultLon)
var result:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: newLat, longitude: newLon)
return result
}
Detailed answer can be found here
U can calculate the midPoint of two coordinates using the mid point formula (http://www.purplemath.com/modules/midpoint.htm) which gives a close approximation to the actual geographical point if the distances are lesser than 500 miles. If you consider that the earth is spherical, then a more complex treatment of the points would be involved.

Adding random MKAnnotations around Current Location in a MKMapView

I have an MKMapView that shows the current users location. When I click a button in the navigation bar, I want to randomly drop 10 MKAnnotation pins. These can be dropped anywhere, randomly, but only within the current visible map area and around the current location.
How would go about doing something like this? Is there a way to have a long/lat range that is around the users location, but within the map area? Then, I could randomly choose from this range?
Basically, I need to find what coordinates are available in the current MKCoordinateRegion.
Is there a better way to go about this?
You can get your annotations using the distance between user location and other location
check out below code
#define DISTANCE_RADIUS 10.0 // in KM
-(NSArray *)findNearMe:(NSArray *)lounges {
NSMutableArray *arNearme = [NSMutableArray array];
CLLocation *currentLocation;
for(NSDictionary *d in lounges)
{
currentLocation = [[CLLocation alloc] initWithLatitude:29.33891 longitude:48.077202];
CGFloat latitude=[[d valueForKey:#"latitude"] floatValue];
CGFloat longitude=[[d valueForKey:#"longitude"] floatValue];
CLLocation *newPinLocation=[[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
double distanceValue=[currentLocation distanceFromLocation:newPinLocation];
if(distanceValue/1000.0<=DISTANCE_RADIUS) {
[arNearme addObject:d];
}
}
return arNearme;
}
It will returns you an array of range of 1 to 1000 km near user location.
Used the annotations array and show your annotations in to map view with user location.
I figured it out, here is how I did it:
/**
* Adds new pins to the map
*
* #version $Revision: 0.1
*/
+ (void)addPinsToMap:(MKMapView *)mapView amount:(int)howMany {
//First we need to calculate the corners of the map so we get the points
CGPoint nePoint = CGPointMake(mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake((mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));
//Then transform those point into lat,lng values
CLLocationCoordinate2D neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];
CLLocationCoordinate2D swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];
// Loop
for (int y = 0; y < howMany; y++) {
double latRange = [MapUtility randomFloatBetween:neCoord.latitude andBig:swCoord.latitude];
double longRange = [MapUtility randomFloatBetween:neCoord.longitude andBig:swCoord.longitude];
// Add new waypoint to map
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(latRange, longRange);
MPin *pin = [[MPin alloc] init];
pin.coordinate = location;
[mapView addAnnotation:pin];
}//end
}//end
/**
* Random numbers
*
* #version $Revision: 0.1
*/
+ (double)randomFloatBetween:(double)smallNumber andBig:(double)bigNumber {
double diff = bigNumber - smallNumber;
return (((double) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + smallNumber;
}//end
In Swift 3.0
func addPins() {
let nePoint = CGPoint(x: mapView.bounds.origin.x + mapView.bounds.size.width, y: mapView.bounds.origin.y)
let sePoint = CGPoint(x: mapView.bounds.origin.x, y: mapView.bounds.origin.y + mapView.bounds.size.height)
let neCoord = mapView.convert(nePoint, toCoordinateFrom: mapView)
let seCoord = mapView.convert(sePoint, toCoordinateFrom: mapView)
var y = 5
while y > 0 {
let latRange = randomBetweenNumbers(firstNum: Float(neCoord.latitude), secondNum: Float(seCoord.latitude))
let longRange = randomBetweenNumbers(firstNum: Float(neCoord.longitude), secondNum: Float(seCoord.longitude))
let location = CLLocationCoordinate2D(latitude: CLLocationDegrees(latRange), longitude: CLLocationDegrees(longRange))
let pin = MKPointAnnotation()
pin.coordinate = location
pin.title = "Home"
mapView.addAnnotation(pin)
y -= 1
}
}
func randomBetweenNumbers(firstNum: Float, secondNum: Float) -> Float{
return Float(arc4random()) / Float(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum)
}
swift 4:
// returned coordinates all restricted to the provided bound
// nwCoordinate: north West coordinate of the target bound
// seCoordinate: south east coordinate of the target bound
func createRandomLocation(nwCoordinate: CLLocationCoordinate2D, seCoordinate: CLLocationCoordinate2D) -> [CLLocationCoordinate2D]
{
return (0 ... 30).enumerated().map { _ in
let latitude = randomFloatBetween(nwCoordinate.latitude, andBig: seCoordinate.latitude)
let longitude = randomFloatBetween(nwCoordinate.longitude, andBig: seCoordinate.longitude)
return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
}
private func randomFloatBetween(_ smallNumber: Double, andBig bigNumber: Double) -> Double {
let diff: Double = bigNumber - smallNumber
return ((Double(arc4random() % (UInt32(RAND_MAX) + 1)) / Double(RAND_MAX)) * diff) + smallNumber
}

Resources