在 Swift 中根据用户的方向旋转 Mapbox 地图
Rotate Mapbox Map depending on user's orientation in Swift
我正在寻找一个简单的导航应用程序,它总是根据用户的方向旋转。我已经看到 android 的一些答案,但还没有找到任何适用于 Swift 的答案。
我尝试了以下方法,但它对我不起作用,我不确定这是否让我更接近我正在寻找的东西。
let newCam = MGLMapCamera()
newCam.heading = 90
mapView.setCamera(newCam, animated: true)
我是 Xcode 和 Swift 的新手,理解起来有困难,请放轻松!
mapView 有一个 属性: direction
,你可以设置。更改此 属性 的值会立即更新地图视图。如果要为更改设置动画,请改用 -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 方法计算与北方的偏移量。如果您需要,此方法还有另一个版本,带有完成处理程序。
** 编辑 **
您可以使用以下代码提取 phone 的方向。它在此处打印到控制台(自然地从真实设备打印),但您可以获取数字(度数)并在代码中确定设备指向北方的距离。
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)
}
}
我现在去看牙医,但会在一小时左右回来查看。
func locationManager(_ manager: CLLocationManager, didUpdateHeading heading: CLHeading) {
// set mapView's direction to user's orientation, i.e. magnetic heading
mapView.direction = heading.magneticHeading
}
我正在寻找一个简单的导航应用程序,它总是根据用户的方向旋转。我已经看到 android 的一些答案,但还没有找到任何适用于 Swift 的答案。
我尝试了以下方法,但它对我不起作用,我不确定这是否让我更接近我正在寻找的东西。
let newCam = MGLMapCamera()
newCam.heading = 90
mapView.setCamera(newCam, animated: true)
我是 Xcode 和 Swift 的新手,理解起来有困难,请放轻松!
mapView 有一个 属性: direction
,你可以设置。更改此 属性 的值会立即更新地图视图。如果要为更改设置动画,请改用 -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 方法计算与北方的偏移量。如果您需要,此方法还有另一个版本,带有完成处理程序。
** 编辑 **
您可以使用以下代码提取 phone 的方向。它在此处打印到控制台(自然地从真实设备打印),但您可以获取数字(度数)并在代码中确定设备指向北方的距离。
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)
}
}
我现在去看牙医,但会在一小时左右回来查看。
func locationManager(_ manager: CLLocationManager, didUpdateHeading heading: CLHeading) {
// set mapView's direction to user's orientation, i.e. magnetic heading
mapView.direction = heading.magneticHeading
}