0
I would like to know how to get latitude and longitude in iOS using Objective-C. Could someone help me? I’ve followed several examples but none displays the correct information.
0
I would like to know how to get latitude and longitude in iOS using Objective-C. Could someone help me? I’ve followed several examples but none displays the correct information.
1
From the iOS 8 you need a few steps beyond what we were used to before the system upgrade, maybe this was missing in the tutorials you found on the web because I came across this same situation.
In your file Info.plist
, there are two properties you can add (use text mode):
To get location while the app is running (even on background):
<key>NSLocationAlwaysUsageDescription</key>
<string>Mensagem para o usuário</string>
To get location while the app is focused only:
<key>NSLocationWhenInUseUsageDescription</key>
<string>Mensagem para o usuário</string>
After importing the framework Corelocation to your project, add the following delegate in your file .h:
@interface LocalizacaoViewController: UIViewController <CLLocationManagerDelegate>
And then start searching your implementation 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:
- (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 end the location update with [self.locManager stopUpdatingLocation]
, they will be executed every time there is change.
See if you can succeed with implementation this way.
Browser other questions tagged ios objective-c
You are not signed in. Login or sign up in order to post.
Thanks for the help, but I performed the procedure and still not displaying.
– Bruno Richart
Bruno, are you testing on the device or in the simulator? Something in the log? Tried to put breakpoints to see how far you are getting?
– Paulo Rodrigues
@Brunorichart managed to resolve with this reply, consider marking this as correct so that it serves as references to others with the same doubt.
– Paulo Rodrigues