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

猫猫分享,必须精品

原创文章,欢迎转载。转载请注明:翟乃玉的博客

地址:http://blog.csdn.net/u013357243

一:效果

输入经纬度,能够得到相应的地名

二:思路

跟地里编码差点儿相同

1.获取用户输入的经纬度

2.依据用户输入的经纬度创建CLLocation对象

3.依据CLLocation对象获取相应的地标信息

三:代码

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@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

- (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]];

    // 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;
}

@end

四:知识扩充CLGeocoder

使用CLGeocoder能够完毕“地理编码”和“反地理编码”

地理编码:依据给定的地名。获得详细的位置信息(比方经纬度、地址的全称等)

反地理编码:依据给定的经纬度,获得详细的位置信息

->地理编码方法

- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;

->反地理编码方法

- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;

CLGeocodeCompletionHandler

当地理\反地理编码完毕时,就会调用

CLGeocodeCompletionHandler typedef void (^CLGeocodeCompletionHandler)(NSArray *placemarks, NSError *error);

这个block传递2个參数

error :当编码出错时(比方编码不出详细的信息)有值

placemarks :里面装着CLPlacemark对象

CLPlacemark

CLPlacemark的字面意思是地标,封装详细的地址位置信息

地理位置

@property (nonatomic, readonly) CLLocation *location;

区域

@property (nonatomic, readonly) CLRegion *region;

详细的地址信息

@property (nonatomic, readonly) NSDictionary *addressDictionary;

地址名称

@property (nonatomic, readonly) NSString *name;

城市

@property (nonatomic, readonly) NSString *locality;

结构图

时间: 2024-08-26 22:36:04

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

iOS 地理和反地理编码

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

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

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

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

iOS 地理编码 / 反地理编码

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

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上使用地理编码和反地理编码,如果是手动输入经纬度,是不需要获取用户授权的,但是一般是获取用户的经纬度,然后再通过地理编码实现精确定位,因此需要授权,本文因为是单独讲解地理编码的相关知识,因此采用手动输人经纬度,不再赘述授权的代码. ①导入框架: #import <CoreLocation/CoreLocation.h> ②新建CLGeocoder对象: @

地理编码和反地理编码

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

IOS中CoreLocation框架地理定位

1.CoreLocation框架使用前提: #import <CoreLocation/CoreLocation.h> CoreLocation框架中所有数据类型的前缀都是CL ,CoreLocation中使用CLLocationManager对象来做用户定位 2.CLLocationManager的常用操作: 开始用户定位 - (void)startUpdatingLocation; 停止用户定位 - (void) stopUpdatingLocation; 当调用了startUpdatin

地理编码与反地理编码

#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //地理编码 [self geocoder:nil]; //反地理编码; } //没联网,没办法执行 -(void)geocoder