Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Get Location, but not heading

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?

like image 659
David Findlay Avatar asked Nov 29 '25 01:11

David Findlay


1 Answers

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

like image 61
Optimus Avatar answered Dec 02 '25 00:12

Optimus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!