Search code examples
annotationscoordinateszoomingmapbox

How can I include all the coordinates in the MapBox by adjusting the zoom level?


The code I have used is:

let bounds = MGLCoordinateBounds(
    sw: CLLocationCoordinate2D(latitude: 40.7115, longitude: 10.3725),
    ne: CLLocationCoordinate2D(latitude: 40.7318, longitude: 10.4222))
    mapView.setVisibleCoordinateBounds(bounds, animated: false)

I have 3 annotation and I want to see the 3 annotaion in the mapBox by adjusting the zoom level. Please help me to find it out.


Solution

  • There is method on MGLMapView that you can use:

    func showAllAnnotations() {
        guard let annotations = mapView.annotations else { return }
    
        // Either this...
        mapView.showAnnotations(annotations, edgePadding: UIEdgeInsets(top: 40, left: 35, bottom: 35, right: 35), animated: true)
        // or this.
        mapView.showAnnotations(annotations, animated: true)
    }