地理编码与反地理编码

作者: IIronMan | 来源:发表于2016-06-30 01:38 被阅读379次
  • 使用CLGeocoder可以完成“地理编码”和“反地理编码”
  • 地理编码:根据给定的地名,获得具体的位置信息(比如经度和纬度,以及地址的全称)
  • 反地理编码:根据给定的经度和纬度,获取具体的位置信息

(一)地理编码

  • 具体的做法:(就两步)

    1.创建地理编码对象:导入框架#import <CoreLocation/CoreLocation.h>
    
         CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    
    2.利用地理编码对象编码:声明一个@property(nonatomic,strong) CLGeocoder *geocoder;属性
    

    调用一个地理编码方法:(很重要)

    [self.geocoder geocodeAddressString:self.addressFiled.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error)
    

地理编码方法:


编码展示

下面是具体的代码:(在storyBoard里面画的,1个button,1个UITextFiled,3个UILabel)

 #import "ViewController.h"
 #import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>

/*
   需要编码的地址容器
*/
@property (weak, nonatomic) IBOutlet UITextField *addressFiled;

/*
    经度容器
*/
 @property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
/*
    纬度容器
*/
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
/*
    详情容器
*/
@property (weak, nonatomic) IBOutlet UILabel *detailAddressLabel;
/*
    监听地理编码点击事件
*/

- (IBAction)geocoderBtnClick;

@property(nonatomic,strong) CLGeocoder *geocoder;

@end

@implementation ViewController

//1.创建地理编码对象
-(CLGeocoder *)geocoder
{
  if (!_geocoder) {
    
    _geocoder = [[CLGeocoder alloc]init];
}

  return _geocoder;
}

- (void)viewDidLoad {
[super viewDidLoad];

self.addressFiled.clearButtonMode = UITextFieldViewModeAlways;
self.addressFiled.placeholder = @"    请输入地理位置";

}


- (IBAction)geocoderBtnClick {

//获取用户输入的位置
NSString *addressString = self.addressFiled.text;

NSLog(@"%@",addressString);

if (addressString == nil || addressString.length == 0) {
    
    NSLog(@"请输入地址");
    return;
}

//2.利用地理编码对象编码
//根据传入的地址获取该地址的对应的经纬度信息
[self.geocoder geocodeAddressString:self.addressFiled.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    
    //placemarks 地标  地标数组里面存放着地标,每一个地标包含了该位置的经纬度,以及城市/区域/国家代码/邮编等等....
    if (placemarks.count == 0 || error != nil) {
        
        return;
    }

    //获取数组里面第一个信息
    CLPlacemark *placemark = [placemarks firstObject];
    
    self.latitudeLabel.text = [NSString stringWithFormat:@"   %f", placemark.location.coordinate.latitude];
    
    self.longitudeLabel.text = [NSString stringWithFormat:@"   %f",placemark.location.coordinate.longitude];
    
    NSArray *array = placemark.addressDictionary[@"FormattedAddressLines"];
    
    NSMutableString *stringAddstring = [NSMutableString string];
    
    for (NSString *string in array) {
        [stringAddstring appendString:string];
    }
    
    self.detailAddressLabel.text = stringAddstring;
    
    NSLog(@"%@ %@ %f %f",placemark.name,placemark.addressDictionary,placemark.location.coordinate.latitude,placemark.location.coordinate.longitude);
    
}];

 }
 @end

(二)反地理编码

    1.创建地理编码对象:导入框架#import <CoreLocation/CoreLocation.h>

       CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    2.利用地理编码对象编码:声明一个@property(nonatomic,strong) CLGeocoder *geocoder;属性

调用一个反地理编码方法:(很重要)

[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) 
反编码
  • 下面是具体的代码

    #import "ViewController.h"
    #import <CoreLocation/CoreLocation.h>
    @interface ViewController ()<CLLocationManagerDelegate>
    
    - (IBAction)clickButton;
    
    @property (weak, nonatomic) IBOutlet UITextField *latitudeValue;
    
    @property (weak, nonatomic) IBOutlet UITextField *longitudeValue;
    
    @property (weak, nonatomic) IBOutlet UILabel *labelText;
    
    @property(nonatomic,strong) CLGeocoder *geocoder;
    
    @end
    
    @implementation ViewController
    
    -(CLGeocoder *)geocoder
    {
       if (!_geocoder) {
      
         _geocoder = [[CLGeocoder alloc]init];
        
     }
    
       return _geocoder;
    }
    
     - (void)viewDidLoad {
     [super viewDidLoad];
    
     }
    
    - (IBAction)clickButton {
    
      //1.获取用户输入的经纬度
    
     NSString *latitudeString = self.latitudeValue.text;
    
     NSString *longitudeValueString = self.longitudeValue.text;
    
     if (latitudeString == nil || latitudeString.length == 0 || longitudeValueString == nil || longitudeValueString.length == 0) {
      
      NSLog(@"请输入经纬度");
      
      return;
     }
    
     //2.根据用户输入的经纬度创建CLLocation对象
    
      CLLocation *location = [[CLLocation alloc]initWithLatitude:[latitudeString doubleValue] longitude:[longitudeValueString doubleValue]];
    
     //3.根据CLLocation对象获取相应的地表信息
    
      [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    
      for (CLPlacemark *placemark in placemarks) {
          
          self.labelText.text = placemark.locality;
          NSLog(@"哈哈");
          
        }
      
     }];
    
    }
    @end

相关文章

  • CLGeocoder

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

  • 高德地图问题

    1:定位的时候获取用户的省市区位置,通过反地理编码 地理编码与反地理编码 地理编码:根据地址获得相应的经纬度以及详...

  • 地理编码与反地理编码

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

  • 地理编码

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

  • 地图和定位(三)

    一、地理编码和反地理编码 地理编码:把地址转为经纬度反地理编码:把经纬度转为地址 二、获取当前城市名称(定位+反地...

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

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

  • iOS开发之CoreLocaiton框架使用(地理编码,反地理编

    什么是地理编码和反地理编码? 地理编码 地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)。...

  • 地理编码与反编码

    首先我们要了解地理编码和反编码的含义和作用:<1>地理编码:把地名转换成位置信息作用:把文字描述的 位置转换成地图...

  • iOS地理编码的简单实现

    今天来写写地理编码 ,废话不多说,直接进入正题 地理编码有两种方式 反地理编码:把经纬度转换成地名 正地理编码:把...

  • iOS开发 - 地理编码与反地理编码

    在获取用户位置时候,Core Location 会返回一对经纬度。我们人类看不出什么东东,但是存在就是合理,交给机...

网友评论

  • e5d6184d0903:您好,请教一个问题。我用国外某地的经纬度坐标反地理编码的时候报错,是什么原因呢?

本文标题:地理编码与反地理编码

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