Latitude & Longitude Not Retrieved - ios

UPDATED for iOS9: I am new to iOS, experimenting with CoreLocation to return latitude and longitude values. The following code never executes the log output
-(void)viewWillAppear:(BOOL)animated{
manager = [[CLLocationManager alloc]init];
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
[manager startUpdatingLocation];
}
Here is my didUpdateToLocation function with is never reached
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(#"Location: %#", locations);
CLLocation *currentLocation = locations.lastObject;
if (currentLocation != nil) {
float latitude = currentLocation.coordinate.latitude;
float longitude = currentLocation.coordinate.longitude;
NSLog(#"dLongitude : %f", longitude);
NSLog(#"dLatitude : %f", latitude);
}
else{ NSLog(#"ERROR");
}
}

CLLocationManager is asynchronous, You should get the latitude and longitude in the delegate method.
locationManager.delegate = self;
Then implement this delegate method:
- locationManager:didUpdateLocations:

1.first of all, you should use you should use a actual device instead of a simulator
2.implement CLLocationManagerDelegate in the interface,like
#interface XXXXX () <CLLocationManagerDelegate>
3.after
locationManager = [[CLLocationManager alloc] init];
add
locationManager.delegate = self;
4.implement the delegate method
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
for (CLLocation *location in locations) {
//you can get locations here
}
}

Related

CoreLocation iOS9 storing Coordinates

I recently asked this question, not knowing of the changes made to CL in iOS 9. I am trying to store the user's current location and I have changed my code to reflect the new delegate methods in iOS 9, but didUpdateLocations is still never reached.
Here is the link to my original question: Latitude & Longitude Not Retrieved
And my updated code:
viewWillAppear
- (void)viewWillAppear:(BOOL)animated{
manager = [[CLLocationManager alloc] init];
manager.delegate = self;
if ([manager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[manager requestWhenInUseAuthorization];
}
[manager startUpdatingLocation];
}
didUpdateLocations
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(#"Location: %#", locations);
CLLocation *currentLocation = locations.lastObject;
if (currentLocation != nil) {
float latitude = currentLocation.coordinate.latitude;
float longitude = currentLocation.coordinate.longitude;
NSLog(#"dLongitude : %f", longitude);
NSLog(#"dLatitude : %f", latitude);
}
else{ NSLog(#"ERROR");
}
}
check for
authorizationStatus and startUpdatingLocation
and
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
you can also go to settings ->privacy -> location services (turn on) -> select ur app and select while using.

Objective-C issues with Location Manager

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];
}

iOS: Watch Kit get current location and show on map

I have an iOS app that uses a users current location to show directions/ distance between the users current location and a destination point. I am able to show the destination point on the map, however when I try to get the user's current location using CLlocationManager, it returns coordinates (0,0). Below is my code for retrieving the users location.
Is retrieving current location possible on the Apple Watch?
If so, what am I doing wrong here that the location is returning (0,0)?
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = 5;
CLLocation *location = locationManager.location;
CLLocationCoordinate2D myLoc = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
Also, I have #import < CoreLocation/CoreLocation.h> in my .h file and < CLLocationManagerDelegate>
Try the following code:
#property (nonatomic, strong) CLLocationManager *locationManager;
- (void)willActivate
{
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager requestLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
if ([locations count] == 0) {
// error
return;
}
// success
self.currentLocation = [locations firstObject];
CLLocationCoordinate2D myLoc = CLLocationCoordinate2DMake(
self.currentLocation.coordinate.latitude,
self.currentLocation.coordinate.longitude);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(nonnull NSError *)error
{
// error
}
If you did not implemented this:
(with attributes inspector or programmatically)
self.mapView.showsUserLocation = YES
Or in addition to tell the mapView that the user's location is depending of LocationManager.location

CLLocationCoordinate2D latitude & longitude always zero

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];
}

Testing a fake current location on iOS Simulater is not working

I have been trying to fake my location in the iOS simulator but the methods I have been reading about are not working. I have used the debugger to set my fake location, and have made a custom location in Debug->Location->Custom Location, yet I still log 0.000 0.000 for lat and long.
I'm using this code to find current location.
- (IBAction)currentLocationButtonPressed:(UIButton *)sender
{
//Search for pubs and bars in current location.
//Push to tableViewController
NSLog(#"current location Pressed.");
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest; // Best accuracy
[locationManager startUpdatingLocation];
NSLog(#"%#",[self deviceLocation]);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSLog(#"Latitude: %f Longitude: %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
}
}
- (NSString *)deviceLocation {
return [NSString stringWithFormat:#"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
}
The delegate is set and I am requesting authorization.
- (void)viewDidLoad {
[super viewDidLoad];
searchLocationTextField.delegate = self;
locationManager.delegate = self;
locationManager = [[CLLocationManager alloc]init];
[locationManager requestWhenInUseAuthorization]; // For foreground access
[locationManager requestAlwaysAuthorization]; // For background access
}
Set locationManager delegate to after allocating it.
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager:didUpdateToLocation:fromLocation: delegate method is deprecated, use locationManager:didUpdateLocations: instead.
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations lastObject];
NSLog(#"Latitude: %f Longitude: %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
}

Resources