7.7 Converting Meaningful Addresses to Longitude and Latitude

来源:互联网 发布:软件可靠性分配付 编辑:程序博客网 时间:2024/04/29 21:46


通过地址获取经纬度


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//    NSString *oreillyAddress = @"1005 Gravenstein Highway North, Sebastopol, CA 95472, USA";

   NSString *oreillyAddress = @"北京";

   self.myGeocoder = [[CLGeocoderalloc] init];

    [self.myGeocodergeocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks,NSError *error)

    {

        if ([placemarks count] >0 && error == nil){

            NSLog(@"Found %lu placemark(s).", (unsignedlong)[placemarks count]);

            CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];

            NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude);

            NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude);

         }

        else if ([placemarkscount] == 0 && error ==nil){

             NSLog(@"Found no placemarks.");

         }

        else if (error !=nil){

             NSLog(@"An error occurred = %@", error);

         }

     }];

}



输出:

2014-04-08 13:53:28.415 cookbook[504:c07] Found 1 placemark(s).

2014-04-08 13:53:28.416 cookbook[504:c07] Longitude = 116.405285

2014-04-08 13:53:28.416 cookbook[504:c07] Latitude = 39.904989


0 0