Question: I'm looking for a way to fit all views with the right zoom which in my app's instance is one annotation and userlocation and the whole polyline which signifies the route connecting the annotation and the userlocation.

I have this code below which I call every time the map loads the mapview:

```
func mapViewDidFinishLoadingMap(_ mapView: MGLMapView) {
        if let location = mapView.userLocation?.location?.coordinate, let annotations = mapView.annotations {
                let coordinate = annotations.first?.coordinate
                mapView.setVisibleCoordinateBounds(MGLCoordinateBounds(sw: location, ne: coordinate!), edgePadding: UIEdgeInsetsMake(100, 50, 100, 200 ), animated: true)
        }
    }

```

This works well for coordinates where the routes are almost linear but when the routes are a bit more complicated and long it doesn't help.

Answer: 
This method will give you the bounds on the polyline no matter how short or long it is. You need an array of locations somewhere in your VC being locationList: [CLLocation] in this example.

```
func setCenterAndBounds() -> (center: CLLocationCoordinate2D, bounds: MGLCoordinateBounds)? {
    let latitudes = locationList.map { location -> Double in
        return location.coordinate.latitude
    }

    let longitudes = locationList.map { location -> Double in
        return location.coordinate.longitude
    }

    let maxLat = latitudes.max()!
    let minLat = latitudes.min()!
    let maxLong = longitudes.max()!
    let minLong = longitudes.min()!

    let center = CLLocationCoordinate2D(latitude: (minLat + maxLat) / 2, longitude: (minLong + maxLong) / 2)
    let span = MKCoordinateSpan(latitudeDelta: (maxLat - minLat) * 1.3, longitudeDelta: (maxLong - minLong) * 1.3)
    let region = MKCoordinateRegion(center: center, span: span)
    let southWest = CLLocationCoordinate2D(latitude: center.latitude - (region.span.latitudeDelta  / 2),
        longitude: center.longitude - (region.span.longitudeDelta / 2)
    )
    let northEast = CLLocationCoordinate2D(
        latitude: center.latitude + (region.span.latitudeDelta  / 2),
        longitude: center.longitude + (region.span.longitudeDelta / 2)
    )

    return (center, MGLCoordinateBounds(sw: southWest, ne: northEast))
}

```

Then you can set the mapbox view like this:

```
func populateMap() {
    guard locationList.count > 0, let centerBounds = setCenterAndBounds() else { return }
    mapboxView.setCenter(centerBounds.center, animated: false)
    mapboxView.setVisibleCoordinateBounds(centerBounds.bounds, animated: true)}

```
If you know the zoom level use [Truf's centroid](http://turfjs.org/Docs#centroid) feature to grab the centroid of the polygon, then apply it to MGL [map.flyTo](https://www.mapbox.com/mapbox-gl-js/api/#map#flyto) like this:

```
    var centroid = turf.centroid(myCoolFeatureGeometry);

    map.flyTo({
      center: centroid.geometry.coordinates,
      zoom: 19,
      curve: 1,
      screenSpeed: 4,
      easing(t) {
        return t;
      }
    });

```

If you also need the zoom level, you can get it using [Turf's bbox](http://turfjs.org/Docs#bbox) (or [envelope](http://turfjs.org/Docs#envelope)) and apply its output to MGL [map.fitbounds](https://www.mapbox.com/mapbox-gl-js/api/#map#fitbounds)

```
var features = turf.featureCollection([
  turf.point([-75.343, 39.984], {"name": "Location A"}),
  turf.point([-75.833, 39.284], {"name": "Location B"}),
  turf.point([-75.534, 39.123], {"name": "Location C"})
]);

var enveloped = turf.envelope(features);

map.fitBounds(enveloped, {
  padding: {top: 10, bottom:25, left: 15, right: 5}
});

```