Didupdateheading Not Called in Swift

I don't understand Property 'self.heading' not initialized at super.init call error in my code

You MUST initial all instance variables of the current class before initializing the super.

So add some initial value to heading before calling super.init() like:

override init() {
heading = 0 // Or anything you need
super.init()

locationManager = CLLocationManager()
locationManager!.delegate = self
locationManager!.desiredAccuracy = kCLLocationAccuracyBest
locationManager!.requestWhenInUseAuthorization()
}

Getting the direction the user is facing using Swift

Swift 5.2

You can do this with the CoreLocation framework.

  1. Conform to the CLLocationManagerDelegate protocol
  2. Instantiate an instance of CLLocationManager and set the delegate as self
  3. You do not need user permissions if you just need the heading (and not actual user location)
  4. Tell the CLLocationManager to start updating the heading
  5. The heading will be reported back to the delegate method didUpdateHeading
  6. Use a switch statement to find cardinal direction based on the input degrees.
  7. Update your label


import CoreLocation
import UIKit

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet var directionLabel: UILabel!

var locationManager: CLLocationManager!

override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.startUpdatingHeading()
}

func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
directionLabel.text = cardinalValue(from: newHeading.trueHeading)
}

func cardinalValue(from heading: CLLocationDirection) -> String {
switch heading {
case 0 ..< 22.5:
return "N"
case 22.5 ..< 67.5:
return "NE"
case 67.5 ..< 112.5:
return "E"
case 112.5 ..< 157.5:
return "SE"
case 157.5 ..< 202.5:
return "S"
case 202.5 ..< 247.5:
return "SW"
case 247.5 ..< 292.5:
return "W"
case 292.5 ..< 337.5:
return "NW"
case 337.5 ... 360.0:
return "N"
default:
return ""
}
}
}

The heading from the delegate can be accessed as either .magneticHeading or .trueHeading

Magnetic Heading

The value in this property represents the heading relative to the
magnetic North Pole, which is different from the geographic North
Pole. The value 0 means the device is pointed toward magnetic north,
90 means it is pointed east, 180 means it is pointed south, and so on.
The value in this property should always be valid.

True Heading

The value in this property represents the heading relative to the
geographic North Pole. The value 0 means the device is pointed toward
true north, 90 means it is pointed due east, 180 means it is pointed
due south, and so on. A negative value indicates that the heading
could not be determined.

CoreLocation Location does not update when heading is updating

The location has not been updating because the method has been updated in ios 6.0

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* location = [locations lastObject];

This is how the didUpdateLocations should look like, now it takes an array.
[locations lastObject] this is the last know location in the array. From it you can get the coordinates and else.



Related Topics



Leave a reply



Submit