I am monitoring the users location with these configurations:
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
locationManager.requestAlwaysAuthorization()
locationManager.startMonitoringSignificantLocationChanges()
I have a switch that allows the user to either enter an address manually or allow for GPS tracking. When toggling from manual to GPS location, I'd like to kick the CLLocationManager to update the users location and call the didUpdateLocations event. Otherwise, it just waits and waits to update when it feels like it. Is manually triggering an update one time possible?
from the docs:
startUpdatingLocation
Calling this method several times in succession does not automatically result in new events being generated. Calling stopUpdatingLocation in between, however, does cause a new initial event to be sent the next time you call this method.
so you could stop and immediately restart the location updates to trigger it manually!
Is manually triggering an update one time possible
Yes, but not with startMonitoringSignificantLocationChanges. You will need to call startUpdatingLocation. Note that even then it may take several calls to your delegate, and several seconds, before you get your single usable update.
What I do (example code here) is to watch both the horizontalAccuracy and the timestamp, and stop when I either get a sufficiently accurate location or too much time elapses.
Start it when you want to start scanning for updates, but not when you want an immediate location.
If you want an immediate location, request it with manager.location. If the response is nil then no location is currently available and you need to wait. If a location is returned you can check the timestamp to determine if you can use it.
Related
to retrieve location of the user when the app is in the background i first do startMonitoringSignificantLocationChanges then when DidUpdateLocations is fired i do with a second CLLocationManager startUpdatingLocation to retrieve the exact geoposition (with an accuracy on 100m)
The problem is that the doc say:
Calling this method causes the location manager to obtain an initial
location fix (which may take several seconds). After that, the receiver generates update events primarily when the value in the distanceFilter property is exceeded.
So as i understand the first DidUpdateLocations will be called with a fix not really accurate, and with time other DidUpdateLocations will be called with more accurate location.
Now the problem is, when (and how?) did i need to stop the startUpdatingLocation ? i m in the background so i can't use any timer or think like this
Since you require a 100m accuracy, you can simply check the location horizontal accuracy property. If the condition is fulfilled you can stop the core location manager otherwise you keep tracking.
Be aware that usually the callback of significant monitoring changes keeps your app alive for a minimum amount of time.
At wake-up time, the app is put into the background and you are given
a small amount of time (around 10 seconds) to manually restart
location services and process the location data. (You must manually
restart location services in the background before any pending
location updates can be delivered, as described in Knowing When to
Start Location Services.) Because your app is in the background, it
must do minimal work and avoid any tasks (such as querying the
network) that might prevent it from returning before the allocated
time expires. If it does not, your app will be terminated. If an iOS
app needs more time to process the location data, it can request more
background execution time using the
beginBackgroundTaskWithName:expirationHandler: method of the
UIApplication class.
I want to track the user location in background, in the purpose to show him an alert when he is close to one of his friend.
So i start with CLLocationManager. As far as i know their is only one reliable way to let the app know about the location update even if the user reboot the Iphone or kill the app: startMonitoringSignificantLocationChanges. But the problem is that even inside a city with many wifi, startMonitoringSignificantLocationChanges fire the DidUpdateLocations when the user move around 1km and that is really too much for my need
on the other way startUpdatingLocation is firing DidUpdateLocations at good interval (even too much because even when the user do not move it's fire quite often DidUpdateLocations). But startUpdatingLocation not survive to iphone reboot or app being killed by the user. Also I suspect that even with an accuracy of 100m, startUpdatingLocation use lot of battery consumption.
So the question: What strategy i can use in my app to track efficiently without draining too much the battery the user location at full time? I need an accuracy of around 100m and if possible an interval between 2.5 - 5 min for each track (i didn't find any option to specify a delay to wait before to catch a new location)
Actually i think to do something like this :
2 locationManager, 1 GPS and 1 Significant Changes
when app start I do with significantChangesLocationManager: startMonitoringSignificantLocationChanges and startMonitoringVisits
I also call GPSLocationManager startUpdatingLocation to retrieve the accurate user position. I set up PausesLocationUpdatesAutomatically(true) so that the GPSLocationManager will stop by himself soon or late
on DidUpdateLocations raise by the GPSLocationManager I start monitoring region enter/exit (100m radius around the obtained latitude/longiture) with significantChangesLocationManager
What do you think of such strategy ?
Even though you will receive more triggers than you need, as you already said, you can use startMonitoringSignificantLocationChanges. It is implemented in a very energy efficient way. It allows the app to be terminated and only be woken up again when iOS thinks the device has moved significantly. Another advantage would be that your app doesn't need the location background mode, which could raise questions during an app review.
The startUpdatingLocation let's the app continuously update the location of the device, even though you only receive a couple didUpdateLocations: events. Also, iOS cannot shutdown the app while updating is active, so it consumes a lot of battery.
You can also consider geofencing, with an exit geofence around the current location. However, significant location updates will be more reliable. Exit geofences won't trigger anymore once you're already out of a geofence, which could happen when the phone is turned off inside a geofence and turned back on outside. This solution has the same advantage of not needing a background mode.
As far as I understand your use case, startMonitoringSignificantLocationChanges sounds as the best option. You won't have control over the exact time interval and distance it triggers, but it is very energy efficient and easy to use.
As mentioned in comments, here are some approach I had to do in order to get the expected result in our app.
Significant Location Updates - is good to wake your app up once it gets finished by some reason that you can't control, so even if you lose some points, it will get back in some seconds/minutes.
This is not good if you need a good accuracy, as you cannot control the distance / time or whatever, iOS will update locations when mobile antenna is changed, wifi connection, turn air plane mode off and on but will not give a sequence of gps points.
Start Location Updates - Best accuracy but battery drainer. So you can't just turn it on let it go. You must implement some controls over it.
Background tasks - The only way I've found to keep my app alive.
The way you can combine is:
Have 2 location managers isolated, one for significant location change and one for start updating location;
Start your app with both turned on;
Inside your didUpdateLocations you can create your logic to start and stop your background tasks;
Create methods to start and pause your location manager and create your timers to control that;
inside your bg task you will start or pause your update locations, but never stop it, just if for example, your user logs out and you want to stop location;
keep significant location update location manager alive forever, if for some reason iOS decide to kill your app when in bg, it will ensure that in a given moment your app will come back to life;
For battery live, try do not use kCLLocationAccuracyBestForNavigation for accuracy as when it starts it eat a LOT of resources, most of time, kCLLocationAccuracyBest is more then enough and if you can use kCLLocationAccuracyNearestTenMeters;
Your didUpdateLocations will give you mostly a bunch of points, try to get only those with good horizontalAccuracy, around 20 meters for us was good enough, so let it work and once you have a point with good accuracy, you can pause it again.
Below you have some links that helped me a lot to implement our solution, none of those have a "as is" solution, as I said, I had to mix all of them and test a lot to understand its behaviour and make the necessary adjustments.
https://github.com/voyage11/Location
http://mobileoop.com/background-location-update-programming-for-ios-7
http://zaachi.com/2013/09/30/ios-locationmanager-location-update-in-my-own-interval-with-application-in-the-background.html
In addition to Mark's answer I would like you to explore pausesLocationUpdatesAutomatically which can help you save battery from draining when user has stopped Apple Documentation
Allowing the location manager to pause updates can improve battery life on the target device without sacrificing location data. When this property is set to true, the location manager pauses updates (and powers down the appropriate hardware) at times when the location data is unlikely to change. For example, if the user stops for food while using a navigation app, the location manager might pause updates for a period of time. You can help the determination of when to pause location updates by assigning a value to the activityType property.
I'm working with CLLocationManager class. I want to get location updates periodically. I had found two methods to get location in didUpdateLocations method, that are startUpdatingLocation() and startMonitoringSignificantLocationChanges(). If I have to track location updates in foreground mode also then which method should I use?
The most important difference between the 2 is this:
startMonitoringSignificantLocationChanges: it does not rely on the value in the distanceFilter property to generate events. The receiver generates update events only when a significant change in the user’s location is detected
startUpdatingLocation : the receiver generates update events primarily when the value in the distanceFilter property is exceeded
So, if you want more precision, go for startUpdatingLocation, at the expense of more battery consumption, but more precision of the location. It really depends on your goal, you should evaluate the tradeoff.
startMonitoringSignificantLocationChanges initiates the delivery of location events asynchronously, returning shortly after you call it. Location events are delivered to your delegate’s locationManager:didUpdateLocations: method. The first event to be delivered is usually the most recently cached location event (if any) but may be a newer event in some circumstances. After returning a current location fix, the receiver generates update events only when a significant change in the user’s location is detected. Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification.
To sum up startMonitoringSignificantLocationChanges will provide you with location only when location changes by some significant amount which is roughly around 500 meters or after some fixed time say 5 minutes. Where as startUpdatingLocation will provide you with location based on distanceFilter property set. Default value of distanceFilter is kCLDistanceFilterNone which reports all the movements.
startUpdatingLocation returns immediately. Calling this method causes the location manager to obtain an initial location fix (which may take several seconds) and notify your delegate by calling its locationManager:didUpdateLocations: method. After that, the receiver generates update events primarily when the value in the distanceFilter property is exceeded. Updates may be delivered in other situations though. For example, the receiver may send another notification if the hardware gathers a more accurate location reading.
I want to get the location updates for every 30 secs.In foreground and when the app is in background.Also how could I know that my location is updating or not if I use simulator.please provide some help.
You don't, you get location update when the device moves. Location update are not really meant to be polled, but rather you are notified when there is a new location available.
Just create a instance of CLLocationManager and start call the startUpdatingLocation method. Then the delegate of the CLLocationManager will start receiving location updates. Just be aware that if you keep this running for a long time it will drain the devices battery faster.
I am wondering if iOS system restarts locationUpdates of CLLocationManager automatically after being paused automatically by the pausesLocationUpdatesAutomatically property or if I have to handle the case by my own.
If yes, according to which rules and if not how to know that I start moving again and should relaunch locationUpdates ?
If you set
yourLocationManager.pausesLocationUpdatesAutomatically = YES;
you should also set a value to
yourLocationManager.activityType
"The location manager uses the information in this property as a cue to determine when location updates may be automatically paused. Pausing updates gives the system the opportunity to save power in situations where the user's location is not likely to be changing. For example, if the activity type is CLActivityTypeAutomotiveNavigation and no location changes have occurred recently, the radios might be powered down until movement is detected again."
UPDATE: changed Apple documentation
"
After a pause occurs, it is your responsibility to restart location services again when you determine that they are needed. Core Location calls the
locationManagerDidPauseLocationUpdates(_:)
method of your location manager's delegate to let you know that a pause has occurred. In that method, you might configure a local notification whose trigger is of type UNLocationNotificationTrigger and is set to notify when the user exits the current region. The message for the local notification should prompt the user to launch your app again so that it can resume updates."