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);
}
Related
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];
}
I am using Location Manager. Its acting very strange. I have these UILabels in place and I am looking for away to consistently update these UILabels when the coordinates change. This is what I have in place:
#implementation ViewController
{
CLLocationManager *locationManager;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
locationManager = [[CLLocationManager alloc] init];
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
currentLocation = nil;
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
and then this:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *) newLocation
fromLocation:(CLLocation *)oldLocation
{
/*if (currentLocation == nil)
currentLocation = newLocation;
if (currentLocation != nil) {
Longitude = [NSString stringWithFormat:#"%.10f", currentLocation.coordinate.longitude];
Latitude = [NSString stringWithFormat:#"%.10f", currentLocation.coordinate.latitude];
LongitudeDouble = currentLocation.coordinate.longitude;
LatitudeDouble = currentLocation.coordinate.latitude;
}*/
currentLocation = newLocation;
Longitude = [NSString stringWithFormat:#"%.10f", currentLocation.coordinate.longitude];
Latitude = [NSString stringWithFormat:#"%.10f", currentLocation.coordinate.latitude];
LongitudeDouble = currentLocation.coordinate.longitude;
LatitudeDouble = currentLocation.coordinate.latitude;
Long.text = [NSString stringWithFormat:#"%.10f", LongitudeDouble];
Lat.text = [NSString stringWithFormat:#"%.10f", LatitudeDouble];
}
My issue is when I run the app is updates the labels sometimes, but after a bit, its stops and when I close the app and reopen it the labels do not update at all.
What Am I doing wrong?
May be you should add this line self.locationManager.distanceFilter = 0.1
And fix your code like this
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = 0.1
[locationManager startUpdatingLocation];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
currentLocation = nil;
}
Try this
viewDidLoad:
locationManager =[[CLLocationManager alloc] init] ;
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];
Info.plist:
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
Then
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(#"failed to update location");
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *currentLocation=[locations lastObject];
lat=currentLocation.coordinate.latitude;
lon=currentLocation.coordinate.longitude;
NSString *strLat=[NSString stringWithFormat:#"%f",lat];
NSString *strLon=[NSString stringWithFormat:#"%f",lon];
latitude_1=[[NSMutableString alloc] initWithString:strLat];
longitude_1=[[NSMutableString alloc] initWithString:strLon];
}
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];
}
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;
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