码迷,mamicode.com
首页 > 其他好文 > 详细

地理编码

时间:2016-04-25 19:30:32      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

 

1、使用CLGeocoder可以完成“地理编码”和“反地理编码”

 

(1)地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)

(2)反地理编码:根据给定的经纬度,获得具体的位置信息

 

2、地理编码方法

 

(1)编码

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

(2)反编码

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

 

3、CLGeocodeCompletionHandler

 

(1)当地理\反地理编码完成时,就会调用CLGeocodeCompletionHandler

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

 

(2)这个block传递2个参数

  error :当编码出错时(比如编码不出具体的信息)有值

  placemarks :里面装着CLPlacemark对象

 

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

 

(1)地理位置

@property (nonatomic, readonly) CLLocation *location;

 

(2)区域

@property (nonatomic, readonly) CLRegion *region;

 

(3)详细的地址信息

@property (nonatomic, readonly) NSDictionary *addressDictionary;

 

(4)地址名称

@property (nonatomic, readonly) NSString *name;

 

(5)城市

@property (nonatomic, readonly) NSString *locality;

 

 

5、示例代码

 

#import "ViewController.h"

#import <CoreLocation/CoreLocation.h>

@interface ViewController ()

@property (nonatomic, strong) CLGeocoder *geocode;

@end


@implementation ViewController

#pragma mark - 懒加载

- (CLGeocoder *)geocode {

    if (!_geocode) {

        _geocode = [[CLGeocoder alloc]init];

    }

    return _geocode;

}

- (IBAction)geoCodeClick:(UIButton *)sender {
 //北京  经纬度

    [self.geocode geocodeAddressString:@"东莞" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        //地标

        CLPlacemark *pl =  [placemarks firstObject];

        //经纬度

        CLLocationCoordinate2D coordinate = pl.location.coordinate;

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

        for ( CLPlacemark *pl in placemarks) {

            NSLog(@"%@",pl.name);

        }

    }];

}

- (IBAction)unGeoClick:(UIButton *)sender {

    CLLocation *loction = [[CLLocation alloc]initWithLatitude:23 longitude:118];

    [self.geocode reverseGeocodeLocation:loction completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        //地址  字符串

        NSLog(@"%@",error);

        CLPlacemark *pl = [placemarks firstObject];

        NSLog(@"%@,%@",pl.name,pl.locality);    

    }];

}

 

@end

 

地理编码

标签:

原文地址:http://www.cnblogs.com/cleven/p/5432240.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!