Latitude and Longitude 0 when called second time in Objective-C - objective-c

I am using the below code to get coordinates on click of a button
-(CLLocationCoordinate2D) getLocation{
CLLocationCoordinate2D coordinate;
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
if ([CLLocationManager locationServicesEnabled]) {
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
coordinate = [location coordinate];
}
else {
coordinate.latitude = 0.0;
coordinate.longitude = 0.0;
}
return coordinate;
}
When the button is clicked for the first time, I do get my valid coordinates, but if I click the button again, The latitude and longitude values are 0.0000
Any suggestions

Please initialize the CLLocationManager outside the method.Take the You can initialize in viewdidload method.
It may helps to you

You cannot expect [locationManager location] to return anything useful immediately after activating a location manager with [locationManager startUpdatingLocation] .
CLLocationManager takes time to acquire a location. You need to set the CLLocationManager's delegate (eg, to 'self') and then use the delegate method - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations; to get notified when the location manager has got a location that you can use.
See: https://developer.apple.com/reference/corelocation/cllocationmanagerdelegate/1423615-locationmanager?language=objc
(If you only want one location, and not a continuous stream of location updates, instead of calling 'startUpdatingLocation' call [locationManager requestLocation] - but even then you still need to use the delegate method to be notified when a location has been acquired).
You need to split out the code for creating and starting the location manager to where it is only run once (only need one location manager for the app) - eg, viewDidLoad. Then the rest of the code, to process the locations acquired, should go in the delegate method.
In reality, if you want you button click to have a location immediately, you need to have your location manager running and updating locations before your button is available to be clicked. Eg, make your button enabled=NO and when you start getting locations via the delegate method, set the button enabled=YES.

Related

objective c find out my location?

