地理编码与反地理编码

简介: 地理编码与反地理编码
  • 使用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)


地理编码方法:


image.png

下面是具体的代码:(在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 *> * _Nul



image.png


下面是具体的代码

#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


目录
相关文章
|
JSON 定位技术 API
腾讯位置 - 逆地址解析(结尾附视频)
腾讯位置 - 逆地址解析(结尾附视频)
1959 0
|
NoSQL 数据挖掘 定位技术
如何让geopandas支持写出gdb文件
如何让geopandas支持写出gdb文件
618 4
|
JavaScript
【vue】vue 在线编辑、实时预览的代码交互组件 vue-code-view
【vue】vue 在线编辑、实时预览的代码交互组件 vue-code-view
2082 0
|
人工智能 Java
通过okhttp调用SSE流式接口,并将消息返回给客户端
通过okhttp调用SSE流式接口,并将消息返回给客户端
|
机器学习/深度学习 存储 算法
真是太强大了!YOLO-World检测一切的任务框架使用指南,支持开放词汇检测任务
真是太强大了!YOLO-World检测一切的任务框架使用指南,支持开放词汇检测任务
|
NoSQL MongoDB 数据库
深入探究MongoDB的ObjectId:唯一性、顺序性与应用指南
深入探究MongoDB的ObjectId:唯一性、顺序性与应用指南
1325 0
|
索引
Elasticsearch exception [type=illegal_argument_exception, reason=index [.1] is the write index for data stream [slowlog] and cannot be deleted]
在 Elasticsearch 中,你尝试删除的索引是一个数据流(data stream)的一部分,而且是数据流的写入索引(write index),因此无法直接删除它。为了解决这个问题,你可以按照以下步骤进行操作:
1374 0
|
JavaScript 前端开发 数据可视化
富文本编辑器使用详细介绍
富文本编辑器使用详细介绍
870 0