百度地图地理编码工具类封装

百度地图地理编码单例工具类封装

封装百度地图地理编码、反地理编码接口于单例工具类中,方便使用。


GeoCoding.h

//
//  GeoCoding.h
//  xxx
//
//  Created by xys on 2018/6/1.
//  Copyright © 2018年 xxx. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef void(^ReverseGeoCodeResultBlock)(NSString * address);

typedef void(^GeoCodeResultBlock)(CLLocationCoordinate2D  coord);


@interface GeoCoding : NSObject

+ (instancetype)defaultGeoCoding;

/*
 *  消毁单例类
 */
+ (void)deallocGeoCoding;


/*
 *  地理编码(即地址转坐标)
 */
- (void)geoCode:(NSString *)address
         result:(GeoCodeResultBlock) result;


/*
 *  逆地理编码(即坐标转地址)
 */
- (void)reverseGeoCode:(CLLocationCoordinate2D)coord
                result:(ReverseGeoCodeResultBlock) result;
@end

GeoCoding.m

//
//  GeoCoding.m
//  xxx
//
//  Created by xys on 2018/6/1.
//  Copyright © 2018年 xxx. All rights reserved.
//

#import "GeoCoding.h"
#import <BaiduMapKit/BaiduMapAPI_Search/BMKSearchComponent.h>

static GeoCoding * geoCoding = nil;
static dispatch_once_t allocOnceToken;
static dispatch_once_t initOnceToken;

@interface GeoCoding()<BMKGeoCodeSearchDelegate>
@property (nonatomic, copy)      ReverseGeoCodeResultBlock    reverseGeoCodeCallback;
@property (nonatomic, copy)      GeoCodeResultBlock           geoCodeCallback;
@property (nonatomic, strong)    BMKGeoCodeSearch           * searcher;
@end

@implementation GeoCoding

#pragma mark - Overrides -

+ (id)allocWithZone:(struct _NSZone *)zone
{
    if (nil == geoCoding) {
        dispatch_once(&allocOnceToken, ^{
            geoCoding = [super allocWithZone:zone];
        });
    }
    return geoCoding;
}


+ (id)copyWithZone:(struct _NSZone *)zone
{
    return geoCoding;
}


+ (id)mutableCopyWithZone:(struct _NSZone *)zone
{
    return geoCoding;
}


- (id)copyWithZone:(NSZone *)zone
{
    return geoCoding;
}


- (id)mutableCopyWithZone:(NSZone *)zone
{
    return geoCoding;
}


- (void)dealloc
{
    DDLogDebug(@"---delloc:%@----",NSStringFromClass([self class]));
}


- (id)init
{
    dispatch_once(&initOnceToken, ^{
        geoCoding = [super init];
        _searcher =[[BMKGeoCodeSearch alloc]init];
        _searcher.delegate = self;
    });
    return geoCoding;
}


#pragma mark - BMKGeoCodeSearchDelegate -
/**
 *返回地址信息搜索结果
 *@param searcher 搜索对象
 *@param result 搜索结BMKGeoCodeSearch果
 *@param error 错误号,@see BMKSearchErrorCode
 */
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR)
    {
        if (self.geoCodeCallback) 
        {
            self.geoCodeCallback(result.location);
        }
    }
    else
    {
        NSLog(@"抱歉,未找到结果");
        if (self.geoCodeCallback)
        {
            self.geoCodeCallback(kCLLocationCoordinate2DInvalid);
        }
    }
    self.geoCodeCallback = nil;
}


/**
 *返回反地理编码搜索结果
 *@param searcher 搜索对象
 *@param result 搜索结果
 *@param error 错误号,@see BMKSearchErrorCode
 */
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher
                           result:(BMKReverseGeoCodeResult *)result
                        errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR)
    {
        if (self.reverseGeoCodeCallback)
        {
            self.reverseGeoCodeCallback(result.address);
        }
    }
    else
    {
        NSLog(@"抱歉,未找到结果");
        if (self.reverseGeoCodeCallback)
        {
            self.reverseGeoCodeCallback(nil);
        }
    }
    self.reverseGeoCodeCallback = nil;
}


#pragma mark - Public methods -

+ (instancetype)defaultGeoCoding
{
    return [[self alloc] init];
}


+ (void)deallocGeoCoding
{
    geoCoding      = nil;
    allocOnceToken = 0;
    initOnceToken  = 0;
}


/*
 *  地理编码(即地址转坐标)
 */
- (void)geoCode:(NSString *)address
         result:(GeoCodeResultBlock)result
{
    BMKGeoCodeSearchOption *geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
    geoCodeSearchOption.address = address;
    BOOL flag = [_searcher geoCode:geoCodeSearchOption];
    if(flag)
    {
        NSLog(@"geo检索发送成功");
        self.geoCodeCallback = result;
    }
    else
    {
        NSLog(@"geo检索发送失败");
        if (result)
        {
            result(kCLLocationCoordinate2DInvalid);
        }
    }
}


/*
 *  逆地理编码(即坐标转地址)
 */
- (void)reverseGeoCode:(CLLocationCoordinate2D)coord
                result:(ReverseGeoCodeResultBlock) result
{
    BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeoCodeSearchOption.reverseGeoPoint = coord;
    BOOL flag = [_searcher reverseGeoCode:reverseGeoCodeSearchOption];
    if(flag)
    {
        NSLog(@"反geo检索发送成功");
        self.reverseGeoCodeCallback = result;
    }
    else
    {
        NSLog(@"反geo检索发送失败");
        if (result)
        {
            result(nil);
        }
    }
}
@end

使用示例

//使用百度地图地理编码
GeoCoding * geoCoding = [GeoCoding defaultGeoCoding];
[geoCoding geoCode:@"江苏省南京市雨花区雨花台风景区" result:^(CLLocationCoordinate2D coord) {
       AddressMapViewController * mapVC = [[AddressMapViewController alloc] initWithCoordinate:coord];
       [self.navigationController pushViewController:mapVC animated:NO];
 }];
GeoCoding * geocoding = [GeoCoding defaultGeoCoding];
[geocoding reverseGeoCode:CLLocationCoordinate2DMake(pointModel.lat, pointModel.lng) result:^(NSString *address) {
      if (nil == address) 
      {
          NSLog(@"反地理编码错误");
      }
      else
      {
          NSLog(@"反地理编码后的地址:%@",address);
      }
 }];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值