didUpdateHeading not called in swift - swift

I am trying to get the heading information of the device in order to use the difference between the magnetic and true heading to determine the declination for the user location. For this, I have a Helper class and my MainVc (mvc). In my helper class init method I do the following:
...
...
locationManager = CLLocationManager()
switch CLLocationManager.authorizationStatus() {
case .AuthorizedAlways:
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
case .NotDetermined:
locationManager.requestAlwaysAuthorization()
case .AuthorizedWhenInUse, .Restricted, .Denied:
mvc.alertDenied();
}
if (!initialized)
{
initialized = true;
self.performSelector("finishUpdatingLocation", withObject: nil, afterDelay: 2.0);
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print ("didUpdateLocations")
var userLocation:CLLocation = locations[0] as! CLLocation
longitude = Float(userLocation.coordinate.longitude);
latitude = Float(userLocation.coordinate.latitude);
}
func finishUpdatingLocation () {
locationManager.stopUpdatingLocation();
NSObject.cancelPreviousPerformRequestsWithTarget(self, selector: "finishUpdatingLocation", object: nil);
mvc.goingToUpdateHeading();
locationManager.startUpdatingHeading();
}
didUpdateLocations is being called and I am successfully fetching the device's coordinates. However although I have added didUpdateHeading, the first line in it is not being printed, the device's stuck at that point:
func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
print("didUpdateHeading");
if (newHeading.headingAccuracy > 0) {
variation = newHeading.trueHeading - newHeading.magneticHeading;
doneLoading = true;
locationManager.stopUpdatingHeading();
mvc.goingToCalculateData();
if (calcData) {
calculateData();
print ("Calculating data");
calcData = false;
}
}
print(newHeading.magneticHeading)
}
Any ideas why the startUpdatingHeading is not being called?

You have to call locationManager.startUpdatingHeading() like
...
...
locationManager = CLLocationManager()
switch CLLocationManager.authorizationStatus() {
case .AuthorizedAlways:
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
locationManager.startUpdatingHeading()
case .NotDetermined:
locationManager.requestAlwaysAuthorization()
case .AuthorizedWhenInUse, .Restricted, .Denied:
mvc.alertDenied();
}

If you are checking it on simulator then it will not work please check it on real device.

Related

didUpdateLocations only can called once

func startLocationManager() {
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.startUpdatingLocation()
}
}
After getting location authorization, I called this method in viewDidLoad(), and then thefunc locationManager(_:, didUpdateLocations:) starts to be called.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let lastLocation = locations.last!
if lastLocation.timestamp.timeIntervalSinceNow < -5 {
return
}
if lastLocation.horizontalAccuracy < 0 {
return
}
var distance = CLLocationDistance(Double.greatestFiniteMagnitude)
if let location = location {
distance = lastLocation.distance(from: location)
}
if location == nil || location!.horizontalAccuracy > lastLocation.horizontalAccuracy {
location = lastLocation
if location.horizontalAccuracy <= locationManager.desiredAccuracy {
stopLocationManager()
getWeather()
}
} else if location == nil || distance < 1 {
let timeInterval = lastLocation.timestamp.timeIntervalSince(location!.timestamp)
if timeInterval > 5 {
stopLocationManager()
getWeather()
}
}
}
And this is how I end updateLocation:
func stopLocationManager() {
locationManager.stopUpdatingLocation()
locationManager.delegate = nil
}
Finally, I got the location data I wanted perfectly.
But when I want to re-acquire the new location in other ways, that is, it has executed locationManager.startUpdatingLocation(), and still cannot execute func locationManager(_:, didUpdateLocations:).
I want to know how to fix this.
Don't set locationManager.delegate = nil when you stop updating your location. Or if you do that, set it back to self when you want to start getting location updates again.
Remove this
locationManager.delegate = nil

How to access user coordinates

I am trying to access the users coordinates using:
import MapKit
import CoreLocation
override func viewDidLoad() {
super.viewDidLoad()
let locationManager = CLLocationManager()
let userCoordinates = (locationManager.location?.coordinate)!
}
However, it crashes upon the simulator loading. I set the simulator location to Apple, and input privacy keys into info.plist, but I am not sure why this is not grabbing the user location.
There are a few things you need to do first before you can start using a device's current geolocation safely and based on the code you provided I'm going to assume that you might be missing some so here is a common set up using a Google Map, which acts like any other basically:
class YourViewController: UIViewController, CLLocationManagerDelegate {
// properties
var locationManager = CLLocationManager()
var currentCoordinate: CLLocationCoordinate2D?
// load view
override func loadView() {
addLocationManager()
}
// add location manager
func addLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
// location manager delegate: did change authorization
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .restricted:
print("LOCATION ACCESS RESTRICTED")
case .denied:
print("LOCATION ACCESS DENIED")
case .notDetermined:
print("LOCATION ACCESS NOT DETERMINED")
case .authorizedAlways:
fallthrough
case .authorizedWhenInUse:
print("LOCATION STATUS GRANTED")
}
}
// location manager delegate: did fail with error
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
locationManager.stopUpdatingLocation()
print("LOCATION ACCESS ERROR: \(error)")
}
// location manager delegate: did update locations
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let lastLocation = locations.last!
// unhide map when current location is available
if mapView.isHidden {
mapView.camera = GMSCameraPosition.camera(withLatitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude, zoom: 18, bearing: 0, viewingAngle: 0)
mapView.isHidden = false
}
// update current location properties
currentCoordinate = lastLocation.coordinate
}
}
And no need to import CoreLocation if you've imported MapKit.

