Locationmanager Didupdatelocations Fires Twice on Device, Only Once on Simulator

locationManager didUpdateLocations fires twice on device, only once on simulator

Location Manager delegate methods can be called very frequently and at any time.

You may however, apply following algorithm to safeguard yourself:

  1. Create a global bool say didFindLocation.
  2. Set didFindLocation to false when you call startUpdatingLocation.
  3. Inside delegate call back didUpdateLocations:, if didFindLocation was false, set didFindLocation to true and then call stopUpdatingLocation.

Hope this helps.

didUpdateLocations called only once and then stops firing

Few tips for similar issues as per my experience with location-based programming on iOS.

  • If it works on simulator, then it should work on actual device as
    well.
  • Unlike simulator, the actual device will only give update when its receives new information from GPS.
  • There are many cases where you won't get enough/required updates on a device, like when you are in a building (or covered area)
  • Try testing the app in open space (GPS works much better in open space rather than room/house/large-building).

PS. I don't know how much you moved but just to let you know, moving few steps doesn't make sure you will get a new location update, try using it on some vehicle (bike/car/bus).

locationManager(_:didUpdateLocations:) last entry is sometimes extremely outdated

When Location Service can't get the current location by requesting GPS server.

It will return the cached/recent location. That is why you see its timestamp is 1 min or 1 hour ago.

It will return the cached/recent location.

This will help users to quickly access their current locations if they didn't travel significantly, that is when Accelerometer works to measure the movement of the device.

locationManager:didUpdateLocations: always be called several times

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.

didUpdateLocations gets called, but sends wrong location

I figured it out, thanks to the comments below other answers..

I was waiting too short before calling the [locationManager stopUpdatingLocation]; method. The GPS just needed some more time..

So i just waited before doing the stopUpdatingLocation with an NSTimer. That did the job. Thanks to gWiz!

locationManager didUpdateLocations crashing in suspend mode

It seems you are storing location details in Keychain. Accessing keychain when the app is in suspended state will not be always error free. It's behavior is totally weird for background cases. When Touch ID / Face ID is enabled in the device, it makes the case more complicated when the device is locked.

Let's say, your device has Touch ID / Face ID enabled and device is locked. Now, when the app receives location update, it will try to access keychain but the device is locked with Touch ID / Face ID enabled. This kind of combination will sometimes work and sometimes will not. Still, i am searching why this happens.

didUpdateLocations never called

where do you start the location update ? for example:

//location manager
lazy var locationManager: CLLocationManager = {
var _locationManager = CLLocationManager()
_locationManager.delegate = self
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
_locationManager.activityType = . automotiveNavigation
_locationManager.distanceFilter = 10.0 // Movement threshold for new events
_locationManager.allowsBackgroundLocationUpdates = true // allow in background

return _locationManager
}()

if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation() // start location manager

}

here is a working conroller code:
also important to to set up Custom iOS Target Properties.

Add these two lines to the Info.plist:

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

//
// ViewController.swift
// LocationTest2

import UIKit
import CoreLocation

class ViewController: UIViewController {

//location manager
lazy var locationManager: CLLocationManager = {
var _locationManager = CLLocationManager()
_locationManager.delegate = self
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
_locationManager.activityType = . automotiveNavigation
_locationManager.distanceFilter = 10.0 // Movement threshold for new events
// _locationManager.allowsBackgroundLocationUpdates = true // allow in background

return _locationManager
}()

override func viewDidLoad() {
super.viewDidLoad()
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

//allow location use
locationManager.requestAlwaysAuthorization()

print("did load")
print(locationManager)

//get current user location for startup
// if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
// }
}
}

// MARK: - CLLocationManagerDelegate
extension ViewController: CLLocationManagerDelegate {

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

for location in locations {

print("**********************")
print("Long \(location.coordinate.longitude)")
print("Lati \(location.coordinate.latitude)")
print("Alt \(location.altitude)")
print("Sped \(location.speed)")
print("Accu \(location.horizontalAccuracy)")

print("**********************")

}
}

}


Related Topics



Leave a reply



Submit