地理编码 ,反编码 ,地理编解码

本文介绍了地理编码和反编码的概念及其在地图应用中的作用。地理编码将地名转化为经纬度,反编码则将位置信息转为文字描述。在实际操作中,这些过程可能耗时,建议采用异步处理。在iOS开发中,涉及到的主要类包括CLLocationManager负责定位管理,CLGeocoder用于编码和反编码,CLLocation表示位置信息,CLPlacemark封装详细地理信息,而CLLocationCoordinate2D结构体则表示经纬度坐标。

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

<1>地理编码:把地名转换成位置信息
作用:把文字描述的 位置转换成地图上的经纬度;

<2>反编码:把位置信息转换成文字
作用:可以点击地图上的某个位置 来获得文字的描述

<3>地理编解码在编解码的时候 是一个耗时的操作 可以采用异步操

//编码  把地名转换成位置信息  
// addressString 指定的地理名称 
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
//反编码  把位置信息转换成文字   
//location 指定的地理位置经纬度
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;

*注:在工程运行开始时,都必须要添加定位框架#import <CoreLocation/CoreLocation.h> 或者直接添加 

@import MapKit;


1、地理编码:文字描述的位置 转换成 地图上的经纬度
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    [geocoder geocodeAddressString:@"郑州" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark *placemark = placemarks.firstObject;
        CLLocation *loc = placemark.location;

        NSLog(@"纬度%f 经度%f",loc.coordinate.latitude,loc.coordinate.longitude);   
    }];

输出结果:纬度34.707001 经度113.509167

#pragma mark 根据地名确定地理坐标
-(void)getCoordinateByAddress:(NSString *)address{ 
//地理编码 
[_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) { 
//取得第一个地标,地标中存储了详细的地址信息,注意:一个地名可能搜索出多个地址 
CLPlacemark *placemark=[placemarks firstObject]; CLLocation *location=placemark.location;//位置 CLRegion *region=placemark.region;//区域 NSDictionary *addressDic= placemark.addressDictionary;//详细地址信息字典,包含以下部分信息 //
 NSString *name=placemark.name;//地名 // NSString *thoroughfare=placemark.thoroughfare;//街道 
// NSString *subThoroughfare=placemark.subThoroughfare; //街道相关信息,例如门牌等 
// NSString *locality=placemark.locality; // 城市
 // NSString *subLocality=placemark.subLocality; // 城市相关信息,例如标志性建筑 
// NSString *administrativeArea=placemark.administrativeArea; // 州 
// NSString *subAdministrativeArea=placemark.subAdministrativeArea; //其他行政区域信息 
// NSString *postalCode=placemark.postalCode; //邮编 
// NSString *ISOcountryCode=placemark.ISOcountryCode; //国家编码 
// NSString *country=placemark.country; //国家 // NSString *inlandWater=placemark.inlandWater; //水源、湖泊 
// NSString *ocean=placemark.ocean; // 海洋 
// NSArray *areasOfInterest=placemark.areasOfInterest; //关联的或利益相关的地标
 NSLog(@"位置:%@,区域:%@,详细信息:%@",location,region,addressDic); }];
}
#pragma mark 根据坐标取得地名-(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{ //反地理编码 CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude]; 
[_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { 
CLPlacemark *placemark=[placemarks firstObject]; NSLog(@"详细信息:%@",placemark.addressDictionary); }];
}
@end

总结我们目前在地图上接触到的几个类:

CLLocationManager:定位管理器,用来设置管理定位,设置定位的精度、定位频率、后台运行等。

CLGeocoder:主要用来编码与反编码。

CLLocation:用于表示位置信息,包含地理坐标、海拔等信息,包含在CoreLoaction框架中。

CLPlacemark:定位框架中地标类,封装了详细的地理信息。

CLLocationCoordinate2D:他是一个结构体,用来表示经纬度。







                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值