I'm trying to get my location in simulator. A couple of time it worked but... Now I always get 0.000000 as my location. There is code that I used:
#import <CoreLocation/CoreLocation.h>
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSLog(#"%f",location.coordinate.latitude);
NSLog(#"%f",location.coordinate.longitude);
After running app I change in the simulator Debug -> Location -> Custom Location... Does somebody can explain why does it stop working?
This will never work, except by accident:
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
When you call startUpdatingLocation you get delegate messages (assuming you have a location manager delegate, which you should, always) — in particular, locationManager:didUpdateLocations:. That is where you receive information about your location.

Deferring Location Updates in Background

I have a app which collects location data in foreground and background. To save battery i want to use the allowDeferredLocationUpdatesUntilTraveled property as explained in Apple Doc
From the documents, allowDeferredLocationUpdatesUntilTraveled gets set(based on time and distance) in foreground. Once app goes in the background we won't receive regular location updates instead we receive them based on the allowDeferredLocationUpdatesUntilTraveled.
I have implemented following code but the deferring does not get called in the background.
#pragma mark - Location Manager
-(void) setLocationManager
{
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(#"didUpdateLocations");
if (!deferringUpdates)
{
CLLocationDistance distance = 200;
NSTimeInterval time = 60;
[locationManager allowDeferredLocationUpdatesUntilTraveled:distance timeout:time];
deferringUpdates = YES;
}
}
-(void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error
{
NSLog(#"didFinishDeferredUpdatesWithError");
}
SetLocationManager gets called in the foreground and works fine. Once app goes in the background i still receive regular location updates rather than allowDeferredLocationUpdatesUntilTraveled.
I have also set the following values in the info.plist file
Background Fetch
Background Location Updates
Device Capabilities - location service
Anyone luck with implementing this?
You need to set deferringUpdates back to NO within didFinishDeferredUpdatesWithError : as per Apple documentation, the "delegate’s locationManager:didFinishDeferredUpdatesWithError: method is called exactly once for each time you call (allowDeferredLocationUpdatesUntilTraveled)".
Thus allowDeferredLocationUpdatesUntilTraveled is only called once until deferringUpdates is NO.

CLLocationManager updates only on Simulator Not on Devices

Here we have to update the current location by using the location manager,and its correctly updating on simulator but its not updating on device only 4 or 5 times only repeated.why the location is not updated frequently kindly hep me to solve this problem
Here i used code is showed below
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[locationManager startUpdatingLocation];
Delegate Method:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog(#"newLocation: %#",newLocation);
}
This is the delegate method is calling four or five times.
If you are using location accuracy as 'kCLLocationAccuracyBestForNavigation', your device should remain plugged in.
Better you set it as 'kCLLocationAccuracyBest'. Also move to significant distance to hit the delegate. initially it will hit 4-5 times to locate you. Once your location is identified, it will call the delegate when there will be change in your location.
So, in delegate write something in file stored in documents directory in append mode. log the lat/long and Time details into it. Now move for some distance.
Join the mobile and get the file from documents directory. It must have logg the required details.
Here i solved this problem using this delegate method
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations{
}

CoreLocation , MapKit Error

I Tried To Show My Current Location In Mapkit and this is code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Make this controller the delegate for the map view.
self.MapView.delegate = self;
// Ensure that you can view your own location in the map view.
[self.MapView setShowsUserLocation:YES];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}
and I wrote This Delegate Of Mapkit
#pragma mark - MKMapViewDelegate methods.
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
MKCoordinateRegion region;
region = MKCoordinateRegionMakeWithDistance(locationManager.location.coordinate,2000,2000);
[mv setRegion:region animated:YES];
}
But I Get Wrong Location Iam In Egypt And This Give ME iam in UnitedStates
Any One Can Hep ME ???
Just in case: if you are using the simulator, the position will not be your real position. You have to set your location in the simulator:
Debug -> Location -> Custom Location (or anything else)
First of all.
You didnt used
[locationManager startUpdatingLocation];
Please add this line after you initiate the location manager variable. Also please let me know if you are seeing this location USA in Simulator.
If it is: Your simulator simulates the location given by user preferences. Get to the simulator window, there you'll get the option to show custom location as per your selection. By default it is selected as USA.
Hope it resolves your issue.

ios CLLocationManager won't get location second time

I have a getLocation method that initializes my CLLocationManager and calls startUpdatingLocation. After I get the user's location, I call stopUpdatingLocation.
Later, when the user presses refresh, I call a method reloadData which calls getLocation so I can get the user's latest location. However, this never calls the locationManager:didUpdateToLocation:fromLocation
method.. so I never get the user's latest location. What could be the issue?
-(void) getLocation {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}
- (void) locationManager: (CLLocationManager *)manager
didUpdateToLocation: (CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (oldLocation!= nil) { // make sure to wait until second latlong value
[self setLatitude:newLocation.coordinate.latitude];
[self setLongitude: newLocation.coordinate.longitude];
[self.locationManager stopUpdatingLocation];
self.locationManager.delegate = nil;
self.locationManager = nil;
[self makeUseOfLocationWithLat:[self getLatitude] andLon:[self getLongitude]];
}
}
-(void) reloadData {
[self getLocation];
}
Is it really necessary to allocate a new CLLocationManager? Try just allocate it once (in your init for example) and just call startUpdatingLocation and stopUpdatingLocation on demand.
For me, this solution works great.
Do you move during testing this? Because I think the callback will only be triggered, when:
you call startUpdatingLocation
your location changes
the location manager gets better results (more detailed)
So I think for your use-case: as you don't move, the callback locationManager:didUpdateToLocation:fromLocation: will only be called once after you hit refresh and as there is no old location, you will not go into the if case.
Another thing: you should definitely have only one CLLocationManager in your class. I only use one per project/application (wrapped in a singleton). This is the preferred way to do! Also I think this will retain the oldLocation value so that your problem may be resolved by changing this.
Best,
Christian
you can try this ,this will help u to update for every time when open the page.Cause if u put startUpdatingLocation in viewdidload ,it will get load only for the first time
-(void)viewWillAppear:(BOOL)animated
{
[locationManager startUpdatingLocation];
}