【问题标题】:iOS MapBox fit camera to multiple coordinates [latitude and longitude] or annotationsiOS MapBox 适合相机到多个坐标 [纬度和经度] 或注释
【发布时间】:2022-04-28 11:33:55
【问题描述】:

在 Android 的 MapBox 文档中有 LatLngBounds,但对于 iOS,什么都没有,有什么方法可以在 iOS 上完成

【问题讨论】:

    标签: ios swift mapbox-ios


    【解决方案1】:

    根据他们的文档,您似乎只需要从类似于 Apple Maps 工作流程的地理位置列表中构建您的 MGLCoordinateBounds,然后在您的地图框视图上调用 https://docs.mapbox.com/ios/maps/api/6.3.0/Classes/MGLMapView.html#/c:objc(cs)MGLMapView(im)setVisibleCoordinateBounds:animated:。

    【讨论】:

    • 很抱歉没有提到我正在使用最新版本的 MapBox (10.0.0-rc.4)。原因是苹果M1芯片。其中目前Public MapBox (v6.3.0) 不支持Apple M1 芯片。我有 [MacBook mini]。我没有搜索相同的东西...没有相机动画到 CordinateBouns 只有docs.mapbox.com/ios/maps/api/10.0.0-rc.4/Classes/…
    【解决方案2】:

    从 Mapbox v10.4.3 开始,这个解决方案适用于我,基本上你需要 4 个坐标作为相机的矩形边界。

    https://docs.mapbox.com/ios/maps/guides/migrate-to-v10/#fit-the-camera-to-a-given-shape

    示例片段

    class Coordinate {
      var longitude: Double = 0
      var latitude: Double = 0
    
      init(longitude: Double, latitude: Double) {
        self.longitude = longitude
        self.latitude = latitude
        self.datasetId = datasetId
      }
    
      func coordinates() -> CLLocationCoordinate2D {
        return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
      }
    }
    
    func setCoordinateBounds(bounds: [Coordinate]) -> CoordinateBounds {
        var flyTo = MKMapRect.null
          for bound in bounds {
            let point: MKMapPoint = MKMapPoint(bound.coordinates())
            let pointRect = MKMapRect(x: point.x, y: point.y, width: 0, height: 0)
            flyTo = flyTo.isNull ? pointRect : flyTo.union(pointRect)
          }
        let coordNE = MKMapPoint(x: flyTo.origin.x, y: flyTo.maxY).coordinate
        let coordSW = MKMapPoint(x: flyTo.maxX, y: flyTo.origin.y).coordinate
        return CoordinateBounds(southwest: coordSW, northeast: coordNE)
      }
    
    // Implementation
    let coordinates = [
        CLLocationCoordinate2DMake(24, -89),
        CLLocationCoordinate2DMake(24, -88),
        CLLocationCoordinate2DMake(26, -88),
        CLLocationCoordinate2DMake(26, -89),
        CLLocationCoordinate2DMake(24, -89)
    ]
    let coordinateBounds = setCoordinateBounds(bounds: coordinates)
    let cameraOptions = self.mapView.mapboxMap.camera(for: coordinateBounds, padding: UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20), bearing: self.mapView.cameraState.bearing, pitch: 0)
          self.mapView.camera.fly(to: cameraOptions, duration: 0.3, completion: nil)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-27
      • 1970-01-01
      相关资源
      最近更新 更多