地理编码与反地理编码

简介: 地理编码与反地理编码
  • 使用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
腾讯位置 - 逆地址解析(结尾附视频)
腾讯位置 - 逆地址解析(结尾附视频)
1649 0
|
Web App开发 域名解析 缓存
如何在 Ubuntu 20.04 上安装 Node.js 和 npm
本文我们主要为大家介绍在 Ubuntu 20.04 上安装 Node.js 和 npm 的三种不同的方式。
164164 7
如何在 Ubuntu 20.04 上安装 Node.js 和 npm
|
安全 前端开发
FastAPI(56)- 使用 Websocket 打造一个迷你聊天室 (上)
FastAPI(56)- 使用 Websocket 打造一个迷你聊天室 (上)
593 0
FastAPI(56)- 使用 Websocket 打造一个迷你聊天室 (上)
|
分布式计算 大数据 Serverless
云栖实录 | 开源大数据全面升级:Native 核心引擎、Serverless 化、湖仓架构引领云上大数据发展
在2024云栖大会开源大数据专场上,阿里云宣布推出实时计算Flink产品的新一代向量化流计算引擎Flash,该引擎100%兼容Apache Flink标准,性能提升5-10倍,助力企业降本增效。此外,EMR Serverless Spark产品启动商业化,提供全托管Serverless服务,性能提升300%,并支持弹性伸缩与按量付费。七猫免费小说也分享了其在云上数据仓库治理的成功实践。其次 Flink Forward Asia 2024 将于11月在上海举行,欢迎报名参加。
725 6
云栖实录 | 开源大数据全面升级:Native 核心引擎、Serverless 化、湖仓架构引领云上大数据发展
|
网络协议 应用服务中间件 网络安全
阿里云免费SSL申请流程(白嫖20张SSL免费证书)2024年新版教程
本文详述了2024年最新的阿里云免费SSL证书申请流程。用户可通过阿里云数字证书管理服务控制台一键申请最多20张免费单域名SSL证书,每张证书有效期为3个月。首先登录控制台,选择“SSL证书管理”下的“个人测试证书”,同意协议并完成购买流程。之后需创建证书、输入域名等信息并进行域名验证。验证方法包括手动DNS验证、域名授权自动化验证或文件验证。完成验证后,等待审核通过即可下载适用于不同服务器类型的SSL证书。请注意,阿里云免费SSL证书到期后不支持续费,需重新申请。了解更多详情,请访问阿里云官方SSL证书页面。
|
机器学习/深度学习 数据可视化 TensorFlow
使用Python实现深度学习模型:图像语义分割与对象检测
【7月更文挑战第15天】 使用Python实现深度学习模型:图像语义分割与对象检测
346 2
|
JavaScript
【vue】vue 在线编辑、实时预览的代码交互组件 vue-code-view
【vue】vue 在线编辑、实时预览的代码交互组件 vue-code-view
1851 0
|
JSON JavaScript 定位技术
Echarts 绘制地图(中国、省市、区县),保姆级教程!
Echarts 绘制地图(中国、省市、区县),保姆级教程!
|
NoSQL MongoDB 数据库
深入探究MongoDB的ObjectId:唯一性、顺序性与应用指南
深入探究MongoDB的ObjectId:唯一性、顺序性与应用指南
1037 0
基于sortablejs实现拖拽element-ui el-table表格行进行排序
基于sortablejs实现拖拽element-ui el-table表格行进行排序

热门文章

最新文章