6

I want to know if there is a way to have a dynamic zoom when I set in my mapview the blue dot (my position) and another pin...when I load mapview I want to show these two point in the same frame...is it possible? thanks

cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241

1 Answers1

9

Yes, it is possible. After adding the second annotation. This sample code should work for any number of annotations:

MKMapRect zoomRect = MKMapRectNull;
NSArray *annotations = [mapView annotations];
for (MKAnnotation *annotation in 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);
    }

}

[mapView setVisibleMapRect:zoomRect animated:YES];
Jan S.
  • 10,328
  • 3
  • 31
  • 36
  • 1
    it's better.....http://stackoverflow.com/questions/4680649/zooming-mkmapview-to-fit-annotation-pins – cyclingIsBetter May 18 '12 at 09:19
  • 1
    Thank you, really useful, i only need to modify the type of the variable _"annotation"_ from **`MKAnnotation`** to **`MKPointAnnotation`** and works ;)! – IgniteCoders Mar 20 '14 at 15:15