地理编码、反编码CLGeocoder、CLLocationManager入门级应用

地理编码 与 反地理编码让你便于理解的效果图如下,这些控件相信大家都会敲这里就不多说了,主要看两种编码的代码

这里写图片描述

#pragma mark——地理编码 控件——
    buttonCoding = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonCoding.frame = CGRectMake(20, 50, 120, 30);
    buttonCoding.backgroundColor = [UIColor redColor];
    [buttonCoding setTitle:@"地理编码" forState: UIControlStateNormal];
    [buttonCoding addTarget:self action:@selector(geocodeAddress) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonCoding];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 100, 80, 30)];
    label.text = @"地址";
    [self.view addSubview:label];


    textCoding = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];
    textCoding.keyboardType =  UIKeyboardTypeDefault;
    textCoding.delegate = self;

    textCoding.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:textCoding];


#pragma mark——地理编码 方法——
- (void)geocodeAddress {

    NSString *addName = textCoding.text;
    CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
    [geoCoder geocodeAddressString:addName completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error) {
            NSLog(@"您输入的地址不存在,请重新输入");
        }else{
        for (CLPlacemark *placeMarkXX in placemarks) {
            NSLog(@"%@ %@  经度是:%f 纬度是:%f",placeMarkXX.country,placeMarkXX.name,placeMarkXX.location.coordinate.longitude,placeMarkXX.location.coordinate.latitude);
            }
        }
    }];
}
//这里需要注意的是:geocodeAddress 是地理编码,addName 就是上图中你要输入的文字地址
#pragma mark——反地理编码 控件——

    againstButtonCoding = [UIButton buttonWithType:UIButtonTypeCustom];
    againstButtonCoding.frame = CGRectMake(20, 200, 120, 30);
    againstButtonCoding.backgroundColor = [UIColor redColor];
    [againstButtonCoding setTitle:@"反地理编码" forState: UIControlStateNormal];
    [againstButtonCoding addTarget:self action:@selector(reverseGeocode) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:againstButtonCoding];

    UILabel *reverse000 = [[UILabel alloc]initWithFrame:CGRectMake(20, 250, 60, 30)];
    reverse000.text = @"经度";
    [self.view addSubview:reverse000];

    reversetextCoding00 = [[UITextField alloc]initWithFrame:CGRectMake(90, 250, 100, 30)];
    reversetextCoding00.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:reversetextCoding00];


    UILabel *reverse001 = [[UILabel alloc]initWithFrame:CGRectMake(200, 250, 60, 30)];
    reverse001.text = @"纬度";
    [self.view addSubview:reverse001];

    reversetextCoding11 = [[UITextField alloc]initWithFrame:CGRectMake(270, 250, 100, 30)];
    reversetextCoding11.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:reversetextCoding11];



#pragma mark——反地理编码方法——
- (void)reverseGeocode {

    CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
    CLLocationDegrees latitude = [reversetextCoding11.text doubleValue];
    CLLocationDegrees longitude = [reversetextCoding00.text doubleValue];
    CLLocation *cllocationxx = [[CLLocation alloc]initWithLatitude: latitude longitude:longitude];
    [geoCoder reverseGeocodeLocation:cllocationxx completionHandler:^(NSArray *placemarks, NSError *error) {

        if (error) {
            NSLog(@"经度:0°~180°,纬度0°~90°,请重新输入");
        }else{
        for (CLPlacemark *reveser in placemarks) {
            NSLog(@"%@ %@ %@",reveser.country,reveser.locality,reveser.name);
            }
       } 
    }];
}
//这里需要注意的是:reverseGeocode是反地理编码的意思,cllocationxx是你要放入的经纬度,如上图所示

完了之后在模拟器上随便输入一个地址 或者 经纬度试试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值