CLLocationManager updates location when user select location button - iphone

In my application ,in initial lunch i have to used cllocationmanager class to find current location ([locationManager startUpdatingLocation];) using this method.After find a location i
fired this method ([locationManager stopUpdatingLocation];).
Now a user will move from one place to other place.It will automatically detects newlocation or we need to fire ([locationManager startUpdatingLocation];) this method again.
Help me?
Note:
If i will call ([locationManager startUpdatingLocation];) method again it will give a newlocation or old location.How to differentiate it ?

Whenever you call [locationManager startUpdatingLocation];, this will give you the current location. So, when you call this function again it will give you new location.

In such case, either you have to continuously keep on watch for the updated location, if you want automatic update or after certain duration of time you have to check again for updated location and compare it with previous one. By doing this, you will come to know weather is it new location or old.

Short answer: YES, you need to call [locationManager startUpdatingLocation]; to get a new location. You can stop and then start and then stop and then start, and each time you start, you will get the current (new) location of the device.
Long answer: You can also choose to keep the locationManager on (never call stopUpdatingLocation) and get updates all the time. By setting the distanceFilter property for the locations manager, you can choose when (or, to be exact, at what distance delta from last update) a new location update will be sent to your app. This seems to be the way used in most implementations that take more than one location sample, or need to track the location of the user constantly.
Also note, there is a new startMonitoringSignificantLocationChanges which gives different way to get updates: read the documentation carefully if you want to use this feature, it is not always available.

Related

didUpdateLocations will call on stable phone or now?

I want to know that locationManager:didUpdateLocations: will call when my phone is in stable position?
In my case its not calling when i put my phone on one place.
I Placed my phone from one location to other location within my room but still its not calling.
Can anyone tell me that when it will call.
When i change location using xcode then its always calling.
I used
self.locMgr.desiredAccuracy=kCLLocationAccuracyBest;
Okay, so you say within the simulator it works, but it does not work when you run your program on the actual iPhone device, right?
You were correct in setting the accuracy. This should work actually unless you have switched off the location update in general for your iPhone (settings under Privacy->Location Services).
If this is all working you maybe want to paste more of your code, but again, I think if it works under the simulator it should work on the device.
The location manager calls for location updates on the accuracy you set. It will not get called on a stable location more than once. you can use distance filter to call it after a certain distance.
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 10; //meters
For updating it on the same location you will have to use a timer which will call for updating after every desired interval.
Probably it depends on the accuracy you set to the locationManager.
1).. Cell Radio 2).. WiFi Map, 3).. GPS
If you set best as accuracy the location manager will continue to
check you position, if the location with better accuracy is out of the
range of the distance filter the delegate method will be called again.
GPS is not exact.some time if you move about a feet and still it will
not update. or some time you didn't moved at all and it will update.
Stop testing GPS and Location Services inside! Go outside and move around to test it.
Make sure that you set the delegate for the Location Manager & also
make sure you get the permission for using "User's Current Location."

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

IOS Location found detection

