iphone - 带注释的 map 边界框

标签 iphone objective-c ios google-maps geometry

我有一种算法,可以为包含一组给定坐标的 map 计算边界框的大小和位置。虽然效果很好,但它产生的边界并不总是容纳我放置在通常位于边界框边缘上的坐标处的图钉:



...并且在大多数情况下,它会产生可接受的结果:

我已经考虑了一段时间,但是我还没有想出一种方法来修改我的算法,以确保图钉始终在边界框内。我的算法在下面列出,任何建议将不胜感激。 :)

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);

最佳答案

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

- (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;

}

关于iphone - 带注释的 map 边界框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9493049/

相关文章:

iphone - 如何在仍然按住手指的同时从使用一个 UIButton 切换到另一个?

iphone - ios asihttprequest 发布字符串

ios - 在 UITableView 的单元格中选择特定的字符串值

ios - Facebook 登录 Swift 项目

iphone - Facebook API 对话框页面在第二次及之后显示错误

iphone - 如何在 View 中显示加载动画 - IOS

iphone - 尝试使用 QuartzCore 动画时收到错误消息

Sketch 3 的 iOS 图像分辨率大小

ios - UIDocumentInteractionController 停止工作 iOS 8

C# vuforia.image.pixels 覆盖到 iOS CIImage 或 CGImage