首页 > 解决方案 > 旋转 Mapbox 相机

问题描述

我在我的 iOS 项目中使用 Swift 语言使用 Mapbox。我目前正在寻找一种以编程方式在固定高度旋转地图视图的方法,其中包含某些坐标。我已经尝试了很多,甚至还浏览了他们的 API 参考,但找不到任何帮助。

有没有人试图做这样的事情,其中​​ MapView 正在不断旋转,使用 MAPBOX 库将某些坐标保持在界限内。

帮助将不胜感激。

标签: iosswiftmapbox

解决方案


感谢@riastrad 的指导,我想出了一堆代码来帮助实现这个功能。

与所有人分享,以便他们在需要时获得帮助:

代码适用于 Swift 4.2

//Create a bound using two coordinates.
let coordinateBounds = MGLCoordinateBounds(sw: coordinateOne, ne: coordinateTwo)
//Add Insets for the bounds if needed
let mapEdgeInsets = UIEdgeInsets(top: 10.0, left: 0.0, bottom: 0.0, right: 10.0)
//get the camera that fit those bounds and edge insets
let camera = self.mapView.cameraThatFitsCoordinateBounds(coordinateBounds, edgePadding: mapEdgeInsets)
//Update camera pitch (if required)
camera.pitch = 60
//setup CameraHeading 
let zoomLevel = self.mapView.zoomLevel
var cameraHeading = camera.heading
if zoomLevel > 14 {
    cameraHeading += 2.2
} else {
    cameraHeading += 0.7
}
if cameraHeading > 359 {
    cameraHeading = 1
}
camera.heading = cameraHeading
//set new camera with animation
let newCamera = camera
self.mapView.setCamera(newCamera, withDuration: 0.1, animationTimingFunction: CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))

将上述代码集放在一个方法中,并每 0.1 秒重复调用该方法。

private func enableRotationTimer(_ enable:Bool) {
    guard self.store != nil else { return }
    if enable == true {
        if mapRotationTimer == nil {
            mapRotationTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(rotateCamera), userInfo: nil, repeats: true)
        }
    } else {
        mapRotationTimer?.invalidate()
        mapRotationTimer = nil
    }
}

希望这对其他人有帮助。谢谢


推荐阅读