iOS : Detect user location when App is back from Background - geolocation

I have a MKMapView & I use it to display user's current location. It works normal when I first load it in the App. But when I press Home button to put the App to background & call it back from background, the map prompts it is unable to find the user's location.
I was thinking is the MKMapView takes time to search for user's location, and when I call it back from background, it does not have enough time to load the location service ?
iOS5 SDK, xCode 4.3 is used.

If I understand your question correctly, your problem is that when your app resumes (becomes active), mapView.userLocation returns nil. Although I haven't tested this, I suspect it is because the mapView will need some time to relocate the user. You can get around this by adding your logic to the appropriate delegate method instead, which is - (void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation, if memory serves me right (you might want to verify that, though).
EDIT: Make sure you test location-related code on a device, since its behaviour might differ from the simulator.

Related

Blue banner '%MyApp% is Using Your Location' for app that uses location services only when active

In my app I'm updating user location every time when app becomes active. I stop CLLocationManager once updated location is received or in applicationWillResignActive:.
In Info.plist there is a NSLocationWhenInUseUsageDescription with appropriate description.
If app is activated and then immediately moved to background, blue banner saying that '%MyApp% is Using Your Location' appears for less then a second. This banner gets hidden as soon applicationWillResignActive: is called.
I've noticed the same problem in Google Maps, but not in Apple Maps.
Is there a way to get rid of this banner completely? Is there anything else I should do to make iOS happy? I do not want it to freak out my users and prevent them from using cool features that require location.
You have probably set location as Background mode in your info.plist. If you remove that, then the blue bar won't appear anymore.
Or, you can use requestAlwaysAuthorization instead of requestWhenInUseAuthorization on CLLocationManager (and provide the related NSLocationAlwaysUsageDescription in the info.plist file), and then it will not appear either.
In my case, my app is using location in the background in some cases, and I would like the blue bar to appear when that is the case. But I do not want it to appear when exiting the app without location in background. As far as I know that is not possible to achieve (I've asked about it here).
EDIT: Note that starting with iOS-9, you can make sure that the blue bar only appears when the user actually wants the app to use the location in the background. See this answer.
If you want to get access to the user's location with CLLocationManager, the app will need to show that banner and the user will need to press "Allow". If they don't do that, your location manager won't be able to detect the user's location.
Apple's Maps is a special case because it's a first party app.

Location updates issue for iOS 7

I'm developing an iOS map application, so it's essential to receive location data consistently.
What is happening now is that the system allows the to app load the map and that's it!
The location getting instruction seems to freeze, even the network traffic indicator at the top bar disappears. Simply when I go to Settings->Privacy->Location Updates, switch off location updates for my app and then switch it back on. The map loads the current location.
What could be the possible causes to this?, please advice.
Use:
[_mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
This will smoothly follow the users current location. If you drag the map it will even set the tracking mode back to MKUserTrackingModeNone which is usually the behaviour you want.

Location manager icon still displayed even when I call stopUpdatingLocation

Even though I called stopUpdatingLocation on my location manager, the location manager icon is still on the top right of my iPhone. Why hasn't it stopped?
There are several reasons why the location arrow on your phone may still be on even after calling stopUpdatingLocation.
For one, another app on your phone may currently be using your location.
Another possibility is that you enabled significant location change monitoring using the startMonitoringSignificantLocationChanges method but forgot to turn this off using the stopMonitoringSignificantLocationChanges method. Apps that are monitoring significant location changes will display a solid arrow in the status bar in the same way apps that are using standard location updates do.
Of course it's also possible that you are for whatever reason incorrectly calling stopUpdatingLocation and you actually are still getting location updates from a location manager. To check if this is happening wait several minutes after calling stopUpdatingLocation and go to Settings>Privacy>Location Services and find your app. If there is a solid purple arrow next to your app then your app is indeed currently using location services and something most likely went wrong when calling stopUpdatingLocation. However if there is a solid gray arrow, this just signifies that your app has recently used location services and it's likely that you did correctly stop getting updates. Another way to check if you correctly turned off location updates is by calling your CLLocationManager's location property and checking its timestamp property to make sure that it is not recent. If the timestamp is greater than the timestamp at which you called stopUpdatingLocation then you know something is wrong.
The icon you see at the top is not locationManager, is the location service of the device. If you deactivate in settings no longer appears.

Update MKMapView when app is in background

I have code in my iOS app that uses MKMapView to run certain code when the user moves:
- (void)mapView:(MKMapView *)mapView
didUpdateUserLocation:(MKUserLocation *)userLocation
{
// The code I want to run
}
I was wondering if it was possible to keep updating even if the app was in the background, or if not, how to replicate this code in CLLocationManager.
EDIT:
I need this code to get the position of the user in the background
I was wondering if it was possible to keep updating even if the app was in the background, or if not, how to replicate this code in CLLocationManager.
There's no point in updating a map view if the view isn't being displayed on the screen.
If you want to do something even when the map view isn't visible, you should use Core Location. All you need to do is to create an instance of CLLocationManager, give it a delegate, and tell it to start updating. Your location manager delegate can implement -locationManager:didUpdateLocations: to get location updates, and you can do whatever you like with the updated information.
Note that you shouldn't unnecessarily use location when the app is in the background. Read more about getting updates in the background in Getting Location Updates in the Background.

Google maps ios api using user's location in fitbounds

I am trying to set a bounds between the users location and a selected marker. I can set everything up properly but when I try to use the users location (mapView_.myLocation.coordinate) it says the lat and long are both 0. I did create a button to center the camera on the users location and it gets the lat and long just fine. Does not make any sense to me. Also I don't have a device set up to test so I am using xcodes iOS simulator with a location put into the simulator (is that where my problem is?). Any ideas?
Are you trying to access myLocation.coordinate from loadView or similar, ie when the app first starts up?
Generally when enabling location services the app doesn't receive a location update immediately, it comes through after some delay. So it's possible that myLocation.coordinate won't be valid in loadView, but will have a value set some time later (which is why it works with your button).
You could perhaps listen for changes to myLocation, and then set your bounds when it changes to something.

Resources