This part of code is from Book
1. Declare a property of type CLGeocoder
#import
#import
@interface
Converting_Meaningful_Addresses_to_Longitude_and_LatitudeViewController : UIViewController
@property (nonatomic, strong) CLGeocoder *myGeocoder;
@end
2. In view did load
-
NSString *oreillyAddress =
@"1005 Gravenstein Highway North, Sebastopol, CA 95472, USA";
self.myGeocoder = [[CLGeocoder alloc] init];
[self.myGeocoder
geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > 0 && error == nil){
NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]); CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude);
NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude);
}
else if ([placemarks count] == 0 &&
error == nil){ NSLog(@"Found no placemarks.");
}
else if (error != nil){
NSLog(@"An error occurred = %@", error); }
}];
No comments:
Post a Comment