I have an algorithm that calculates the size and location of a bounding box for a map that contains a set of given coordinates. While it works perfectly, the bounds that it produces don't always accommodate the push pin that I place at coordinates that often lie right on the edge of the bounding box:
...and again, it produces an acceptable result most of the time:
I've mulled it over for awhile, but I haven't been able to think of a way to modify my algorithm to ensure that the push pins are always within the bounding box. My algorithm is listed below, and any suggestions would be greatly appreciated. :)
MKMapPoint *points = malloc([coordinates count] * sizeof(MKMapPoint));
MKMapPoint upperRightCorner;
MKMapPoint lowerLeftCorner;
for(int i = 0; i < [coordinates count]; i++)
{
CLLocation *location = [coordinates objectAtIndex:i];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(herp.coordinate.latitude, herp.coordinate.longitude);
MKMapPoint point = MKMapPointForCoordinate(coordinate);
points[i] = point;
if (i == 0) {
upperRightCorner = point;
lowerLeftCorner = point;
}
else {
if (point.x > upperRightCorner.x) upperRightCorner.x = point.x;
if (point.y > upperRightCorner.y) upperRightCorner.y = point.y;
if (point.x < lowerLeftCorner.x) lowerLeftCorner.x = point.x;
if (point.y < lowerLeftCorner.y) lowerLeftCorner.y = point.y;
}
}
MKMapRect boundingBox = MKMapRectMake(lowerLeftCorner.x, lowerLeftCorner.y,
upperRightCorner.x - lowerLeftCorner.x,
upperRightCorner.y - lowerLeftCorner.y);
I think it might be late, but here's a solution it works for me, quite similar to yours, I ended up adding a padding at the end.
}