在Swift中根据用户的方向旋转Mapbox地图(Rotate Mapbox Map depending on user's orientation in Swift)

编程入门 行业动态 更新时间:2024-10-28 09:22:52
在Swift中根据用户的方向旋转Mapbox地图(Rotate Mapbox Map depending on user's orientation in Swift)

我正在寻找一个简单的导航应用程序,它总是根据用户的方向旋转。 我已经看到了一些Android的答案,但还没有发现任何工作在斯威夫特。

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

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

我对Xcode和Swift非常陌生,并且无法理解,因此轻松点我!

I'm looking to make a simple navigation application which always rotates depending on the user's orientation. I've seen some answers for android but haven't found anything that works in Swift yet.

I tried the following however it is not working for me and am unsure if this is even getting me closer to what I'm looking for.

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

I'm quite new to Xcode and Swift and am having trouble understanding so go easy on me!

最满意答案

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) } }

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

The mapView has a property: direction, that you can set. Changing the value of this property updates the map view immediately. If you want to animate the change, use the -setDirection:animated: method instead. Or you could use the following method:

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

Where direction is

The new direction for the map, measured in degrees relative to true north.

You could call this periodically, calculating the offset from north, using the startUpdatingHeading method on CLLocationManager. There is another version of this method with a completion handler if you needed that.

** EDIT **

You can use the following code to extract the direction the phone is oriented. It prints to the console here (from a real device naturally) but you could take the figure (degrees) and in determine in your code how far from north the device is pointing.

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) } }

I'm off to the dentist now but will check back in an hour or so.

更多推荐

本文发布于:2023-07-28 12:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1305178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方向   地图   用户   Mapbox   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!