【问题标题】:Map Bounding Box with Annotations带注释的地图边界框
【发布时间】: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


【解决方案1】:

我认为可能已经晚了,但这是一个对我有用的解决方案,与您的非常相似,我最终在最后添加了一个填充。

- (MKCoordinateRegion)boundingBoxForAnnotations:(NSArray *)annotations { 

CLLocationCoordinate2D topLeftCoord; 
topLeftCoord.latitude = -90; 
topLeftCoord.longitude = 180; 

CLLocationCoordinate2D bottomRightCoord; 
bottomRightCoord.latitude = 90; 
bottomRightCoord.longitude = -180; 

for (id<MKAnnotation> annotation in annotations) {

    topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); 
    topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude); 
    bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); 
    bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); 
}

MKCoordinateRegion region; 
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5; 
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5; 
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; 

region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; 

return region;

}

【讨论】:

    猜你喜欢
    • 2019-11-13
    • 2020-08-12
    • 1970-01-01
    • 2021-04-16
    • 2019-09-04
    • 2021-08-30
    • 2019-09-13
    • 2011-09-12
    • 1970-01-01
    相关资源
    最近更新 更多