CLLocation + Weather(Alamofire) issues

I am trying to use CLLocation to capture longitude and latitude and then use the longitude and latitude in Alamofire to get weather. Every time, the longitude and latitude won't stop updating and the weather data won't print(if you wanna check it out here's an example link of the data: http://forecast.weather.gov/MapClick.php?lat=37.33233141&lon=-122.0312186&FcstType=json)
class SampleViewController: UIViewController, CLLocationManagerDelegate {
var locationManager:CLLocationManager!
var startLocation: CLLocation!
var isFetchingWeather = false
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
}
override func viewDidAppear(_ animated: Bool) {
getCurrentLocation()
}
func getCurrentLocation(){
if CLLocationManager.locationServicesEnabled(){
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var userLocation:CLLocation = locations[0]
if isFetchingWeather != false{
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")
let requestLink = "http://forecast.weather.gov/MapClick.php?lat=\(userLocation.coordinate.latitude)&lon=\(userLocation.coordinate.longitude)&FcstType=json"
print(requestLink)
Alamofire.request(requestLink).validate().responseJSON
{ response in
switch response.result {
case .success(let data):
let json = JSON(data)
self.weatherData = json["data"].arrayValue
for weather in self.weatherData{
let temp = weather["weather"].stringValue
self.weatherString.append(temp)
}
print (self.weatherString)
if self.startLocation == nil {
self.startLocation = userLocation as! CLLocation
self.locationManager.stopUpdatingLocation()
}
case .failure(let error):
print(error)
}
}
}
else{
print("is fetching weather is false")
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
}
Thanks.
You really shouldn't run your weather request inside your location delegate. Instead, get your location in the didUpdateLocations delegate and save it to a var. Next, call stopUpdatingLocation() then call a separate function to make your weather request. Something like this:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last
//check accuracy and timestamp of location to make sure its not a cached/old location (if you don't care about accuracy or time, you can remove this check)
let timeDiff = newLocation?.timestamp.timeIntervalSinceNow
if timeDiff < 5.0 && (newLocation?.horizontalAccuracy)!<=self.accuracyNeeded{
//stop updating location
self.locationManager.stopUpdatingLocation()
//set currentUserLocation
self.myLocation=newLocation?.coordinate
//call function to get weather
//remove delegate
self.locationManager.delegate = nil
}
}
Set a flag to indicate when you start fetching weather info and do not call Alamofire to fetch weather information if that flag is set. For example, you would declare something like after the line where you declare startLocation:
var isFetchingWeather = false
Then, in locationManagerdidUpdateLocations first check if isFetchingWeather is false. If not, return. Otherwise, fetch the weather info.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if isFetchingWeather {
return
}
isFetchingWeather = true
// Do the actual weather fetching
}
Of course, you might want to do the actual fetching of the weather after you've gotten a few location updates since the initial ones might not be that accurate :)

Core Location returns nil when asked for location

My private constant in my Custom class:
private let locationManager = CLLocationManager()
in viewDidLoad() I call the method:
private func setupLocationManager() {
locationManager.delegate = self
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
But when I ask about location at some time later:
println("-----\(locationManager.location)")
I get:
-----nil
Where is the problem? I run this on device. My app is allowed to use location...
You should not try to get location that way because getting location can take some time (for more information see GPS TTFF). Since your location manager already has a delegate you must get location in didUpdateLocations delegate method. Also always make sure you have the permission to use location before calling startUpdatingLocation
private func setupLocationManager() {
locationManager.delegate = self
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways{
locationManager.startUpdatingLocation()
}else{
locationManager.requestWhenInUseAuthorization()
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){
println(manager.location)
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.AuthorizedWhenInUse || status == CLAuthorizationStatus.AuthorizedAlways {
locationManager.startUpdatingLocation()
}
}

locationManager:didUpdateLocations: always be called several times

I start updating current location when the view did appear, and stop updating location whenever locationManager:didUpdateLocations: is called. But why the locationManager:didUpdateLocations: always be called several times? What have I missed?
#import "ViewController.h"
#interface ViewController (){
CLLocationManager *locationManager; // location manager for current location
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self startUpdatingCurrentLocation];
}
- (void)startUpdatingCurrentLocation
{
if (!locationManager)
{
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
locationManager.distanceFilter = 10.0f; // we don't need to be any more accurate than 10m
}
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
[locationManager stopUpdatingLocation];
}
#end
Probably it depends about the accuracy you set to the locationManager. You have 3 kinds o localization Cell Radio, WiFi Map, GPS. If you set best as accuracy the location manager will continue to check you position, if the location with better accuracy is out of the range of the distance filter the delegate method will be called again.
SWIFT version
i made a helper class as HelperLocationManager and added a notification- observer pattern
import UIKit
import CoreLocation
class HelperLocationManager: NSObject {
var locationManager = CLLocationManager()
static let sharedInstance = HelperLocationManager()
var currentLocation :CLLocation?
var notGotUserLocation = true
override init() {
super.init()
var code = CLLocationManager.authorizationStatus()
if code == CLAuthorizationStatus.NotDetermined {
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
}
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
locationManager.distanceFilter = 100;
}
}
extension HelperLocationManager: CLLocationManagerDelegate{
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var locValue = locations.last as! CLLocation
println(locValue)
self.currentLocation = locValue
NSNotificationCenter.defaultCenter().postNotificationName("sendCurrentAddressToViewController", object:self.currentLocation)
notGotUserLocation = false
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("Your error is ", error.localizedDescription)
}
}
Now if your Viewcontroller class needs the location then put an observer there
var helperLocation:HelperLocationManager?
in viewDidLoad as
override func viewDidLoad() {
helperLocation = HelperLocationManager()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "getCurrentAddressToViewController:", name: "sendCurrentAddressToViewController", object: nil)
}
//and observer as
func getCurrentAddressToViewController(notification: NSNotification) {
currentLocation = notification.object as? CLLocation
NSNotificationCenter.defaultCenter().removeObserver(self, name: "sendCurrentAddressToViewController", object: nil)
}
//although didUpdateLocation is called multiple times you only get one time location because of removing observer after you get the location.
EDIT: I refractored this helper class so that you dont need to add notificationobserver pattern
class HelperLocationManager: NSObject {
private lazy var locationManager = CLLocationManager()
static let sharedInstance = HelperLocationManager()
var currentLocation :CLLocation?
override init() {
super.init()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
}
}
extension HelperLocationManager: CLLocationManagerDelegate{
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case CLAuthorizationStatus.NotDetermined:
locationManager.requestWhenInUseAuthorization()
case CLAuthorizationStatus.Restricted:
PrinterHelper.messagePrinter("Restricted Access to location")
case CLAuthorizationStatus.Denied:
PrinterHelper.messagePrinter("User denied access to location")
case CLAuthorizationStatus.AuthorizedWhenInUse:
if #available(iOS 9.0, *) {
locationManager.requestLocation()
} else {
locationManager.startUpdatingLocation()
}
default:
PrinterHelper.messagePrinter("default authorization")
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue = locations.last
HelperLocationManager.sharedInstance.currentLocation = locValue
locationManager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
PrinterHelper.errorPrinter(error.localizedDescription)
}
}
View Controller where you need to get user Permission
var helperLocationManager:HelperLocationManager?
in viewDidLoad as
override func viewDidLoad() {
helperLocationManager = HelperLocationManager.sharedInstance
}
And to get the location you need to call the singleton property currentLocation as
if let userCurentLoc = HelperLocationManager.sharedInstance.currentLocation{
//userCurrentLoc is the user Location
}
To complement on Anish's answer, if you wanna know when your helper class didn't call the location update (i.e when you have turned off your location services), you can manage this using the locationManager:didChangeAuthorizationStatus: method, and if your location is not allowed, you can call another notification- observer
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
var shouldIAllow = false
switch status {
case CLAuthorizationStatus.Restricted:
locationStatus = "Restricted Access to location"
case CLAuthorizationStatus.Denied:
locationStatus = "User denied access to location"
case CLAuthorizationStatus.NotDetermined:
locationStatus = "Status not determined"
default:
locationStatus = "Allowed to location Access"
shouldIAllow = true
}
if (shouldIAllow == true) {
print("Location to Allowed")
// Start location services
locationManager!.startUpdatingLocation()
} else {
print("Denied access: \(locationStatus)")
NSNotificationCenter.defaultCenter().postNotificationName("deniedLocation", object:locationStatus)
}
}