如何在MGL mapView中缩放,以便视图包含地图上的一个“对象”

时间:2019-02-13 16:30:46

标签: ios swift zoom mapbox

我在地图框地图上有一条MGLPolyline,我想这样做的是,当用户点击该行时,它将围绕该行居中并尽可能放大,以便显示整条线。目前,我可以使居中工作正常,但缩放可以随机进行:

我只是将其设置为最大缩放,但是,这当然不是我想要的。

下面是我要添加缩放量的地方:

    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)
    }

1 个答案:

答案 0 :(得分:2)

MGLMapView实际上具有专门用于此目的的引入方法。您应该能够使用-showAnnotations:animated来实现该功能。如果您想弄乱折线周围的填充,还可以使用该方法的showAnnotations:edgePadding:animated风格。

如下所示:

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