One call of requestLocation() leads to several calls of delegate

Hi *,


I'm facing a problem with the requestLocation() call on iOS 12.

Normally one requestLocation() call leads to one

event for the locationManager( didUpdateLocations) delegate method.

Unfortunately, from time to time there are several events generated, thus the delegate method is called several times.


Is there a way to figure out the very last call of the delegate method? Using the timestamp associated with every location that is forwarded to the delegate method is not an option. That is based on the fact, that one call of the requestLocation() method will generate on event with an already cached location object with a timestamp that is before the time the requestLocation() call was made.


The code with requestLocation() call looks like:

@IBActionfunc getPosition(_ sender: UIButton) {
        sender.isEnabled = false
        calls = 0
        newHeading = nil

        clearUI()

        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.activityType = .other
        now = Date()
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "HH:mm:ssSSSS"
        let timeStr = dateFormatter.string(from: now)
        theCurrentTime.text = timeStr
       
        locationManager.requestLocation()
    }


Thanks in advance

Peter

Replies

Would the following work to provide you the most recent location ?


     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
          if locations.count == 0 { return }
          let newLocation = locations[locations.count - 1]
          locationManager.stopUpdatingLocation()
          currentLocation = newLocation
          print("Update location", currentLocation!)
     }


currentLocation is a property defined in the class

     fileprivate var currentLocation : CLLocation?

Unfortunately not!

The problem is, that the delegate method might get called several times for one requestLocation() call.

My map should respond to the "last" of these calls, the "correct" position. This position is sent to server.

If we are not able to find a solution we have to implement iOS specific software at the server based on the fact that we don't have these problems with the Android devices ...