Comment zoomer Mapbox en utilisant Swift à un rayon donné en kilomètres?

J'ai récemment mis à jour depuis l'ancien Mapbox SDK (version 1.6) vers le dernier SDK 3.x de Mapbox iOS.

Dans la nouvelle version, je ne suis pas en mesure de comprendre comment zoomer Mapbox MGLMapView à un rayon donné en kilomètres pour s'adapter à l'écran.

Dans l'ancienne (version 1.6), la méthode RMMapView.zoomWithLatitudeLongitudeBoundsSouthWest effectue le travail comme suit:

 func centerAndZoom(center: CLLocationCoordinate2D, kilometers: Float) { let meters = Double(kilometers * 1000) let region = MKCoordinateRegionMakeWithDistance(center, meters / 2, meters / 2); let northEastLat = center.latitude - (region.span.latitudeDelta / 2); let northEastLon = center.longitude + (region.span.longitudeDelta / 2); let northEast = CLLocationCoordinate2D(latitude: northEastLat, longitude: northEastLon) let southEastLat = center.latitude + (region.span.latitudeDelta / 2); let southEastLon = center.longitude - (region.span.longitudeDelta / 2); let southEast = CLLocationCoordinate2D(latitude: southEastLat, longitude: southEastLon) self.mapView.zoomWithLatitudeLongitudeBoundsSouthWest(southEast, northEast: northEast, animated: true) } 

Comment atteindre un rayon zoom dans la dernière Mapbox en utilisant Swift?

Maintenant -setVisibleCoordinateBounds:animated fera le travail:

Modifie la window d'affichage du récepteur pour l'adapter aux limites de coordonnées données, en animant éventuellement la modification.

Référence du SDK Mapbox iOS

Voici un exemple:

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

Voici comment j'ai fait un zoom sur un rayon spécifique à partir d'une coordonnée donnée:

 let center = CLLocationCoordinate2D(latitude: <#T##CLLocationDegrees#>, longitude: <#T##CLLocationDegrees#>) let kilometers: Double = 2.0 let halfMeters = (kilometers * 1000) / 2 let region = MKCoordinateRegionMakeWithDistance(center, halfMeters, halfMeters) 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) ) let bounds = MGLCoordinateBounds(sw: southWest, ne: northEast) mapView.setVisibleCoordinateBounds(bounds, edgePadding: .zero, animated: false)