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.
- Conform to the
CLLocationManagerDelegate
protocol - Instantiate an instance of
CLLocationManager
and set the delegate as self - You do not need user permissions if you just need the heading (and not actual user location)
- Tell the
CLLocationManager
to start updating the heading - The heading will be reported back to the delegate method
didUpdateHeading
- Use a switch statement to find cardinal direction based on the input degrees.
- 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
Creating Semaphore with Initial Value of 0 Make Issues with Execution
How to Hide The Top Bar (With Buttons) Usin Swift and Macos
How to Bind My Array Controller to My Core Data Model
How to Resolve Rctpromiseresolveblock After Bftask
Multi-Users Chat Room Data Structure in Firebase Database
Swift - Rotate Gesture and Rotation Increments of 90 Degrees
Why Extensions Cannot Add Stored Properties
Swift: Right/Left Click on Nsstatusbar
Realitykit - Updating Entity's Translation Returns Unexpected Values
How to Get Buffer from Avaudioengine's Installtap at High Frequency
Execute Code on Main Thread from Async F#
Understanding UIviewrepresentable
Applescript Used in My Cocoa MAC App, Stopped Working in Osx 10.14
Mac App Disappears When Window Is Closed Then Another App Is Selected
Spritekit Skscene Not Resizing Correctly to Fit iPhone 12
How to Use Protocols for Stucts to Emulate Classes Inheritance