gpt4 book ai didi

ios - 未经许可将 SCNNode 旋转到北以进行位置跟踪

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:28:21 25 4
gpt4 key购买 nike

我们想旋转一个 SCNNode 以便与北方向匹配。

Arkit 允许将其 z 轴与北方对齐,但这需要用户许可才能进行位置跟踪,而我们不想要求这样做。见gravityandheading

因此,我们尝试使用 CMDeviceMotion 的磁场属性。但我们不知道该怎么做。大概有一些矩阵计算,我们暂时不掌握。见magneticfield

任何帮助将不胜感激!谢谢!

最佳答案

未经用户许可,您不能使用位置跟踪。

以下是有关如何为AR 应用NON-AR 应用 正确设置它的一些提示:

iOS 11 及更高版本中,您需要执行以下操作才能使位置正常工作:

  • 将 key 添加到您的 info.plist 并向要求其启动的位置管理器请求授权。 info.plist 中有两个 Property List Keys 用于位置授权。 需要其中一个或两个键。在非 AR 应用程序中,如果两个键都不存在,您可以调用 startUpdatingLocation 方法,但位置管理器实际上不会启动。它也不会向委托(delegate)发送失败消息(因为它从未启动,所以它不会失败)。 如果您添加一个或两个 key 但忘记明确请求授权,它也会失败

Use these two Property List Keys in info.plist file. They are used since iOS 11 was released:

<key>NSLocationWhenInUseUsageDescription</key>
<string>App needs an access to your location (in foreground)</string>

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>App needs an access to your location (in background)</string>

And these two Property List Keys were deprecated earlier (DON'T USE THESE KEYS):

    <key>NSLocationUsageDescription</key>
<string>App needs an access to your location (in background)</string>

<key>NSLocationAlwaysUsageDescription</key>
<true/>

AR 应用的代码应如下所示:

let configuration = ARWorldTrackingConfiguration()

func session(_ session: ARSession, didFailWithError error: Error) {

switch error.code {
case 101:
configuration.worldAlignment = .gravity
restartSession()
default:
configuration.worldAlignment = .gravityAndHeading
restartSession()
}
}
func restartSession() {
self.sceneView.session.pause()
self.sceneView.session.run(configuration,
options: [.resetTracking, .removeExistingAnchors])
}

对于非 AR 应用,使用这两个实例方法:requestAlwaysAuthorization()(请求在应用运行时使用定位服务的权限)和requestWhenInUseAuthorization()(请求允许在应用程序处于前台时使用定位服务。

非 AR 应用的代码应如下所示:

import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

var locationManager = CLLocationManager()

func updateMyLocation() {

locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers

if locationManager.respondsToSelector(#selector(CLLocationManager.requestWhenInUseAuthorization)) {
locationManager.requestWhenInUseAuthorization()
} else {
locationManager.startUpdatingLocation()
}
}
}

此外,您可以请求始终授权:

let locationManager = CLLocationManager()   

func enableLocationServices() {

locationManager.delegate = self

switch CLLocationManager.authorizationStatus() {

case .notDetermined:
// Request when-in-use authorization initially
locationManager.requestWhenInUseAuthorization()
break

case .restricted, .denied:
// Disable location features
disableMyLocationBasedFeatures()
break

case .authorizedWhenInUse:
// Enable basic location features
enableMyWhenInUseFeatures()
break

case .authorizedAlways:
// Enable any of your app's location features
enableMyAlwaysFeatures()
break
}
}

希望这对您有所帮助。

关于ios - 未经许可将 SCNNode 旋转到北以进行位置跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55872126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com