0

情况如下:我正在使用 MapKit 在地图上显示多个位置。我有代码来计算允许地图上所有点适合窗口的边界。我最近刚刚添加了代码以允许出现最近位置的标注气泡(专有名词?)。不幸的是,标注气泡不适合窗口的边界。理想情况下,我希望调整缩放以适应所有注释以及最近位置的标注气泡。关于如何做到这一点的任何建议?这是我的缩放方法:

-(void)zoomToFitMapAnnotations:(MKMapView*)mv
{   
    if([mv.annotations count] == 0)
            return;

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

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

    for(WaybackAnnotation *annotation in [mv annotations])
    {
        if ([annotation isKindOfClass:[WaybackAnnotation class]])
        {
            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; // Add a little extra space on the sides
    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides

    region = [mv regionThatFits:region];
    [mv setRegion:region animated:YES];
}

提前致谢!詹姆士

4

1 回答 1

3

由于气泡始终显示在图钉上方,因此您需要在顶部添加一些额外的填充。您知道填充的高度(以屏幕像素为单位)。使用MKMapViews 转换方法(-convertRect:toRegionFromView:和朋友)将这些转换为地理坐标。

于 2011-03-09T11:13:38.437 回答