是否将所有注释(包括多段线和用户位置)拟合到Mapbox iOS sdk的地图视图中?

是否将所有注释(包括多段线和用户位置)拟合到Mapbox iOS sdk的地图视图中?,ios,mapbox,mapbox-gl,mapbox-marker,Ios,Mapbox,Mapbox Gl,Mapbox Marker,我正在寻找一种适合所有视图的方法,在我的应用程序实例中,它是一个注释和用户位置,以及整个多段线,表示连接注释和用户位置的路线 我有以下代码,每次地图加载mapview时我都会调用这些代码: func mapViewDidFinishLoadingMap(_ mapView: MGLMapView) { if let location = mapView.userLocation?.location?.coordinate, let annotations = mapView.an

我正在寻找一种适合所有视图的方法,在我的应用程序实例中,它是一个注释和用户位置,以及整个多段线,表示连接注释和用户位置的路线

我有以下代码,每次地图加载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)
        }
    }

这适用于路线几乎是线性的坐标,但当路线有点复杂和长时,这没有帮助。

如果您知道缩放级别使用功能获取多边形的质心,请将其应用于MGL,如下所示:

    var centroid = turf.centroid(myCoolFeatureGeometry);

    map.flyTo({
      center: centroid.geometry.coordinates,
      zoom: 19,
      curve: 1,
      screenSpeed: 4,
      easing(t) {
        return t;
      }
    });
func populateMap() {
    guard locationList.count > 0, let centerBounds = setCenterAndBounds() else { return }
    mapboxView.setCenter(centerBounds.center, animated: false)
    mapboxView.setVisibleCoordinateBounds(centerBounds.bounds, animated: true)}
如果还需要缩放级别,可以使用或获取它,并将其输出应用于MGL


此方法将为您提供多段线上的边界,无论多短或多长。在本例中,您需要VC中某个位置的数组,即locationList:[CLLocation]

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))
}
然后,可以按如下方式设置mapbox视图:

    var centroid = turf.centroid(myCoolFeatureGeometry);

    map.flyTo({
      center: centroid.geometry.coordinates,
      zoom: 19,
      curve: 1,
      screenSpeed: 4,
      easing(t) {
        return t;
      }
    });
func populateMap() {
    guard locationList.count > 0, let centerBounds = setCenterAndBounds() else { return }
    mapboxView.setCenter(centerBounds.center, animated: false)
    mapboxView.setVisibleCoordinateBounds(centerBounds.bounds, animated: true)}

这不是针对JS映射框的,因此无法应用您的答案。他需要mapboxswift,它与javascript不同。