Currently I'm building an application which shows nearby advertisements based on the current location. When the user starts the application for the first time I want them to see a top 10 of nearby advertisements.
I'm having a slight problem with fetching the location on time. My problem is that the current location isn't fetched yet before the view is loaded.
Of course there is the delegate UpdatedLocation, but this one gets fired multiple times. I guess it fires multiple times, because it wants to fetch the most accurate location?
So my question is; What can I do to 'wait' until the location is found and then start searching for advertisements?
Thanks in advance!
Regards,
Mittchel
You should update the view every time you get a (more accurate) location. So remember the last location and check whether horizontalAccuracy/verticalAccuracy have decreased (smaller = more accurate, but negative = invalid). Maybe even updating every time you get a new location would be better since the accuracy might not change but the position does (user is moving).
If you just show a list, you should remember the previous results. Fetch the new result and if they differ, update your list.
When CLLocationManager gets an updated hit from the Core Location system, it fires its delegate's didUpdateToLocation:fromLocation: method. Inside that method, you want to do whatever you do when you get an updated location.
In your case, you'll want to check that new location's .horizontalAccuracy property to see that it's a high quality result. You also probably want to chuck the first few, because CL will give you the last hit of the last location session as your first hit of the new one.
Either way, that's the method where you want to do your fetching of data.
There is no one location, you get a series of locations from CL, one cached and then others varying in accuracy over time, getting better (or worse) accuracy, taking anywhere from 1 second to 10 minutes.
CL will start by giving you a cached location, which may be good enough for your purposes. If it's not too old (<60 seconds) and reasonable accuracy, go ahead and use the cached location, it will be the fasted result. Otherwise you have to wait for a sufficiently good accuracy (you have to decide what is sufficient, look at .horizontalAccuracy).
If you insist on having the location before viewDidLoad is called (viewWillAppear would be a better place) then you have to hold off pushing that viewController until you have a sufficient location. How you do that depends on how that view controller is being loaded.
Here is what I would do. Load your view, and in your viewDidAppear, call your method to update location. I have been using a nice open source project on github called MBProgressHUD. It will display a variety of progress indicators on the screen (that will also block the user from leaving your view) while it finishes its work. It also gives a visual feedback to the user that your app hasn't stalled and is working.
You will still need to sort out your accuracy issues, but the progress view gives you time to load the view and keep the user engaged while you pull in what you need. And keeps your UI fluid.

GPS Location takes a few seconds to be retrieved

I have written an iPhone app that uses the iPhone's relative GPS location.
I have a button in the user interface that does reverse geolocation (pulls the street name bases on the lat and long). The only problem is that the location object as retrieved by
CLLocation *location = [[self locationManager] location];
is nil if the user taps the button too soon. I am initializing the LocationManager in viewDidLoad as I don't want to object to be created unless the user actually loads this screen. I start updating the location straight away as well... but still if the user loads the screen and taps the GPS button straight away the location is nil.
Here comes the question: do you know roughly how much time the CLLocationManager needs to retrieve the location?
Thanks
It can take the phone a few seconds to retrieve this data. For usability, you may want to deactivate the button control. Fire an NSNotification once the location is found. Register your view controller to listen for this notification, which fires a selector that re-activates the button control.
Acquiring a location fix is dependent on the amount of accuracy you want, and whether it's previously had some kind of location data, what the weather is like, whether you're inside/outside ... there really isn't one answer. If you go to the Maps application and click on the 'locate' button then you'll notice that in different circumstances, it will be some time after you start the resolution process.
The callbacks will tell you when there is data; you shouldn't get it by polling or assuming instant access (or worse, blocking the app until you expect to find data). It sounds like the key problem here is your button is synchronously executing the request and not being dynamic; in other words, a race condition guaranteed to fail at some point, regardless of what value of 'n' you come up with.
Change your code so that the button invokes the location acquisition in the background, and then subsequently pops up the information or fills out the form later.

mkmapview.userslocation property set to 0,0 after allowing app to use current location

On the first time the user tries to use the current location the map.userslocation property has coordinates of 0,0. The next time I load the view containing the map the users location is always found.
I am setting the map.showsUserLocation property to YES on viewDidLoad and when the map loads on the screen the blue dot is there but when I try to get the coordinates with map.userlocation.coorinate they are 0,0.
The program flow goes
-(void) viewDidLoad:
1) set showUserLocation to YES
2) Set a timer to call a method 6 seconds later that grabs the users location from the map.userLocation property
-(void) getUsersLocation
1) print out the users location
This process works great after the first time I load the view, but the first time the view is laoded and the user has to grant permission to use their location the coordinates come back 0,0
Anybody have any idea why this happens and how I can work around it?
Thanks!
rather than arbitrarily waiting six seconds, you should be implementing CLLocationManagerDelegate and noticing when
locationManager:didUpdateToLocation:fromLocation:
is called to give you the first location update, which might take more or less time than that.
David is right, but I've noticed that I do sometimes get that method called with a location of 0,0. You should ignore any such calls, unless you think your app may be being used in the sea off the west coast of Africa.