How to detect location is settled iphone sdk - iphone

I have an application that gets the users location and draws a polyline on a map. At the start the app draws a line all over the place (where the gps trys to gain an accurate location). Is there any way to tell that the location is accurate and therefore start drawing.
i thought count and if statement that after say 5 location updates start- but should the user have a strong location then my app will fail.
Any Ideas?

Check out the CLLocation objects passed to your CLLocationManagerDelegate's
locationManager:didUpdateToLocation:fromLocation
method; they have, among other things, 2 properties available: horizontalAccuracy and verticalAccuracy.
They describe the radius of error, measured in meters.
http://developer.apple.com/library/ios/DOCUMENTATION/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html#//apple_ref/occ/instp/CLLocation/horizontalAccuracy

Related

locationManager:didEnterRegion: only getting called when moving from certain locations

I'm monitoring a region using CLLocationManager's startMonitoringForRegion:desiredAccuracy:. I know for sure that the region gets properly added to the location manager's monitoredRegions.
I set this up while my simulator location is set far away from it (2.3 km to be precise). Then I hit the home button, and change the simulator's location to the very location I set a geofence for. locationManager:didEnterRegion: doesn't get called. Same results if I try moving to this location but from a another location, this one 4.3 km away.
There seems to be some threshold I can't pick up, as locationManager:didEnterRegion: gets called when moving from other locations at 7 km and up. But my CLRegion's radius is 100 m. What could possibly be the issue here?
This is truly just a wild guess, but I wonder if this is a result of the location manager attempting to mitigate false positives with a location that is seemingly jumping around. I imagine that changing the location in the simulator while the app is resigned could appear to the location manager as a location update that doesn't conform to the desired accuracy, thus disregarding it when considering whether you've actually entered a new region.
Have you tried simulating a city bike ride or freeway drive to see if it makes a difference? I would just try to test on a device as soon as possible.

iPhone : How do I display the user location on a map view without updating location / having GPS running in the background?

how do I display the user location in a mapView while not running GPS to update the location (at all)? Will
mapView.userTrackingMode = kCLLocationAccuracyNearestTenMeters;
do the job or will GPS still burn battery in the background?
Thanks :)
I'm going to attempt to answer your question as best as i can given the limited information.
The GPS has 3 main modes that go from low power to high power, low accuracy to high accuracy.
Location Tracking off
Background location updates (uses cell towers and wifi)
GPS location updates
An MKMapView will show a blue dot for the user location if you set showsUserLocation to YES. The MKMapView will use all available location methods to find the users location as accurately as possible and keep updating it while this is set to YES.
The tracking in MKMapView, a mode which keeps the users location centred on screen, moves the visible map region as the user moves and is available in iOS5. you are given three MKUserTrackingModes to choose from. From the docs:
MKUserTrackingModeNone: The map does not follow the user location.
MKUserTrackingModeFollow: The map follows the user location.
MKUserTrackingModeFollowWithHeading: The map follows the user location
and rotates when the heading changes.
So setting it to kCLLocationAccuracyNearestTenMeters isn't going to work as it is not an available option in this context.
Will showing the location burn battery in the background? It depends on what you mean by background. When the user taps the home button the MKMapView is forced to stop using the GPS by the system. While the app is running and the MKMapView is alive and has the showsUserLocation property set to YES it will keep using any available method, including the GPS, to update the location. You have a couple of strategies to reduce this.
1) presume that the user needs to see their location updated while the map is on screen. In which case keep showsUserLocation set to YES until the map is moved off screen, or the device is locked, then set it to NO
2) Presume the user only needs to see a static marker of their location from a particular point in time, like when the app is opened. In this case you need to make a CLLocationManager object, ask it to startUpdatingLocation, filter the delegate messages for the accuracy you want, and then turn off location updates (stopUpdatingLocation). You can then add an MKAnnotation to the MKMapView to show the user their location.
As you can see number 2 is more work, yet very power efficient. Number 1 is easy, but will use more battery while the map is visible. It is up to you to decide which behaviour the user expects and to implement your app accordingly.

Retrieving current user location from mapView, without map being on screen

I'm writing an app that stores the location of the places you have been throughout the day. I've done a lot of research on CLLocationManager, and have been testing my app for the last 3 weeks. The locations I get with CLLocationManager sometimes is very inaccurate, sometimes 4 or 5 miles away from where I am.
I have a log in my test app and realized that every time I get a new location my DidUpdateToLocation method is called 3 times in a row, like within one second, and I get 3 different locations. One of them is always right, but the other 2 are off. I've tested with it with different accuracies(hundredMeters, nearestTenMeters, and Best), but still had the same problem. So, my first question is:
Is there anyway I can find out which of these locations is the right one so I can store it?
I've also realized that the user's current location on the map view is always very accurate, so I thought about getting the user's location from the mapView(blue dot) instead of the CLLocationManager, but I learned that a mapView object will only return the current location if the map is actually on screen, and this is not the case since I want my app to run in the background. So the second question is:
Is there anyway to get the user's current location from a mapView without the map being on screen? Or at least use the same technique the mapView uses for finding the current location?
If you have any experience with mapKit and CLLocationManager, please share your thoughts.
Thanks for you time. Any help is appreciated.
Is there anyway I can find out which of these locations is the right
one so I can store it?
Look at the horizontalAccuracy property of the locations that you receive. Don't use locations that are less accurate than what you're looking for.
I've also realized that the user's current location on the map view is
always very accurate
The map view probably doesn't have any special access to API's that are more accurate than what you've got, it just uses them better. Instead of starting from scratch each time you need a fix, try caching the location and then updating when you get a new location that's sufficiently accurate.
Take a look at Apple's sample code Locate Me. It uses the variable:
CLLocation *bestEffortAtLocation;
to determine upon updates if the location is less than the last location and less than a time limit set until the most accurate location is found.
In particular look at the GetLocationViewController in the sample code. I've used this code in my apps and it works great.
https://developer.apple.com/library/ios/#samplecode/LocateMe/Listings/Classes_GetLocationViewController_m.html#//apple_ref/doc/uid/DTS40007801-Classes_GetLocationViewController_m-DontLinkElementID_8

