iOS地理反地理编码--CoreLocation

.sidebar{float:left;width:220px;}
.container-fluid>.content{margin-left:240px;}
a{color:#0069d6;text-decoration:none;line-height:inherit;font-weight:inherit;}a:hover{color:#00438a;text-decoration:underline;}
.pull-right{float:right;}
.pull-left{float:left;}
.hide{display:none;}
.show{display:block;}
.row{zoom:1;margin-left:-20px;}.row:before,.row:after{display:table;content:"";zoom:1;*display:inline;}
.row:after{clear:both;}
p{font-size:13px;font-weight:normal;line-height:18px;margin-bottom:9px;}p small{font-size:11px;color:#bfbfbf;}
h1,h2,h3,h4,h5,h6{font-weight:bold;color:#404040;}h1 small,h2 small,h3 small,h4 small,h5 small,h6 small{color:#bfbfbf;}
h1{margin-bottom:18px;font-size:30px;line-height:36px;}h1 small{font-size:18px;}
h2{font-size:24px;line-height:36px;}h2 small{font-size:14px;}
h3,h4,h5,h6{line-height:36px;}
h3{font-size:18px;}h3 small{font-size:14px;}
h4{font-size:16px;}h4 small{font-size:12px;}
h5{font-size:14px;}
h6{font-size:13px;color:#bfbfbf;text-transform:uppercase;}
ul,ol{margin:0 0 18px 25px;}
ul ul,ul ol,ol ol,ol ul{margin-bottom:0;}
ul{list-style:disc;}
ol{list-style:decimal;}
li{line-height:18px;color:#808080;}
ul.unstyled{list-style:none;margin-left:0;}
dl{margin-bottom:18px;}dl dt,dl dd{line-height:18px;}
dl dt{font-weight:bold;}
dl dd{margin-left:9px;}
hr{margin:20px 0 19px;border:0;border-bottom:1px solid #eee;}
strong{font-style:inherit;font-weight:bold;}
em{font-style:italic;font-weight:inherit;line-height:inherit;}
.muted{color:#bfbfbf;}
blockquote{margin-bottom:18px;border-left:5px solid #eee;padding-left:15px;}blockquote p{font-size:14px;font-weight:300;line-height:18px;margin-bottom:0;}
blockquote small{display:block;font-size:12px;font-weight:300;line-height:18px;color:#bfbfbf;}blockquote small:before{content:‘\2014 \00A0‘;}
address{display:block;line-height:18px;margin-bottom:18px;}
code,pre{padding:0 3px 2px;font-family:Monaco, Andale Mono, Courier New, monospace;font-size:12px;-webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px;}
code{padding:1px 3px;}
pre{background-color:#f5f5f5;display:block;padding:8.5px;margin:0 0 18px;line-height:18px;font-size:12px;border:1px solid #ccc;border:1px solid rgba(0, 0, 0, 0.15);-webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px;white-space:pre;white-space:pre-wrap;word-wrap:break-word;}
form{margin-bottom:18px;}
fieldset{margin-bottom:18px;padding-top:18px;}fieldset legend{display:block;padding-left:150px;font-size:19.5px;line-height:1;color:#404040;*padding:0 0 5px 145px;*line-height:1.5;}
form .clearfix{margin-bottom:18px;zoom:1;}form .clearfix:before,form .clearfix:after{display:table;content:"";zoom:1;*display:inline;}
form .clearfix:after{clear:both;}
label,input,select,textarea{font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:13px;font-weight:normal;line-height:normal;}
label{padding-top:6px;font-size:13px;line-height:18px;float:left;width:130px;text-align:right;color:#404040;}
form .input{margin-left:150px;}
input[type=checkbox],input[type=radio]{cursor:pointer;}
input,textarea,select,.uneditable-input{display:inline-block;width:210px;height:18px;padding:4px;font-size:13px;line-height:18px;color:#808080;border:1px solid #ccc;-webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px;}
input[type=checkbox],input[type=radio]{width:auto;height:auto;padding:0;margin:3px 0;*margin-top:0;line-height:normal;border:none;}
input[type=file]{background-color:#ffffff;padding:initial;border:initial;line-height:initial;-webkit-box-shadow:none;-moz-box-shadow:none;box-shadow:none;}
input[type=button],input[type=reset],input[type=submit]{width:auto;height:auto;}
select,input[type=file]{height:27px;line-height:27px;*margin-top:4px;}
select[multiple]{height:inherit;}
textarea{height:auto;}
.uneditable-input{background-color:#ffffff;display:block;border-color:#eee;-webkit-box-shadow:inset 0 1px 2px rgba(0, 0, 0, 0.025);-moz-box-shadow:inset 0 1px 2px rgba(0, 0, 0, 0.025);box-shadow:inset 0 1px 2px rgba(0, 0, 0, 0.025);cursor:not-allowed;}
:-moz-placeholder{color:#bfbfbf;}
::-webkit-input-placeholder{color:#bfbfbf;}
input,textarea{-webkit-transition:border linear 0.2s,box-shadow linear 0.2s;-moz-transition:border linear 0.2s,box-shadow linear 0.2s;-ms-transition:border linear 0.2s,box-shadow linear 0.2s;-o-transition:border linear 0.2s,box-shadow linear 0.2s;transition:border linear 0.2s,box-shadow linear 0.2s;-webkit-box-shadow:inset 0 1px 3px rgba(0, 0, 0, 0.1);-moz-box-shadow:inset 0 1px 3px rgba(0, 0, 0, 0.1);box-shadow:inset 0 1px 3px rgba(0, 0, 0, 0.1);}
input:focus,textarea:focus{outline:0;border-color:rgba(82, 168, 236, 0.8);-webkit-box-shadow:inset 0 1px 3px rgba(0, 0, 0, 0.1),0 0 8px rgba(82, 168, 236, 0.6);-moz-box-shadow:inset 0 1px 3px rgba(0, 0, 0, 0.1),0 0 8px rgba(82, 168, 236, 0.6);box-shadow:inset 0 1px 3px rgba(0, 0, 0, 0.1),0 0 8px rgba(82, 168, 236, 0.6);}
input[type=file]:focus,input[type=checkbox]:focus,select:focus{-webkit-box-shadow:none;-moz-box-shadow:none;box-shadow:none;outline:1px dotted #666;}
form div.clearfix.error{background:#fae5e3;padding:10px 0;margin:-10px 0 10px;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;}form div.clearfix.error>label,form div.clearfix.error span.help-inline,form div.clearfix.error span.help-block{color:#9d261d;}
form div.clearfix.error input,form div.clearfix.error textarea{border-color:#c87872;-webkit-box-shadow:0 0 3px rgba(171, 41, 32, 0.25);-moz-box-shadow:0 0 3px rgba(171, 41, 32, 0.25);box-shadow:0 0 3px rgba(171, 41, 32, 0.25);}form div.clearfix.error input:focus,form div.clearfix.error textarea:focus{border-color:#b9554d;-webkit-box-shadow:0 0 6px rgba(171, 41, 32, 0.5);-moz-box-shadow:0 0 6px rgba(171, 41, 32, 0.5);box-shadow:0 0 6px rgba(171, 41, 32, 0.5);}
form div.clearfix.error .input-prepend span.add-on,form div.clearfix.error .input-append span.add-on{background:#f4c8c5;border-color:#c87872;color:#b9554d;}
table{width:100%;margin-bottom:18px;padding:0;border-collapse:separate;*border-collapse:collapse;font-size:13px;border:1px solid #ddd;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;}table th,table td{padding:10px 10px 9px;line-height:18px;text-align:left;}
table th{padding-top:9px;font-weight:bold;vertical-align:middle;border-bottom:1px solid #ddd;}
table td{vertical-align:top;}
table th+th,table td+td{border-left:1px solid #ddd;}
table tr+tr td{border-top:1px solid #ddd;}
table tbody tr:first-child td:first-child{-webkit-border-radius:4px 0 0 0;-moz-border-radius:4px 0 0 0;border-radius:4px 0 0 0;}
table tbody tr:first-child td:last-child{-webkit-border-radius:0 4px 0 0;-moz-border-radius:0 4px 0 0;border-radius:0 4px 0 0;}
table tbody tr:last-child td:first-child{-webkit-border-radius:0 0 0 4px;-moz-border-radius:0 0 0 4px;border-radius:0 0 0 4px;}
table tbody tr:last-child td:last-child{-webkit-border-radius:0 0 4px 0;-moz-border-radius:0 0 4px 0;border-radius:0 0 4px 0;}
.zebra-striped tbody tr:nth-child(odd) td{background-color:#f9f9f9;}
.zebra-striped tbody tr:hover td{background-color:#f5f5f5;}
.zebra-striped .header{cursor:pointer;}.zebra-striped .header:after{content:"";float:right;margin-top:7px;border-width:0 4px 4px;border-style:solid;border-color:#000 transparent;visibility:hidden;}
.zebra-striped .header:hover:after{visibility:visible;}
footer{margin-top:17px;padding-top:17px;border-top:1px solid #eee;}
.page-header{margin-bottom:17px;border-bottom:1px solid #ddd;-webkit-box-shadow:0 1px 0 rgba(255, 255, 255, 0.5);-moz-box-shadow:0 1px 0 rgba(255, 255, 255, 0.5);box-shadow:0 1px 0 rgba(255, 255, 255, 0.5);}.page-header h1{margin-bottom:8px;}
.close{float:right;color:#000000;font-size:20px;font-weight:bold;line-height:13.5px;text-shadow:0 1px 0 #ffffff;filter:alpha(opacity=20);-khtml-opacity:0.2;-moz-opacity:0.2;opacity:0.2;}.close:hover{color:#000000;text-decoration:none;filter:alpha(opacity=40);-khtml-opacity:0.4;-moz-opacity:0.4;opacity:0.4;}

pre {
padding: 0;
margin: 10px 0px 10px;
overflow: auto; /*--If the Code exceeds the width, a scrolling is available--*/
overflow-Y: hidden; /*--Hides vertical scroll created by IE--*/
}
pre code {
margin: 5px; /*--Left Margin--*/
padding: 0px;
display: block;
line-height: 18px;
}
.center { text-align:center}
.left {text-align:left}
.right {text-align:right}
-->
code {
margin: 0px;
padding: 5px;
border: 0px;
background-color: #f1f1f1;
}
-->

iOS--CoreLocation地理反地理编码

操作步骤

  • 1.导入框架

CoreLocation/CoreLocation.h

  • 2.设置属性

@property (nonatomic, strong) CLGeocoder *geocoder;

  • 3.方法实现

3.1 懒加载创建geocoder属性

- (CLGeocoder *)geocoder
{
    if (!_geocoder) {
        self.geocoder = [[CLGeocoder alloc] init];
    }
    return _geocoder;
}

3.2 地理编码

//地理
- (void)dili
{
    //地理编码方法

    NSString *straddress = @"北京";

    [self.geocoder geocodeAddressString:straddress completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error) { // 有错误
            NSLog(@"找不到该地址");
        } else { // 编码成功

            // 取出最前面的地址
            CLPlacemark *pm = [placemarks firstObject];

            // 取出经纬度
            CGFloat  latitude = pm.location.coordinate.latitude;
            CGFloat longitude =  pm.location.coordinate.longitude;

            //NSLog(@"总共找到%d个地址", placemarks.count);

            for (CLPlacemark *pm in placemarks) {
                NSLog(@"-----地址开始----");

                NSLog(@"%f %f %@", pm.location.coordinate.latitude, pm.location.coordinate.longitude, pm.name);

                [pm.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                    NSLog(@"%@ %@", key, obj);
                }];

                NSLog(@"-----地址结束----");
            }
        }
    }];
}

3.3 反地理编码

//反地理
- (void)fandili
{
    //反地理编码

    // 1.位置
    CLLocationDegrees latitude = 114.0;
    CLLocationDegrees longitude = 45.0;
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

    // 2.反地理编码
    [self.geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error) { // 有错误(地址乱输入)
            NSLog(@"找不到该地址");
        } else { // 编码成功
            // 取出最前面的地址
            CLPlacemark *pm = [placemarks firstObject];
            NSLog(@"%@", pm.name);
            // 设置具体地址

            NSLog(@"总共找到%ld个地址", (long)placemarks.count);

            for (CLPlacemark *pm in placemarks) {
                NSLog(@"-----地址开始----");

                NSLog(@"%f %f %@", pm.location.coordinate.latitude, pm.location.coordinate.longitude, pm.name);

                [pm.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                    NSLog(@"%@ %@", key, obj);
                }];

                NSLog(@"-----地址结束----");
            }
        }
    }];
}

3.4 调用方法

- (void)viewDidLoad {
    [super viewDidLoad];

    [self dili];
    [self fandili];
}
时间: 2024-12-19 22:35:54

iOS地理反地理编码--CoreLocation的相关文章

iOS CLGeocoder反地理编码获取地理位置

要得到当前的位置,只需要2步就能完成 1:判断设备是否支持定位功能,然后创建MKMapView if ([CLLocationManager locationServicesEnabled]) { myMapView =[[MKMapView alloc] init]; myMapView.delegate=self; myMapView.showsUserLocation=YES; } 2:实现MKMapViewDelegate协议 -(void)mapView:(MKMapView *)ma

猫猫学iOS 之CoreLocation反地理编码小Demo输入经纬度得到城市

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 输入经纬度,能够得到相应的地名 二:思路 跟地里编码差点儿相同 1.获取用户输入的经纬度 2.依据用户输入的经纬度创建CLLocation对象 3.依据CLLocation对象获取相应的地标信息 三:代码 #import "ViewController.h" #import <CoreLocation/CoreLocation.h>

iOS 地理编码 / 反地理编码

一.CLGeocoder 地理编码 与 反地理编码 地理编码: 根据给定的地名,获得具体的位置信息(比如经纬度.地址的全称等) // 地理编码方法 -(void)geocodeAddressString:(NSString*)addressStringcompletionHandler:(CLGeocodeCompletionHandler)completionHandler; 反地理编码: 根据给定的经纬度,获得具体的位置信息 // 反地理编码方法 -(void)reverseGeocodeL

地理编码和反地理编码

一.基本概念 地理编码:根据给定的地名,获得具体的位置信息(比如经纬度.地址的全称等) 反地理编码:根据给定的经纬度,获得具体的位置信息 编码器CLGeocoder的每个实例,同时只能处理一个任务,异步执行. 二.基本方法 1. 地理编码方法   - (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler; 2. 反地理

地理反编码的实现

一:效果 输入经纬度,可以得到相应的地名 二:思路 跟地里编码差不多 1.获取用户输入的经纬度 2.根据用户输入的经纬度创建CLLocation对象 3.根据CLLocation对象获取对应的地标信息 三:代码 <code class="hljs objectivec has-numbering"><span class="hljs-preprocessor">#import <span class="hljs-title&q

CLGeocoder 地理编码和反地理编码

使用CLGeocoder可以完成“地理编码”和“反地理编码” 地理编码:根据给定的地名,获得具体的位置信息(比如经纬度.地址的全称等) 反地理编码:根据给定的经纬度,获得具体的位置信息 地理编码方法 - (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler; 反地理编码方法 - (void)reverseGeocode

iOS 原生地图地理编码与反地理编码

当我们要在App实现功能:输入地名,编码为经纬度,实现导航功能. 那么,我需要用到原生地图中的地理编码功能,而在Core Location中主要包含了定位.地理编码(包括反编码)功能. 在文件中导入 #import <CoreLocation/CoreLocation.h> 地理编码: /** 地理编码 */ - (void)geocoder { CLGeocoder *geocoder=[[CLGeocoder alloc]init]; NSString *addressStr = @&qu

iOS 地理和反地理编码

导入CoreLocation框架和对应的主头文件 #import <CoreLocation/CoreLocation.h> 创建CLGeocoder对象 _geoC = [[CLGeocoder alloc] init]; 地理编码 根据地址进行地理编码 [self.geoC geocodeAddressString:@"科技八路" completionHandler:^(NSArray<CLPlacemark *> * __nullable placemar

(七十七)地理编码与反地理编码

所谓地理编码,指的是通过地名获取位置信息,例如经纬度.详细地址等. 所谓反地理编码,指的是通过经纬度.海拔等信息获取地理位置信息. 在iOS上使用地理编码和反地理编码,如果是手动输入经纬度,是不需要获取用户授权的,但是一般是获取用户的经纬度,然后再通过地理编码实现精确定位,因此需要授权,本文因为是单独讲解地理编码的相关知识,因此采用手动输人经纬度,不再赘述授权的代码. ①导入框架: #import <CoreLocation/CoreLocation.h> ②新建CLGeocoder对象: @