How to get location for one time in ios - ios

How to fetch the location for only one time in ios app.

After getting your location, stop update location manager and also release locationManager because you don't need locationManager anymore.
[self.locationManager stopUpdatingLocation];
self.locationManager.delegate = nil;
self.locationManager = nil;

In ios 9 and above we have a method [locationManagerInstance requestLocation]. But this will take almost ten secs to call back the delegate methods since the location is latest and best.
In another way (for earlier ios9 versions), you could still use the old method [locationManagerInstance startUpdatingLocation] to get the immediate location and also you could validate the timestamp for the best accuracy.
Here is the code that I'm using for getting the location. Create a global reference for locationManager instance.
#interface ViewController ()
{
CLLocationManager *locationManager;
}
Implement these utility methods in your.m file
-(BOOL)canUseLocationManager{
if(([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) || ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways)){
return YES;
}
return NO;
}
-(void) getCurrentLocation {
locationManager.delegate = self;
// ios 9 and above versions only
//[locationManager requestLocation]; // This may take more time when compare to alternate method
// ios 2 and later versions can use this method
[locationManager startUpdatingLocation];
}
-(void) stopGettingCurrentLocation {
[locationManager stopUpdatingLocation];
}
Implement location service delegates
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if([CLLocationManager authorizationStatus] != kCLAuthorizationStatusNotDetermined){
[self doneCheckingAccessStatusForLocation];
}
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
[manager stopUpdatingLocation];
locationManager.delegate = nil;
/* Clean the locationManager instance if you don't need*/
//locationManager = nil;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
[manager stopUpdatingLocation];
locationManager.delegate = nil;
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
/* Implement your business logics here */
/* Clean the locationManager instance if you don't need*/
//locationManager = nil;
}
Final methods to create the location manager instance and initiate fetching location
-(void)checkForLocationService{
if(!locationManager){
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
locationManager.delegate = self;
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
[locationManager requestWhenInUseAuthorization];
}else{
[self doneCheckingAccessStatusForLocation];
}
}
-(void)doneCheckingAccessStatusForLocation{
if([self canUseLocationManager]){
[self getCurrentLocation];
}
}
call the checkForLocationService method and implement your logics on the success and failure delegate methods.
[self checkForLocationService];

Related

Location Update Issue iOS

