1

I have used the following code which is being posted all over the SO and also on the net. It's working OK on 4 inch screen but for some reason, 5 inch screen is not giving the required output.

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];
itsaboutcode
  • 24,525
  • 45
  • 110
  • 156

2 Answers2

1

I use the following code in many projects:

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];
gimenete
  • 2,649
  • 1
  • 20
  • 16
  • @gimente - thanks for the comment. It has almost fixed the issue. Just want to know if I want to minus height from map view height so that it use that height. – itsaboutcode Nov 03 '13 at 16:26
  • The code is independent of the map height. The `setRegion` method adjusts the zoom to fit those coordinates to your current mapView height. So it should work regardless of the mapView dimensions. – gimenete Nov 03 '13 at 16:36
0

Well,

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];
oiledCode
  • 8,589
  • 6
  • 43
  • 59