CLLocationCoordinate2D latitude & longitude always zero - ios

I am trying to get latitude & longitude. I am running following code on actual device but I always get zero for latitude & longitude. I also import library and set delegate also. May I know what is wrong and how to do? My device has ios 8.
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
if ([locationManager locationServicesEnabled])
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *str=[[NSString alloc] initWithFormat:#" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
NSLog(#"%#",str);

you have to get the current location in delegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations objectAtIndex:0];
}

Related

Offline Location Update is not working in iOS 14 and above

I am working on the Location update in my application
When the app is with internet connection location update (didUpdateLocation) is called. But when the app goes offline, Location update delegate is not called in iOS 14 . I am not getting the latitude and longitude.
Any idea why this is happening or is this the process of location manager?
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
if([locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]){
[locationManager requestWhenInUseAuthorization];
}else{
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
lattitude = [NSString stringWithFormat:#"%f", location.coordinate.latitude] ;
longitude = [NSString stringWithFormat:#"%f",location.coordinate.longitude];
[locationManager stopUpdatingLocation];
}

How to get current location latitude and longitude? [duplicate]

This question already has answers here:
How to get current location latitude and longitude in IOS SDK 8.0
(4 answers)
Closed 5 years ago.
I'm trying to get user current location.Here is my code:
-(CLLocationCoordinate2D) getLocation{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;
}
and this is where i call it in viewDidLoad
CLLocationCoordinate2D coordinate = [self getLocation];
double latitude = coordinate.latitude;
double longitude = coordinate.longitude;
and it return 0.000000 for both latitude and longitude.
Any help please! I'm using Xcode 8 and running on My real iPhone 6s not Emulator.
You can use this code:
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[self.locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
NSLog(#"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);
}

Not getting appropriate Latitude Longitude

I am a iOS application developer. I am working with Location Manager.
For every 5 seconds I am capturing the latitude and longitude and storing the CLLocation object in array. The lat and long is not completely correct when I am trying to draw those location in Map.
My code is following below:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{//ios6&+
CLLocation *loc;
NSInteger objCount = [locations count];
if (objCount >= 1) {
loc = [locations objectAtIndex:objCount - 1];
NSArray *Arr_TotalTime = [Str_TotalTime componentsSeparatedByString:#":"];
[ArrayOfLocations addObject:loc];
}
}
Any one can please give me more info to get ACTUAL location (lat, long) from didupdatelocation method.
Any help will be highly appreciated.
Depending on how you set up your location manager, the accuracy will vary.
Try something like this:
Location Manager
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
_locationManager.activityType = CLActivityTypeAutomotiveNavigation;
_locationManager.distanceFilter = kCLDistanceFilterNone;
Method
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject];
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if ((abs(howRecent) <= 5) && (newLocation.horizontalAccuracy > 0) && (newLocation.verticalAccuracy > 0) && (newLocation.horizontalAccuracy < 100) && (newLocation.verticalAccuracy < 100))
{
[ArrayOfLocations addObject:newLocation];
}
else
{
// received bad signal
}
}
You can use CoreLocation to get the longitude and latitude.
Include framework:
Click your project in navigator.
Click the plus button under "link binary with libraries"
Add Corelocation to your project.
Import the header file:
import
Declare CLLocationManager:
CLLocationManager *locationManager;
initialize locationManager:
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
}
Then, use
float latitude = locationManager.location.coordinate.latitude;
float longitude = locationManager.location.coordinate.longitude;

CLLocationManager fails to get the user's location in device but not in simulator

I'm using a CLLocationManager for a GPS tracking system in my iOS app. The app works fine with the debug feature of the simulator but once the build is in the device, it fails to do so. Giving me an error, "Failed to get location." This is how I did it:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.pausesLocationUpdatesAutomatically = NO;
then the startupdating is in a uibutton. Any help will be gravely appreciated. Thank you.
First setup the location manager
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
And after that call delegate method
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];
NSLog(#"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);
currentLocationCoordinate.latitude = location.coordinate.latitude;
currentLocationCoordinate.longitude = location.coordinate.longitude;
[[NSNotificationCenter defaultCenter] postNotificationName:#"Address Found" object:self];
}
this is work for me . it will help you

Can't enable the location in my app

I want to find my location in my app. And I use CoreLocationManager to get the coordinate, but when I launched the app,it will alert me weather to authorize the location privacy, and after I click the OK button, nothing happened.
Then I open the Settings-Privacy-location in my iPhone, I found my app had not get the location enabled. I turn it on, then I relaunch the app, still nothing happen?
Here is my code:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
}
the method -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations can't run after I set the breakpoint.
Put the following code to get your current latitude and longitude in yor device (Not in simulator).
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
latitude = [[NSString alloc] initWithFormat:#"%f", coordinate.latitude];
longitude = [[NSString alloc] initWithFormat:#"%f", coordinate.longitude];
NSLog(#"dLatitude : %#", latitude);
NSLog(#"dLongitude : %#",longitude);

Resources