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

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

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


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


/**
 *返回反地理编码搜索结果
 *@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);
        }
    }
}


#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);
      }
 }];

猜你喜欢

转载自blog.csdn.net/xuhen/article/details/80578330