What's the best way to zoom out and fit all annotations in MapKit

前端 未结 4 1561
醉梦人生
醉梦人生 2020-12-12 17:49

Quick background of what I have going on. UIMapView loads and shows a single annotation (Never more than one). On the menu bar, there is a Locate Me button, on tap the use

4条回答
  •  -上瘾入骨i
    2020-12-12 18:13

    You can use this code to show all annotations

    MKMapRect zoomRect = MKMapRectNull;
    for (id  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];
    

    if you want to include user location just replace two lines below with the first line of above code

    MKMapPoint annotationPoint = MKMapPointForCoordinate(mapView.userLocation.coordinate);
    MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
    

提交回复
热议问题