func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) 似乎从未被调用过。

问题描述 投票:0回答:1

我有

locationManager.requestWhenInUseAuthorization()  
locationManager.startUpdatingLocation()  
locationManager.startUpdatingHeading() 

我也把一切plist中的隐私位置位置服务开始打开。

当我检查

if CLLocationManager.headingAvailable()  

返回true,但该方法似乎从未被调用。什么都没有发生。

这是我的完整代码。现在测试起来一团糟。

import UIKit
import SceneKit
import CoreLocation

class GameViewController: UIViewController, CLLocationManagerDelegate {

var locationManager:CLLocationManager!
var angle = 0.0
var rotate = SCNAction.rotate(by: 0.0, around: SCNVector3(0, 1, 0), duration: 0.5)

override func viewDidLoad() {
    super.viewDidLoad()

    let locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
    locationManager.startUpdatingHeading()

    let scene = SCNScene(named: "art.scnassets/CompassObject2 copy.scn")

    let compassNode = scene?.rootNode.childNodes[0]
    compassNode?.position = SCNVector3(x:0, y:0, z: 0)

    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    scene?.rootNode.addChildNode(cameraNode)
    cameraNode.position = SCNVector3(x:0, y:50, z:-5)

    let lightNode = SCNNode()
    lightNode.light = SCNLight()
    lightNode.light?.type = .omni
    lightNode.light?.intensity = 100000
    lightNode.position = SCNVector3(x:0, y:150, z: 0)
    scene?.rootNode.addChildNode(lightNode)

    //let origin = SCNNode()
    //origin.position = SCNVector3(x:0, y:0, z: 0)
    let lookAt = SCNLookAtConstraint(target: compassNode)
    lookAt.isGimbalLockEnabled = true
    cameraNode.constraints = [lookAt]

    _ = scene?.rootNode.childNode

    let scnView = self.view as! SCNView

    scnView.scene = scene
    scnView.allowsCameraControl = false
    scnView.backgroundColor = UIColor.black

    //print(compassNode?.position as Any)

    if CLLocationManager.locationServicesEnabled() {
        print("yes")
    }
    else {
        print("no")
    }
    /*if CLLocationManager.headingAvailable() {
        scnView.backgroundColor = UIColor.blue
        print("yes")
    }
    else {
        print("no")
    }*/
    if angle != 0.0 {
        scnView.backgroundColor = UIColor.green
    }
    compassNode?.runAction(rotate)
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
    //UIView.animate(withDuration: 0.5) {
        self.angle = newHeading.trueHeading.toRadians
        self.rotate = SCNAction.rotate(by: CGFloat(self.angle), around: SCNVector3(0, 1, 0), duration: 0.5)
    //}
}
/* func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    exit(0)
}*/
func locationManagerShouldDisplayHeadingCalibration(_ manager: CLLocationManager) -> Bool {
    return true
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations newLocation: CLLocation) {
    print(newLocation)
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    print("delegate works")
}
}

extension Double {
var toRadians: Double { return self * .pi / 180 }
}

似乎委派确实有效,因为检查授权状态的func会打印 "委派有效",但其他func,最重要的是didUpdateHeading仍然不做任何事情,好像它从来没有被调用。

swift core-location
1个回答
0
投票

确保你已经设置了 CLLocationManager 委托?

顺便说一下,如果你只想知道用户的标题,你不需要位置权限。 如果你想要他们的实际位置,那么你需要申请相应的授权并设置pList值。

import CoreLocation
import UIKit

class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager: CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()
        locationManager.delegate = self // <-- Make sure you have set this
        locationManager.startUpdatingHeading()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        print(newHeading)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.