美文网首页
进阶 13-4Map 地理编码和反地理编码

进阶 13-4Map 地理编码和反地理编码

作者: 轩洛 | 来源:发表于2016-07-15 21:11 被阅读0次

    地理编码:根据给定的位置(通常是地名),确定地理坐标(经纬度)
    反地理编码:根据给定地理坐标(经纬度),确定位置信息(街道,门牌等等)

    #import "GeocoderOneViewController.h"
    //引入定位头文件(用于地理定位)
    #import <CoreLocation/CoreLocation.h>
    @interface GeocoderOneViewController ()
    /* 声明地理编码属性 */
    @property (strong,nonatomic) CLGeocoder *geocoder;
    @end
    
    @implementation GeocoderOneViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //初始化编码对象
        self.geocoder = [[CLGeocoder alloc] init];
    //    [self getCoordinateByAddress:@"北京市"];
        [self getAddressByLatitude:40.0305627852 Longitude:116.3435577061];
    }
    #pragma mark --- 地理编码 ---
    - (void)getCoordinateByAddress:(NSString *)address{
        [self.geocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            //获取第一个地标,地标中存储了详细的地址信息。注意:一个地名可能搜索出多个地址
            CLPlacemark *placeMark = [placemarks firstObject];
            //获取位置
            CLLocation *location = placeMark.location;
            //获取区域
            CLRegion *region = placeMark.region;
            //地名
            NSString *name = placeMark.name;
            
            NSDictionary *dic = placeMark.addressDictionary;
            //街道
            NSString *thoroughfare = placeMark.thoroughfare;
            //街道相关信息
            NSString *subThoroughfare = placeMark.subThoroughfare;
            
            //城市
            NSString *locality = placeMark.locality;
            //州
            NSString *administraiveArea = 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(@"位置:%@,区域:%@,详细信息:%@,name:%@,街道:%@,街道详情%@",location,region,dic,name,thoroughfare,subThoroughfare);
        }];
    }
    
    #pragma mark --- 反地理编码 ---
    - (void)getAddressByLatitude:(CLLocationDegrees)latitude Longitude:(CLLocationDegrees)longitude{
        //初始化位置信息
        CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
        
        //反地理编码方法
        [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * placemarks, NSError * error) {
            //取地标中的第一个
            CLPlacemark *placeMark = [placemarks firstObject];
            NSLog(@"详细信息:%@",placeMark.addressDictionary);
        }];
    }
    @end
    
    

    相关文章

      网友评论

          本文标题:进阶 13-4Map 地理编码和反地理编码

          本文链接:https://www.haomeiwen.com/subject/pkjyjttx.html