ios - 旋转 Mapbox 相机
问题描述
我在我的 iOS 项目中使用 Swift 语言使用 Mapbox。我目前正在寻找一种以编程方式在固定高度旋转地图视图的方法,其中包含某些坐标。我已经尝试了很多,甚至还浏览了他们的 API 参考,但找不到任何帮助。
有没有人试图做这样的事情,其中 MapView 正在不断旋转,使用 MAPBOX 库将某些坐标保持在界限内。
帮助将不胜感激。
解决方案
感谢@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
}
}
希望这对其他人有帮助。谢谢
推荐阅读
- excel - Excel XY 图表五彩线
- sql-server - SQL Server 中的舍入时间
- jekyll - 如何使用 Liquid“样本”过滤器在 Jekyll 中生成随机帖子?
- javascript - uwt.js(Twitter 跟踪像素?)导致 20 秒的加载时间
- npm - 无法从“npm install -g npm@latest”命令升级 npm
- haskell - 变量不在范围内:defaultUserHooks :: UserHooks
- python - PythonAPI 请求
- python - Scapy wrpcap 与 rdpcap 不匹配
- xcode - 编译器无法在合理的时间内对该表达式进行类型检查;尝试将表达式分解为不同的子表达式?
- google-apps-script - 通过电子邮件向自己发送表格的内容会返回 #ERROR!使用 AppScript 的 GoogleFinance 函数