我正在寻找一个简单的导航应用程序,它总是根据用户的方向旋转。我已经看到了一些适用于 android 的答案,但还没有找到任何适用于 Swift 的答案。
我尝试了以下方法,但它对我不起作用,我不确定这是否让我更接近我正在寻找的东西。
let newCam = MGLMapCamera()
newCam.heading = 90
mapView.setCamera(newCam, animated: true)
我是 Xcode 和 Swift 的新手,理解起来有困难,所以请放轻松!
最佳答案
mapView 有一个属性:direction
,你可以设置。更改此属性的值会立即更新 map View 。如果您想为更改设置动画,请改用 -setDirection:animated:
方法。或者您可以使用以下方法:
func setCenter(_ centerCoordinate: CLLocationCoordinate2D, zoomLevel: Double, direction: CLLocationDirection, animated: Bool)
方向在哪里
The new direction for the map, measured in degrees relative to true north.
您可以定期调用它,使用 CLLocationManager 上的 startUpdatingHeading 方法计算与北方的偏移量。如果您需要,此方法还有另一个版本,带有完成处理程序。
** 编辑 **
您可以使用以下代码提取手机的朝向。它在此处打印到控制台(自然地从真实设备),但您可以获取数字(度数)并在代码中确定设备指向北方的距离。
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.startUpdatingHeading()
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading heading: CLHeading) {
print(heading.magneticHeading)
}
}
我现在去看牙医,但会在一小时左右回来检查。
关于ios - 根据用户在 Swift 中的方向旋转 Mapbox map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42852992/