根据用户在Swift

时间:2017-03-17 08:52:36

标签: ios swift xcode mapkit mapbox

我希望制作一个简单的导航应用程序,它总是根据用户的方向旋转。我已经看到了android的一些答案,但还没有发现任何适用于Swift的东西。

我尝试了以下但是它不适合我,我不确定这是否让我更接近我正在寻找的东西。

let newCam = MGLMapCamera()
newCam.heading = 90
mapView.setCamera(newCam, animated: true)

我对Xcode和Swift很新,而且我很难理解,所以对我很轻松!

2 个答案:

答案 0 :(得分:3)

mapView有一个属性:direction,您可以设置。更改此属性的值会立即更新地图视图。如果要为更改设置动画,请改用-setDirection:animated:方法。或者您可以使用以下方法:

func setCenter(_ centerCoordinate: CLLocationCoordinate2D, zoomLevel: Double, direction: CLLocationDirection, animated: Bool)

方向是

  

地图的新方向,以相对于真北的度数来衡量。

您可以使用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)
}
}

我现在去看牙医但会在一个小时左右回来检查。

答案 1 :(得分:2)

func locationManager(_ manager: CLLocationManager, didUpdateHeading heading: CLHeading) {
    // set mapView's direction to user's orientation, i.e. magnetic heading
    mapView.direction = heading.magneticHeading
}