问题
I'm only using the simulator currently, but I'm having trouble with CoreLocation in swift on the iOS simulator. I get position updates printed by this code, but never heading. I don't want course, I'm trying to make a compass type app that will show a bearing to a target.
class CompassViewController: UIViewController, CLLocationManagerDelegate {
var lm:CLLocationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
print ("view did load")
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest
lm.requestWhenInUseAuthorization()
lm.startUpdatingLocation()
lm.startUpdatingHeading()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var locValue:CLLocationCoordinate2D = manager.location!.coordinate
var course = manager.location!.course
print("locations = \(locations)")
}
func headingManger(_ manager: CLLocationManager, didUpdateHeading: [CLHeading]) {
var heading = manager.heading?.magneticHeading
print("heading = \(heading)")
}
}
I've tried a few combinations of this. I tried doing one function that handled both location and heading, but that didn't get any data printing at all. I also tried just having the headingManager function, but nothing was printed. This current code as above will print the location, but not the heading.
How can I get heading to work as well?
回答1:
Compass is not in the list of the hardware interactions supported by the simulator and CLLocationManager headingAvailable reports it's not available on the simulator.
Also, in this doc:
http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html
Some location services require the presence of specific hardware on the given device. For example, heading information is available only for devices that contain a hardware compass. This class defines several methods that you can use to determine which services are currently available. Specifically CLLocationManager has this class property to check if the compass is available:
- (BOOL)headingAvailable If I run this under the simulator:
print("headingAvailable: (Int(CLLocationManager.headingAvailable()))") Outputs:
2011-11-08 22:38:26.873 Craplet[39975:b603] headingAvailable: 0
回答2:
David,
I was refered to your question, which wasn't the answer. This was...
didUpdateHeading heading never called under iOS 11
Don't know if your running iOS 11, the beta was around in June I suspect, but certainly not released until September 2017, but maybe this hopes.
来源:https://stackoverflow.com/questions/44479865/can-get-location-but-not-heading