美文网首页iOS Developer
基于CLGeocoder - 反地理编码

基于CLGeocoder - 反地理编码

作者: 光是光光的光呐 | 来源:发表于2016-05-05 10:52 被阅读1330次

iOS中CoreLocatio框架中的CLGeocoder 类不但为我们提供了地理编码方法,而且还提供了反地理编码:
同样需要导入框架:
#import <CoreLocation/CoreLocation.h>
反地理编码方法:
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
同样当反地理编码完成时,会调用completionHandler 对象,该对象类型为 CLGeocodeCompletionHandler,实际上是一个 block 对象
这个对象中传递了2个参数,其中placemark:里面装了CLPlacemark对象

步骤:
1、创建地理编码对象
2、获取用户的地理坐标(经纬度)
3、根据地理坐标创建CLLocation对象
4、根据CLLocation对象获取对象坐标信息

Demo如下 :

@interface ViewController ()

//地理编码对象
@property (nonatomic ,strong) CLGeocoder *geocoder;

#pragma mark - 反地理编码
- (IBAction)reverseGeocode;

@property (weak, nonatomic) IBOutlet UITextField *longtitudeField;

@property (weak, nonatomic) IBOutlet UITextField *latitudeField;

@property (weak, nonatomic) IBOutlet UILabel *reverseDetailAddressLabel;

@end

@implementation ViewController

#pragma mark - 反地理编码响应

- (void)reverseGeocode
{

      // 1.获取用户输入的经纬度
      NSString *longtitude = self.longtitudeField.text;
      NSString *latitude = self.latitudeField.text;
      if (longtitude.length == 0 ||
          longtitude == nil ||
          latitude.length == 0 ||
          latitude == nil) {
          NSLog(@"请输入经纬度");
          return;
      }
    
      // 2.根据用户输入的经纬度创建CLLocation对象
      CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude doubleValue]  longitude:[longtitude doubleValue]];
      // 116.403857,39.915285
      // 3.根据CLLocation对象获取对应的地标信息
      [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        
        for (CLPlacemark *placemark in placemarks) {
            NSLog(@"%@ %@ %f %f", placemark.name, placemark.addressDictionary, placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);
            self.reverseDetailAddressLabel.text = placemark.locality;
         }
      }];
}

#pragma mark - 懒加载,创建地理编码对象
- (CLGeocoder *)geocoder
{

      if (!_geocoder) {
          _geocoder = [[CLGeocoder alloc] init];
      }
      return _geocoder;
}

效果图如下:


Paste_Image.png

相关文章

  • CLGeocoder

    CLGeocoder(地理编码) 使用CLGeocoder可以完成“地理编码”和“反地理编码”地理编码:根据给定的...

  • 基于CLGeocoder - 反地理编码

    iOS中CoreLocatio框架中的CLGeocoder 类不但为我们提供了地理编码方法,而且还提供了反地理编码...

  • 地理编码

    地理编码和反地理编码都使用CLGeocoder类来实现. 地理编码使用 geocodeAddressString:...

  • 地理编码与反地理编码

    使用CLGeocoder可以完成“地理编码”和“反地理编码” 地理编码:根据给定的地名,获得具体的位置信息(比如经...

  • iOS 地理编码 / 反地理编码

    一、CLGeocoder 地理编码 与 反地理编码 地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址...

  • 基于CLGeocoder - 地理编码

    iOS中CoreLocatio框架中的CLGeocoder为我们提供了地理编码方法:首先需要导入框架#import...

  • ios学习笔记之地图(中)

    一 前言 本章主要介绍地理编码及反地理编码,需要用到CLGeocoder类 二 地理编码 在下图绿框内输入地址,点...

  • 地图-->地名VS地理坐标(根据"北京"

    地理编码 除了提供位置跟踪功能之外,在定位服务中还包含CLGeocoder类用于处理地理编码和逆地理编码(又叫反地...

  • 2019-06-26 反编译经纬度

    //反向地理编码 CLGeocoder *clGeoCoder = [[CLGeocoder alloc] in...

  • 基于百度API的 - 反地理编码

    iOS中CoreLocatio框架中的CLGeocoder 类不但为我们提供了地理编码方法,而且还提供了反地理编码...

网友评论

    本文标题:基于CLGeocoder - 反地理编码

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