Reputation: 2450
I try to get coordinate of my location.
I stackoverflowed so what I coded is below:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
self.longitude=coordinate.longitude;
self.latitude=coordinate.latitude;
NSLog(@"dLongitude : %f",self.longitude);
NSLog(@"dLatitude : %f", self.latitude);
But I'm always getting 0 all the time. Is the code above wrong? or I didn't set my simulator for GPS location?
I don't understand why I'm having trouble in getting coordinate.
Upvotes: 4
Views: 1514
Reputation: 1
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
BOOL shouldIAllow = FALSE;
NSString* locationStatus = @"";
switch (status) {
case kCLAuthorizationStatusRestricted:
locationStatus = @"Restricted Access to location";
break;
case kCLAuthorizationStatusDenied:
locationStatus = @"User denied access to location";
break;
case kCLAuthorizationStatusNotDetermined:
locationStatus = @"Status not determined";
default:
locationStatus = @"Allowed to location Access";
shouldIAllow = TRUE;
break;
}
if (shouldIAllow) {
NSLog(@"Location to Allowed");
// Start location services
[_locationManager startUpdatingLocation];
} else {
NSLog(@"Denied access: \(locationStatus)");
}
}
Upvotes: 0
Reputation: 3675
[CLLocationManager location]
will return you the most recently retrieved user location, but as the documentation says:
The value of this property is nil if no location data has ever been retrieved.
At the beginning your location is still unknown. You should use the delegates methods to react when the CLLocationManager find out your location. Implement this method:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];
//User your location...
}
Take a look at the documentation.
Upvotes: 1
Reputation: 6795
Firstly, here are several issues in your code:
-locationManager:didUpdateLocations:
and -locationManager:didFailWithError:
delegate methods[locationMamager requestWhenInUseAuthorization];
or [locationMamager requestAlwaysAuthorization];
NSLocationWhenInUseUsageDescription
or NSLocationAlwaysUsageDescription
accordingly in your Info.plistYou can simulate location using Xcode, look at the information from Apple: https://developer.apple.com/library/ios/recipes/xcode_help-debugger/articles/simulating_locations.html
Upvotes: 6