zoukankan      html  css  js  c++  java
  • 地理编码与反地理编码

    //成员属性

    @property (nonatomic, strong) CLGeocoder *geocoder;

    //懒加载:使用的时候再加载

    - (CLGeocoder *)geocoder

    {

        if (!_geocoder) {

            self.geocoder = [[CLGeocoder alloc] init];

        }

        return _geocoder;

    }

    //地理编码  能利用地址找到经纬度

    - (IBAction)geocode {

        NSString *address = self.addressField.text;

        if (address.length == 0) return;

        

        [self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {

            if (error) {// 有错误(地址乱输入)

                self.detailAddressLabel.text = @"你找的地址可能只在火星有!!!";

            } else { // 编码成功

                // 取出最前面的地址

                CLPlacemark *pm = [placemarks firstObject];

                // 设置经纬度

                self.latitudeLabel.text = [NSString stringWithFormat:@"%.6f", pm.location.coordinate.latitude];

                self.longitudeLabel.text = [NSString stringWithFormat:@"%.6f", pm.location.coordinate.longitude];

                // 设置具体地址 

              self.detailAddressLabel.text = pm.name; 

               }

        }];

    }

     

    //反地理编码 能利用经纬度显示地址(可用作定位)

    - (void)reverseGeocode{

        // 1.包装位置

        CLLocationDegrees latitude=self.latituaaaa;

        CLLocationDegrees longitude=self.longtituaaaa;

        CLLocation *loc = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

        

        NSLog(@"%f,,,%f",longitude,latitude);

        NSLog(@"%@",loc);

        // 2.反地理编码

        [self.geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {

            if (error) { // 有错误(地址乱输入)

                NSLog(@"%@",error);

                self.reverseDetailAddressLabel.text = @"没有这个地址!!!";

            } else {

                

                // 编码成功

                // 取出最前面的地址

                CLPlacemark *pm = [placemarks firstObject];

                

                // 设置具体地址

    //           self.reverseDetailAddressLabel.text = pm.name;

    //            self.reverseDetailAddressLabel.text=pm.locality;

                

    //            self.reverseDetailAddressLabel.text=pm.addressDictionary;

                

                self.reverseDetailAddressLabel.text=pm.addressDictionary[@"Name"];

                self.dizhi=pm.addressDictionary[@"City"];

                NSString *shi= [self.dizhi substringToIndex:2];

                [self.cleanButton setTitle:shi];

                

                [self.reverseGeocodeButton setTitle:pm.addressDictionary[@"City"] forState:0];

              NSLog(@"%@",pm.addressDictionary);

                

            }

        }];

    }

  • 相关阅读:
    VIM 基本配置
    VIM 基本配置
    CWnd与HWND的区别与转换 如何获取本窗体对象
    CWnd与HWND的区别与转换 如何获取本窗体对象
    CWnd与HWND的区别与转换 如何获取本窗体对象
    XEN
    XEN
    Xinyu Zhang
    (OK)(OK) SEEM ALL Testing Results
    如何写好一篇高质量的IEEE/ACM Transaction级别的计算机科学论文?
  • 原文地址:https://www.cnblogs.com/onlyYura/p/4180032.html
Copyright © 2011-2022 走看看