I know there is a lot of questions related to that,But I cant able to link with my Issue. In my app,I am fetching the Nearby Restaurant,Initially,If user clicks on 'Nearby' button I fetch the lat,long,placemark details,Using this code below.
-(void)setUpUserLocation
{
BOOL locationAllowed = [CLLocationManager locationServicesEnabled];
if (locationAllowed==NO)
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"No authorization"
message:#"Please, enable access to your location"
delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Open Settings", nil];
[alertView show];
}
else
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
if ([locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
}
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location=[locations lastObject];
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = placemarks[0];
NSDictionary *addressDictionary = [placemark addressDictionary];
}];
[self stopSignificantChangesUpdates];
}
- (void)stopSignificantChangesUpdates
{
[locationManager stopUpdatingLocation];
locationManager = nil;
}
My Question is, If the user location changes, how do I give the user an alert like Your location changed, you need to update it then only you can get a Nearby Restaurant Location popup should come once the user location changes otherwise don't want to call the didUpdateLocation everytime. I called startMonitoringSignificantLocationChanges then it did not called the didUpdateLocation in first time itself.Any Help on this.
check after some time or check after user's movement(whatever you want),if condition is true then call service...
I think it help you;
Step 1
(void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
//set the amount of metres travelled before location update is made
[locationManager setDistanceFilter:100];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[locationManager requestAlwaysAuthorization];
// call the timer with 5 minutes cap using 5 * 60 = 300
[NSTimer scheduledTimerWithTimeInterval:300.0f target:self selector:#selector(sendlocation1) userInfo:nil repeats:YES];
Step 2
Every 100 Meter change Device This Method is called :
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(#"%f",newLocation.coordinate.latitude);
NSLog(#"%f",newLocation.coordinate.longitude);
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
if(meters >=100)
{
// call webservice for location is updated
[self sendlocation1];
}else
{
// call normal method
}
}

Code Not going into DidupdateLocation() method, iOS, objective -c, going into didfailwithError()

here is my code :
if (!self.locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
}
if([CLLocationManager locationServicesEnabled]){
[self findCurrentLocation];
}
-(void)findCurrentLocation {
if ([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
[self.locationManager performSelector:#selector(requestAlwaysAuthorization)];
}
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
self.isLocationEnabled=YES;
self.currentLocation = newLocation;
if (currentLocation!= nil) {
latitudeValue = [NSString stringWithFormat:#"%.8f",currentLocation.coordinate.latitude];
longitudeValue = [NSString stringWithFormat:#"%.8f",currentLocation.coordinate.longitude];
}
[self.locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
if([error code] == kCLErrorDenied)
{
[locationManager stopUpdatingLocation];
}
self.isLocationEnabled=NO;
}
i am not getting any errors, but didUpdateToLocation is never called, it is going into didFailWithError ()
I am getting null values for latitude and longtitude. Please let me know what can be done to get values.
from iOS8 and above:
Add a key to your Info.plist and request authorization from the location manager asking it to start.
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
You need to request authorization for the corresponding location method.
[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]
Please Read this Documentation: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

iOS GPS Location Tracking

I am programming in Objective C on Xcode and seem to be having a problem when my app goes into the background. I have a GPS tracking app and it works fine when the user has the app running but when it goes in the background the GPS doesn't follow the user location step by step. It pauses and then when the app re-opens, it automatically finds the location but I want the GPS to be running all the time so it won't "go off road"1 like I have in the picture attached.
I have location updates and background fetch turned on already in background modes.
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations: (NSArray *)locations
{
if (!self.timerPause) {
for (CLLocation *newLocation in locations) {
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (fabs(howRecent) < 10.0 && newLocation.horizontalAccuracy < 20)
other code
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;
self.locationManager.distanceFilter = 10; // meters
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
locationManager.allowsBackgroundLocationUpdates = YES;
Add this where you have set up the location manager. It should work :)

iOS 8 : Mapkit error throws an : Must call -[CLLocationManager requestWhenInUseAuthorization]

In iOS 8.3 with xCode 6.3.2, when I'm launching Map, it throws the following error:
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
I have done everything and follow every step to launch a map in ios8 app, here is my code for this:
//viewcontroller default function set mapview and a function to find user current location.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self otlMapView] setShowsUserLocation:YES];
self.otlMapView.delegate = self;
[self updateUserCurrentLocation];
}
//this function is used to fine the user current location.
-(void)updateUserCurrentLocation
{
// Create a location manager
self.locationManager = [[CLLocationManager alloc] init];
// Set a delegate to receive location callbacks
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}
// Start the location manager
[self.locationManager startUpdatingLocation];
[self.locationManager requestWhenInUseAuthorization];
}
#pragma mark Location Callback Method
// Wait for location callbacks
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(#"Current Location%#", [locations lastObject]);
}
What am I doing wrong?
You should make sure to call
[self.locationManager requestWhenInUseAuthorization];
before calling
[self.locationManager startUpdatingLocation];
Also, have you set up your Info.plist?
I assume you've correctly set up your Info.plist already. Then try something like this:
- (void)viewDidLoad {
[self updateUserCurrentLocation];
}
- (void)updateUserCurrentLocation {
if (nil == locationManager) {
locationManager = [[CLLocationManager alloc] init];
}
locationManager.delegate = self;
// This part was added due to a location authorization issue on iOS8
// See more at: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
if ([self._locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[self._locationManager requestWhenInUseAuthorization];
}
self.mapView.showsUserLocation = YES;
[locationManager startUpdatingLocation];
CLLocation *currentLocation = locationManager.location;
if (currentLocation) {
PAWAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.currentLocation = currentLocation;
}
}
Check if you have this key in your info.plist,
"Privacy - Location Usage Description",
if yes remove this and retain the "When in use" key. it should then work.

Calling and releasing CLLocationManager With ARC

My app should get the user's current location only one time.
It's doing it successfully using the location manager and a delegate. Once i get the location for the first time my delegate calls the manager to stopUpdatingLocation.
Since I'm using ARC, and I keep an instance of this class during all the time my app runs, my CLLocationManager instance stays in memory.
I know GPS services are quite power consuming, but does it have the same effect while i actually don't consume more events? does it keep on working?
I want to know if i should add some logic to release it.
Try Following...
-(void)updateLoc
{
if (self.locationManager != nil)
{
[self.locationManager stopUpdatingLocation];
self.locationManager.delegate = nil;
self.locationManager = nil;
}
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
self.locationManager.headingFilter = 1;
self.locationManager.distanceFilter=10.0;
[self.locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{//Use if you are supporting iOS 5
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
}

Resources