CLLocationManager not getting current location all the times - ios7

In my iOS 7 app, I have to get current location every time the app launches. I have kept the
following code.
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
[locationManager startUpdatingLocation];
I have Added CLLocationManagerDelegate in .h I am getting current location in this delegate
method but not all the times.
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
What could be the problem? Why some times this delegate method was not called? Any suggestions will be appreciated. Thank you

Related

Current Latitude Longitude on Google Map

I am using below code to get current lat long in iOS 8.
.h
#interface DirectionViewController : UIViewController<CLLocationManagerDelegate,GMSMapViewDelegate>
{
CLLocationManager *locationManager;
}
#property (nonatomic, retain) CLLocationManager *locationManager;
.m
#synthesize locationManager;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *latitude = [NSString stringWithFormat:#"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:#"%f", coordinate.longitude];
NSLog(#"dLatitude : %#", latitude);
NSLog(#"dLongitude : %#",longitude);
But here, I am getting 0.00000 value for both latitude and longitude.
Can anybody help me here. How to get current lat long in iOS 8?
Thanks in advance!
AppDelegte.h
#interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate>
{
CLLocation *currentLocation;
}
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (nonatomic, assign) CLLocationCoordinate2D currentLocationCoordinate;
Appdelget.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
locationManager = [[CLLocationManager alloc]init]; // initializing locationManager
locationManager.delegate = self; // we set the delegate of locationManager to self.
locationManager.desiredAccuracy = kCLLocationAccuracyBest; // setting the accuracy
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CurrentLatitude=newLocation.coordinate.latitude;
CurrentLongitude=newLocation.coordinate.longitude;
NSLog(#"%f",CurrentLatitude);
NSLog(#"%f",CurrentLongitude);
[locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to Get Your Location" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
.Plsit add
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
You're not using the proper method to get your coordinates.
once you call startUpdatingLocation, it calls the location manager methods (that you must implement in order for this to work).
There are two of them, [locationManager:didFailWithError:][1]
and [-locationManager:didUpdateLocations:][2]. You should have warnings asking you to add those methods in your .m, with the proper spelling if you haven't done it yet.
didUpdateLocation is called every X seconds and returns coordinates. You have to build your coordinate strings in this very specific method, where the coordinates are set.
From what I could gather,
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
Will give you an array of CLLocation objects containing the location data.
This array always contains at least one object representing the current location.
If updates were deferred or if multiple locations arrived before they could be delivered, the array may contain additional entries.
The objects in the array are organized in the order in which they occurred. Therefore, the most recent location update is at the end of the array.
This means you'll have to get the lastObject of that array for the most recent location.
Here is an example i've found on the web, for what the inside of that method could look. :
CLLocation *newLocation = [locations lastObject];
CLLocation *oldLocation;
if (locations.count > 1) {
oldLocation = [locations objectAtIndex:locations.count-2];
} else {
oldLocation = nil;
}
NSLog(#"didUpdateToLocation %# from %#", newLocation, oldLocation);
MKCoordinateRegion userLocation = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1500.0, 1500.0);
[regionsMapView setRegion:userLocation animated:YES];
Make sure to ask for the different permissions beforehand, and that you have them.
Also note that this method is only available since iOS5 and is different in the previous versions. Though it is very unlikely that you still support iOS5, I thought I should mention it.

cllocationmanager not asking for user permission ios8

Here is my code :
if (!_locationManager)
{
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
}
[_locationManager startUpdatingLocation];
please help if someone knows.. Thanks
You need to add a call to requestWhenInUseAuthorization when using location services for iOS-8.0 and above. Find the sample code below.
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
// this check is required for iOS 8+
// selector 'requestWhenInUseAuthorization' is first introduced in iOS 8
if ([locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
For more information see this blog.
Hope this helps.
Add [locationManager requestAlwaysAuthorization]; to your code and add the entry NSLocationAlwaysUsageDescription to your .plist with the value being some sort of message that you want to ask the user for their permission.

Can we show user location in offline in MKMapview

Can we get current user location "That blue ball which is animating" when our device is offline.when i tried to get current user location and log it i'm getting 0.00000 for both longitude and latitude here is the code that i used to get current user location.I'm using ipad mini to test it.I have also added CLLocationManagerDelegate in .h file.
- (void)viewDidLoad {
[self getUserLocation];
self.myMapView.showsUserLocation = YES;
}
-(void)getUserLocation{
if ([CLLocationManager locationServicesEnabled]) {
locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[locationManager startUpdatingLocation];
}else{
NSLog(#"User location Disabled");
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentGPSLocation = newLocation;
if (currentGPSLocation != nil) {
currentLocation.latitude = self.myMapView.userLocation.coordinate.latitude;
currentLocation.longitude = self.myMapView.userLocation.coordinate.longitude;
currentLocationWalk.latitude=self.myMapView.userLocation.coordinate.latitude;
currentLocationWalk.longitude=self.myMapView.userLocation.coordinate.longitude;
NSLog(#"didUpdateToLocation: %f,%f", self.myMapView.userLocation.coordinate.latitude,self.myMapView.userLocation.coordinate.longitude);
statusLabel.textColor = [UIColor blackColor];
statusLabel.text = [NSString stringWithFormat:#"%f",self.myMapView.userLocation.coordinate.latitude];
statusLabel1.text = [NSString stringWithFormat:#"%f",self.myMapView.userLocation.coordinate.longitude];
}
}
You are not using ARC (I strongly suggest using it, it avoid most of the memory management errors!).
Therefore it might be that "locationManager" is a instance variable that is not retained (maybe you did not declare it as a retained property).
In this case, it might have been released already when your code returns to the main run loop, where the autorelease pool is drained.
This line
locationManager = [[[CLLocationManager alloc] init] autorelease];
is not a good idea.
define the locationManager as a property,
init the locationManager in viewDidLoad(), but remove the autorelease!
and relase the locationManager only when the viewController is unloaded (there where you release the other properties)

CLLocationManager not updating location

Problem is location manager is not updating location.
The (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLoc fromLocation:(CLLocation *)oldLoc functions gets called but it displays the new location as the same old location.
Following is my piece of code:
In my viewDidLoad method i am creating the object of CLLocationManager
-(void) viewDidLoad
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
// created a timer to call locationUpdate method
[NSTimer scheduledTimerWithTimeInterval:20 target: self selector: #selector(locationUpdate) userInfo: nil repeats: YES];
}
-(void)locationUpdate
{
[self.locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLoc
fromLocation:(CLLocation *)oldLoc
{
NSLog(#"in locationmanager did update %f",newLoc.coordinate.latitude);
MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance(newLoc.coordinate, 0.01, 0.02);
[self.mapView setRegion:region animated:YES];
[self.locationManager stopUpdatingLocation];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.02);
MKCoordinateRegion region = MKCoordinateRegionMake(mapView.userLocation.coordinate, span);
[_mapView setRegion:region animated:YES];
[_mapView regionThatFits:region];
}
The value which i am getting in NSLog(#"in locationmanager did update %f",newLoc.coordinate.latitude) is always the same even-though i had moved for more than 2 kilometers starting from current location.
Please help me to how to get exact new location whenever there is a locationupdate. Thanks in advance.
You are stopping the location manager with
[self.locationManager stopUpdatingLocation];
in
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLoc
fromLocation:(CLLocation *)oldLoc
. This delegate method gets called EVERY time the user moves and can give you old (cached) data at the beginning, so by stopping it right away when you get your first location fix you probably get a cached location each time. The fix is simple, just don't stop the location manager there but rather when your viewController disappears or at a similar useful place.

didUpdateToLocation called multiple

i have an iOS application witch uses the current location of the user. I am doing like this :
-(void)startGeoloc{
NSLog(#"start geoloc");
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy=kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate methods
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[locationManager stopUpdatingLocation];
AppDelegate *apDelegate =(AppDelegate *)[UIApplication sharedApplication].delegate;
apDelegate.modeGeoloc = YES;
[self callWebService:locationManager.location];
}
The problem of this, is that my method callWebService:locationManager.location is called twice and i would like to call it just one time. how i can i do this ? thanks for your answers
To ensure locationManager only calls the "didUpdate..." method once, use a BOOL to reference if the location has been found yet.
Create the ivar BOOL:
#property BOOL didFindLocation;
Before locationManager startUpdatingLocation, set the new BOOL to NO. That way you can call for a new location update at will.
-(void) startFindingLocation {
self.didFindLocation = NO; // like this
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self]
[locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
[locationManager startUpdatingLocation];
}
In "didUpdateLocations", check for it.
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
if (!self.didFindLocation) {
self.didFindLocation = YES;
[locationManager stopUpdatingLocation];
// do the rest of your stuff
}
}
If possible, do not set didFindLocation anywhere else in your code to avoid confusion.
locationManager delegate methods can be called very frequently (they didUpdateToLocation all the time, right? :)
One way would be to have your callWebService have state, know whether it is currently executing a request and ignore concurrent requests if one is still going. Another way would be to keep a timestamp and only allow it through if 2 minutes has passed since the previous one.
Had the same problem.
I think the easiest solution is setting the CLLocationManager to null.
locationManager = nil;
after calling
[locationManager stopUpdatingLocation];
locationManager.startUpdatingLocation() fetch location continuously and didUpdateLocations method calls several times,
Just set the value for locationManager.distanceFilter value before calling locationManager.startUpdatingLocation().
As I set 100 meters(you can change as your requirement) working fine, and will work for you.
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 100
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()