【发布时间】:2012-06-12 06:28:16
【问题描述】:
我有一个算法可以计算包含一组给定坐标的地图的边界框的大小和位置。虽然它工作得很好,但它产生的边界并不总是适应我放置在通常位于边界框边缘的坐标处的图钉:
...同样,它在大多数情况下都会产生可接受的结果:
我已经考虑了一段时间,但我还没有想出一种方法来修改我的算法以确保图钉始终在边界框内。我的算法在下面列出,任何建议将不胜感激。 :)
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);
【问题讨论】:
-
... 在顶部添加一点填充?这是最简单的方法:)
-
不是您的问题的答案,但可以通过使用 LatLngBounds 的扩展方法更轻松地完成此操作
标签: iphone objective-c ios google-maps geometry