缩放MapView以适合所有引脚

时间:2013-11-03 14:44:34

标签: ios objective-c mkmapview

我使用了以下代码,这些代码将在整个SO和网络上发布。它在4英寸屏幕上工作正常,但出于某种原因,5英寸屏幕没有提供所需的输出。

MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in _mapView.annotations) {

    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
    MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
    if (MKMapRectIsNull(zoomRect)) {
        zoomRect = pointRect;
    } else {
        zoomRect = MKMapRectUnion(zoomRect, pointRect);
    }


}

zoomRect = MKMapRectMake(zoomRect.origin.x - 40 * (zoomRect.size.width / _mapView.frame.size.width),
                         zoomRect.origin.y - 60 * (zoomRect.size.height / _mapView.frame.size.height),
                         zoomRect.size.width + 80 * (zoomRect.size.width / _mapView.frame.size.width),
                         zoomRect.size.height + 80 * (zoomRect.size.height / _mapView.frame.size.height));

[_mapView setVisibleMapRect:zoomRect
                   animated:NO];

2 个答案:

答案 0 :(得分:1)

我在许多项目中使用以下代码:

CLLocationDegrees maxLat = -90.0f;
CLLocationDegrees maxLon = -180.0f;
CLLocationDegrees minLat = 90.0f;
CLLocationDegrees minLon = 180.0f;

for (id <MKAnnotation> annotation in _mapView.annotations) {
    CLLocationDegrees lat = annotation.coordinate.latitude;
    CLLocationDegrees lon = annotation.coordinate.longitude;

    maxLat = MAX(maxLat, lat);
    maxLon = MAX(maxLon, lon);
    minLat = MIN(minLat, lat);
    minLon = MIN(minLon, lon);
}
MKCoordinateRegion region;
region.center.latitude     = (maxLat + minLat) / 2;
region.center.longitude    = (maxLon + minLon) / 2;
region.span.latitudeDelta  = maxLat - minLat + 0.05;
region.span.longitudeDelta = maxLon - minLon + 0.05;

[map setRegion:region animated:NO];

答案 1 :(得分:0)

好吧,

MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in mapView.annotations)
{
   MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
   MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
   zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
[mapView setVisibleMapRect:zoomRect animated:YES];