ios - 根据用户在 Swift 中的方向旋转 Mapbox map

标签 ios swift xcode mapkit mapbox

我正在寻找一个简单的导航应用程序,它总是根据用户的方向旋转。我已经看到了一些适用于 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/

相关文章:

Swift - 类级别实例化和方法级别实例化之间的区别

C++:Xcode 中未初始化的变量输出为 0

iphone - 我在哪里可以获得有关 VOIP 应用程序的 iOS 文件处理程序的信息?

ios - tableView 添加/删除到第二部分 objective-c

swift - 使用正则表达式将连字符插入长度有限的字符串

xcode - 卸载 macOS 应用程序

ios - XCode Framework 引用路径和构建输出目录

ios - 导航栏上的自定义按钮停止工作

ios - 如何在 SplitView Controller 中隐藏和取消隐藏主视图 Controller

ios - 计算 DragGesture 的速度