iPhone GPS - Battery Draining Extremely Fast

We are developing an app that has heavy GPS usage, and we are unable to optimize the battery life.
Even when the device is not moved, there is significant battery drainage that, according to the code, should not happen.
Here is the code:
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = 100;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
Ideally we want to trigger GPS every 20 minutes (if there is no location change then save battery) OR every 5 minutes if there is location change. According to my developer this cannot be done
Previously we were using kCLLocationAccuracyBest, which was consuming battery very fast and now we are using kCLLocationAccuracyHundredMeters.
startUpdatingLocation - is to get the GPS coordinates. There is another call startMonitoringSignificantLocationChanges which is to get AGPS coordinates which I believe returns the coordinates whenever cell tower will change, and hence consumes battery really fast.
distanceFilter - The minimum distance (measured in meters) a device must move laterally before an update event is generated. On the basis of distance filter we get the GPS fix from the device and then we send the updated GPS coordinates to the server.
Any help will be greatly appreciated
Thank you!
There is another call startMonitoringSignificantLocationChanges which
is to get AGPS coordinates which I believe returns the coordinates
whenever cell tower will change, and hence consumes battery really
fast.
That's exactly what it does, but you're jumping to conclusions about the power required for that. The GPS receiver and WiFi transceiver can be used to help determine location, but they're extra devices that have to be powered to be useful. But mobile phones like the iPhone need to keep in touch with the nearest cell tower anyway in order to receive phone calls, so using cell towers as a source of location information should be very efficient with respect to power. Here's what the CLLocationManager reference page says about -startMonitoringSignificantLocationChanges:
This interface delivers new events only when it detects changes to the
device’s associated cell towers, resulting in less frequent updates
and significantly lower power usage.
It also describes the service as providing "tremendous power savings," so it seems the right tool for the job you describe. Of course, if you're also using the standard location updating mechanism at the same time you won't see that power savings, so make sure you're not using both.
Ideally we want to trigger GPS every 20 minutes (if there is no
location change then save battery) OR every 5 minutes if there is
location change. According to my developer this cannot be done
It sounds like there's some sort of misunderstanding here. You can certainly fire up the GPS every 20 minutes to get a fix if that's what you want, although you can't do that from the background. The significant location change service will notify your app even if it's running in the background, so perhaps your developer is talking about background updates.
Your belief that startMonitoringSignificantLocationChanges consumes the battery really fast is likely incorrect. Cell tower changes usually occur far less frequently than movements of 100 meters. And an iPhone checks for this event even when idle and all apps are sleeping.
However, keeping a cell phone on (e.g. Airplane mode off) when far from any tower (barely 1 bar) will drain the battery of the device whether or not an app is doing any location monitoring.
On stock iOS devices, an app cannot trigger location monitoring purely on a timer basis such every 20 minutes or every 5 minutes, without keeping the device on and your app running in the foreground, which will drain the battery.
If you require a good level of precision, you should startUpdatingLocation and as soon you get the first fix you should then stopUpdatingLocation and then startMonitoringSignificantLocationChanges.
This is enough for most app purposes.
Ideally we want to trigger GPS every 20 minutes (if there is no
location change then save battery) OR every 5 minutes if there is
location change. According to my developer this cannot be done
This could be done using an NSTimer that calls a startLocating (a custom method that creates the CLLocationManager and calls startLocating on it) method every 20 minutes. Make sure you call CLLocationManager's stopLocating once you've found a CLLocation with an accuracy of your liking.
However, doing so mean that for 20 minutes you might be using a location that is totally off. It depends on how you plan to use the location and how precise it needs to be, but maybe what your dev means by "not possible" is that your app needs the user's actual location at all times.

MapKit - How to get accurate initial reading when first ones are off > 1000 meters sometimes?

As I've been playing with MapKit, I've noticed that when my application first launches I have drawn lines all over the place (I'm doing GPS tracking) which shows the initial readings have been way off.
My MapKit is implemented in it's own MapViewController and everything is initialized in the viewDidLoad method (ie. [locationManager startUpdatingLocation:YES])
Is there a good practice / method though where in the viewDidLoad you can fire off some ActionSheet or something which determines the your location to the best possible accuracy initially before presenting the map and your current location?
Don't draw a line until the location is returned with an accuracy narrower than half the width of the lines your app will be drawing. Or make the lines much fatter until.
Also, check the time stamp on the locations to make sure that they are recent ones.