1

我使用了以下代码,这些代码发布在整个 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];
4

2 回答 2

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];
于 2013-11-03T14:53:14.873 回答
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];
于 2013-11-03T14:54:12.030 回答