地理编码和反地理编码

本文介绍了一个iOS应用中实现地理编码与反地理编码的方法。通过实例展示了如何使用Core Location框架中的CLGeocoder类来将地址转换为经纬度坐标(地理编码),以及将经纬度坐标转换回具体的地址信息(反地理编码)。文中提供了完整的代码实现,帮助开发者更好地理解和应用地理编码技术。

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

//
//  ViewController.m



#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()
@property(nonatomic,strong)CLGeocoder *geocoder;

#pragma mark - 地理编码
- (IBAction)geocode:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UITextField *addressField;
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel;

#pragma mark - 反地理编码
- (IBAction)reverseGeocode:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UITextField *longitudeField;
@property (weak, nonatomic) IBOutlet UITextField *latitudeField;
@property (weak, nonatomic) IBOutlet UILabel *reverseDatailAddressLabel;

@end

@implementation ViewController

/**
 *  延迟创建CLGeocoder对象
 */
- (CLGeocoder *)geocoder
{
    if (!_geocoder) {
        self.geocoder = [[CLGeocoder alloc] init];
    }
    return _geocoder;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //退出键盘
    [self.view endEditing:YES];
}

/**
 *  地理编码
 */
- (IBAction)geocode:(UIButton *)sender {
    NSString *address = self.addressField.text;
    if(address.length == 0) return;
    
    [self.geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (error) { //有错误(地址乱输入)
            self.detailAddressLabel.text = @"你找的地址可能只在火星有!!!";
        }else{ //编码成功
            
            //取出最前面的地址
           CLPlacemark *pm = [placemarks firstObject];
           // 设置经纬度
            self.latitudeLabel.text = [NSString stringWithFormat:@"%.1f",pm.location.coordinate.latitude];
            self.latitudeLabel.text = [NSString stringWithFormat:@"%.1f",pm.location.coordinate.longitude];
            //设置具体的位置
            self.detailAddressLabel.text = pm.name;
            
//            NSLog(@"总共找到%lu个地址",placemarks.count);
//            
//            for (CLPlacemark *pm in placemarks) {
//                NSLog(@"----地址开始----");
//                
//                NSLog(@"%f %f %@",pm.location.coordinate.latitude,pm.location.coordinate.longitude,pm.name);
//                
//                //遍历字典
//                [pm.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
//                    NSLog(@"%@ %@",key,obj);
//                }];
//                
//                  NSLog(@"----地址结束----");
//            }
        }
    }];
  
}

/**
 *  反地理编码
 */
- (IBAction)reverseGeocode:(UIButton *)sender {
    
    //1.包装位置
    CLLocationDegrees latitude = [self.latitudeLabel.text doubleValue];
    CLLocationDegrees longitude = [self.longitudeField.text doubleValue];
    
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    
    //2.反地理编码
    [self.geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (error) { //有错误(地址乱输入)
            self.reverseDatailAddressLabel.text = @"你找的地址可能只在火星有!!!";
        }else{ //编码成功
            
            NSLog(@"总共找到%lu个地址",placemarks.count);
            for (CLPlacemark *pm in placemarks) {
                NSLog(@"----地址开始----");

                NSLog(@"%f %f %@",pm.location.coordinate.latitude,pm.location.coordinate.longitude,pm.name);

                //遍历字典
                [pm.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
                    NSLog(@"%@ %@",key,obj);
                }];
                
                  NSLog(@"----地址结束----");
            }
        }
    
    }];
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值