是否适合包含折线和用户位置的所有注释以适合Mapbox iOS sdk的地图视图?

时间:2017-12-04 08:30:30

标签: ios mapbox mapbox-gl mapbox-marker

我正在寻找一种方法,使用正确的缩放来适应所有视图,在我的应用程序实例中是一个注释和用户位置以及表示连接注释和用户位置的路线的整个折线。

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

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

2 个答案:

答案 0 :(得分:0)

如果您知道缩放级别使用Truf's centroid功能来抓取多边形的质心,则将其应用于MGL map.flyTo,如下所示:

ids <- c("1", "3", "7", "9", "12", "13", "18")

foo = function() {
    errors <- c("Error: Try Again", "Error: Stop for loop")
    if (runif(1) < 0.25) stop(sample(errors, 1))
}

numbers <- list()
jj = 1
while(jj <= length(ids)) {
    numbers[[jj]] <- as.numeric(ids[jj])
  outcome = tryCatch(foo(), error = function(e) e)
  if (is.null(outcome)) {
    jj = jj + 1
    next
  } else if (outcome$message == "Error: Stop for loop") {
    message("Loop stopped on iteration ", jj)
    break
  } else if (outcome$message == "Error: Try Again"){
    next
  }
}

如果您还需要缩放级别,可以使用Turf's bbox(或envelope)并将其输出应用于MGL map.fitbounds

    var centroid = turf.centroid(myCoolFeatureGeometry);

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

答案 1 :(得分:0)

无论多长或短,此方法都会为折线赋予边界。您需要在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))
}

然后您可以像这样设置地图框视图:

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