iOS CoreLocation地理编码

一、简单说明

CLGeocoder:地理编码器,其中Geo是地理的英文单词Geography的简写。

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

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

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

 

(1)地理编码方法

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

(2)反地理编码方法

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

 

2.CLGeocodeCompletionHandler

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

  

这个block传递2个参数

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

placemarks :里面装着CLPlacemark对象

 

3.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;

别的 不多说 直接上代码 

//
//  QYDCoreLocation.h
//  QiyiDai
//
//  Created by muzhenhua on 15/4/16.
//  Copyright (c) 2015年 leichi. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
typedef void (^getlocationFinish)(id result);
typedef void (^getaddressFinish)(id result);
@interface QYDCoreLocation : NSObject
+(void)getlocationwith:(NSString *)address completeBlock:(getlocationFinish)block;
+(void)getaddresswith:(CLLocation *)location completeBlock:(getaddressFinish)block;
@end

//
//  QYDCoreLocation.m
//  QiyiDai
//
//  Created by muzhenhua on 15/4/16.
//  Copyright (c) 2015年 leichi. All rights reserved.
//

#import "QYDCoreLocation.h"

@implementation QYDCoreLocation
static CLGeocoder *_geocoder;
static dispatch_once_t onceToken;
/**
 *	@brief	地理编码:地名—>经纬度坐标
 *
 *	@param 	address 	address 地名
 *	@param 	block 	返回经纬度坐标
 */
+(void)getlocationwith:(NSString *)address completeBlock:(getlocationFinish)block
{
    dispatch_once(&onceToken, ^{
        if (_geocoder==nil) {
            _geocoder=[[CLGeocoder alloc]init];
        }
    });
    //经度和纬度
   __block CLLocation *location=nil;
    if (address.length==0) return ;
    //说明:调用下面的方法开始编码,不管编码是成功还是失败都会调用block中的方法
    [_geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error)
     {
         if (block!=nil) {
             if (error||placemarks.count==0) {
                 ZHLog(@"地理编码 地名—>经纬度坐标 失败");
             }else
             {
                  //打印查看找到的所有的位置信息
                  /*
                  firstPlacemark.name:名称
                  firstPlacemark.locality:城市
                  firstPlacemark.country:国家
                  firstPlacemark.postalCode:邮政编码
                  */
                 for (CLPlacemark *placemark in placemarks) {
                     //取出获取的地理信息数组中的第一个显示在界面上
                     CLPlacemark *firstPlacemark=[placemarks firstObject];
                     location =firstPlacemark.location;
//    //纬度
//    CLLocationDegrees latitude=firstPlacemark.location.coordinate.latitude;
//    //经度
//    CLLocationDegrees longitude=firstPlacemark.location.coordinate.longitude;
                     block(location);
                 }
             }
         }
     }];
}
+(void)getaddresswith:(CLLocation *)location completeBlock:(getaddressFinish)block
{
    dispatch_once(&onceToken, ^{
        if (_geocoder==nil) {
            _geocoder=[[CLGeocoder alloc]init];
        }
    });
    if (location==nil) return ;
    //说明:调用下面的方法开始编码,不管编码是成功还是失败都会调用block中的方法
    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
     {
         if (block!=nil) {
             if (error||placemarks.count==0) {
                 ZHLog(@"地理编码 经纬度坐标—>地名 失败");
             }else
             {
                 for (CLPlacemark *placemark in placemarks) {
                     //取出获取的地理信息数组中的第一个显示在界面上
                     CLPlacemark *firstPlacemark=[placemarks firstObject];
                     block(firstPlacemark);
                 }
             }
         }
     }];
}
/**地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
[QYDCoreLocation getlocationwith:@"郑州" completeBlock:^(id reult)
 {
     CLLocation *location=(CLLocation *)reult;
     //经纬度
     CLLocationDegrees latitude=location.coordinate.latitude;
     CLLocationDegrees longitude=location.coordinate.longitude;
 }];
 **/
/**反地理编码:根据给定的经纬度,获得具体的位置信息
[QYDCoreLocation getaddresswith:userLocation.location completeBlock:^(id result)
 {
     CLPlacemark *firstPlacemark=(CLPlacemark *)result;
     NSString *name=firstPlacemark.name;//名称
     NSString *locality=firstPlacemark.locality;//城市
     NSString *country=firstPlacemark.country;//国家
     NSString *postalCode=firstPlacemark.postalCode;//邮政编码
 }];
 **/
@end


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值