user10813210
user10813210

Reputation:

How to zoom, in MGL mapView, so that the view encompasses one "object" on the map

I have an MGLPolyline on a mapbox map and want to make is so when the user taps on the line it centers around the line and zooms in as much as possible so that the full line is on display. Currently, I make the centering work well but the zoom works randomly:

I just set it to max zoom, However, this is of course not what I want.

Bellow is where I want to add the zoom amount:

    func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation) {
         print("Tapped")
         mapView.setCenter(CLLocationCoordinate2D(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude), zoomLevel: mapView.zoomLevel, animated: true)

         mapView.deselectAnnotation(annotation, animated: false)
    }

Upvotes: 0

Views: 537

Answers (1)

riastrad
riastrad

Reputation: 1774

MGLMapView actually has a baked in method specifically for this purpose. You should be able to implement the functionality using -showAnnotations:animated. If you want to fiddle around with the padding around your polyline, you can also use the showAnnotations:edgePadding:animated flavor of the method.

This would look like the following:

 func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation) {        
    mapView.showAnnotations(pointAnnotations, animated: true)
}

Upvotes: 2

Related Questions