2

I've come up with some code that prints the address and postalcode of my location. This is done in the didupdatelocation function.

The only problem that I occur, is that every second this address get's updated by the "didupdatelocation" function. Because this is very battery ineffecient, I was looking for ways to use an interval but I couldn't find ways (also not on Google en stackoverflow) on how to do this.

Can anybody give me tips how I can do this in Swift?

rvano
  • 97
  • 7

5 Answers5

3

The answers of Rajeshkumar and Basir doesn't change much for the battery power, their delegate method is still called the same number of times per minute.

If you want to have a more efficient behavior, try to change the values of distanceFilter, desiredAccuracy, and activityType (You can find all you need here)

manager.distanceFilter = 50 // distance changes you want to be informed about (in meters)
manager.desiredAccuracy = 10 // biggest approximation you tolerate (in meters)
manager.activityType = .automotiveNavigation // .automotiveNavigation will stop the updates when the device is not moving

Also if you only need the location of the user when it changes significantly, you can monitor significantLocationChanges, can also work when your app is killed.

Damien
  • 3,322
  • 3
  • 19
  • 29
  • Hey Thanks for your reply! it really helps me, is there also any option which is battery efficient that doesn't work on meters but on seconds/minutes? Thanks in advance – rvano Mar 28 '17 at 19:10
  • significantLocationChanges update the user location if he moves more than 500 meters (if I remember well) and if that change occurs at least 5min after the last one. – Damien Mar 28 '17 at 19:44
  • Yeah but I really want to configure this myself, so every 20 seconds or what. Shouldn't be impossible, is it? – rvano Mar 28 '17 at 19:45
  • You don't want to do that cause if the user doesn't move for 24h you'll still drain his battery power.Is it possible ? I don't think so, maybe if you look in background modes, I didn't explore that a lot... – Damien Mar 28 '17 at 20:44
2

Try this

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [Any]) {
    var mostRecentLocation: CLLocation? = locations.last
    print("Current location: \(mostRecentLocation?.coordinate?.latitude) \(mostRecentLocation?.coordinate?.longitude)")
    var now = Date()
    var interval: TimeInterval = lastTimestamp ? now.timeIntervalSince(lastTimestamp) : 0
    if !lastTimestamp || interval >= 5 * 60 {
        lastTimestamp = now
        print("Sending current location to web service.")
    }
}
Basir Alam
  • 1,332
  • 20
  • 24
2

Swift 3 version of this answer

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    var newLocation = locations.last!
    var locationAge = -newLocation.timestamp.timeIntervalSinceNow
    if locationAge > 5.0 {
        return
    }
    if newLocation.horizontalAccuracy < 0 {
        return
    }
        // Needed to filter cached and too old locations
    var loc1 = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
    var loc2 = CLLocation(latitude: newLocation.coordinate.latitude, longitude: newLocation.coordinate.longitude)
    var distance: Double = loc1.distanceFromLocation(loc2)
    self.currentLocation = newLocation
    if distance > 20 {
        //significant location update
    }
    //location updated
}
Community
  • 1
  • 1
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
  • If you only need "significant" location updates, as a comment in that code mentions, use `startMonitoringSignificantLocationChanges` instead. – Tom Harrington Mar 28 '17 at 14:58
1

If you want to save as much battery as possible you can use start​Monitoring​Significant​Location​Changes instead of start​Updating​Location.

It will only notify you of changes over 500m and minimum interval of updates is 5 minutes.

You can read more from here.

Radu Diță
  • 13,476
  • 2
  • 30
  • 34
0

Try use

manager.pausesLocationUpdatesAutomatically = true

also try put biggest value on

manager.distanceFilter = value
UnRewa
  • 2,462
  • 2
  • 29
  • 31
  • Thanks, but I don't need it to be paused for ever. Just to make the interval from 1 to 30 seconds – rvano Mar 28 '17 at 10:03