7.7 Converting Meaningful Addresses to Longitude and Latitude

本文介绍了一个使用Objective-C编写的程序片段,该程序能够通过输入地址(例如“北京”),利用CLGeocoder类来获取该地址对应的地理坐标(经度和纬度)。示例中展示了如何在iOS应用中实现这一功能,并提供了具体的输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


通过地址获取经纬度


- (void)viewDidLoad

{

    [super viewDidLoad];

// 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 = [[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);

         }

     }];

}



输出:

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值