usinuniverse
usinuniverse

Reputation: 760

Can I adjust the frequency of locationManager (_: didUpdateLocations :)?

In the simulator, this method is executed only once.

However, when tested with a real mobile phone, it runs as many times as it is at the same location.

I want to control the frequency of this method.

Or is it possible through the distanceFilter?

What should I do if possible?

I suspect that the locationManager (_: didUpdateLocations :) method is automatically run whenever the location changes, so I can not control it.

Is there a way?

import UIKit
import CoreLocation

class MainViewController: UIViewController, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()


    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        // my source code
    }
}

Upvotes: 0

Views: 2263

Answers (4)

Cameron Deardorff
Cameron Deardorff

Reputation: 46

Don't use the function func locationManager(_ manager: CLLocationManager, didUpdateTo newLocation: CLLocation, from oldLocation: CLLocation) it has been deprecated since iOS 6.

deprecated method

Setting the desired accuracy should fix the problem of getting many updates with relatively the same point. For tracking with a little less precision maybe try

locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters

If you are concerned with long running tracking tasks/jobs then consider implementing

func locationManagerDidPauseLocationUpdates(CLLocationManager)

func locationManagerDidResumeLocationUpdates(CLLocationManager)

They will inform you the the location is not changing and to save power the device will shut down some hardware.

CLLocationMangager

CLLocationManagerDelegate

Upvotes: 2

Mike Carpenter
Mike Carpenter

Reputation: 410

Locations don’t change with the simulator. You’ll only get a single call from the didupdatelocations callback. Running on a real device will cause the completion handler to continuously fire. You can simulate a location with a GPX file but I don’t believe it fires more that once using the simulator.

Desired accuracy does change the frequency of firing but the frequency also depends on the GPS hardware and battery life status. iOS control the frequency to a certain point to optimize device performance and longevity

Upvotes: -1

Shiv Jaiswal
Shiv Jaiswal

Reputation: 559

I am not sure that you can control It. You can use func locationManager(_ manager: CLLocationManager, didUpdateTo newLocation: CLLocation, from oldLocation: CLLocation) { } method. This method gives you last location and new location and on the behalf of these locations, you can perform your desired function.

Or you can refer this answer click here

Upvotes: 0

Tejas Ardeshna
Tejas Ardeshna

Reputation: 4371

You can set location accuracy like

self. locationManager.desiredAccuracy = kCLLocationAccuracyBest

See https://developer.apple.com/documentation/corelocation/cllocationaccuracy

Upvotes: 0

Related Questions