I'd like to know how to get latitude and longitude in iOS using Objective-C . can anybody help me? I have already followed several examples but none shows the correct information.
I'd like to know how to get latitude and longitude in iOS using Objective-C . can anybody help me? I have already followed several examples but none shows the correct information.
From iOS 8 you need a few steps beyond what we were accustomed to before the system upgrade, perhaps this was missing from the tutorials you found on the web as I came across this same situation.
In your Info.plist
file, there are two properties you can add (use text mode):
To get location while the app is running (even in background ):
<key>NSLocationAlwaysUsageDescription</key>
<string>Mensagem para o usuário</string>
To get location while the app is in the foreground only:
<key>NSLocationWhenInUseUsageDescription</key>
<string>Mensagem para o usuário</string>
After importing the CoreLocation framework to your project, add the following delegate in your .h :
@interface LocalizacaoViewController: UIViewController <CLLocationManagerDelegate>
Then start the search in your deployment file:
if (self.locManager == nil) {
self.locManager = [[CLLocationManager alloc] init];
[self.locManager setDelegate:self];
[self.locManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
}
// Verificação de recurso apenas para iOS 8
if ([self.locManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locManager requestWhenInUseAuthorization]; // Ou requestAlwaysAuthorization
}
[self.locManager startUpdatingLocation];
And finally, the implementation of delegate methods:
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
// Erro
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *lastLocation = [locations lastObject];
// Latitude: lastLocation.coordinate.latitude
// Longitude: lastLocation.coordinate.longitude
}
In these methods, unless you say to stop updating the location with [self.locManager stopUpdatingLocation]
, they will run every time you change.
See if you can succeed with this implementation.