Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS: zoom in mapkit for two annotation point

Tags:

xcode

ios

mapkit

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

like image 447
cyclingIsBetter Avatar asked May 17 '12 23:05

cyclingIsBetter


1 Answers

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];
like image 148
Jan S. Avatar answered Oct 30 '22 16:10

Jan S.