分享百度地图自己封装的工具类
#import <Foundation/Foundation.h>
@interface TDMapTool : NSObject
/**
* 失败Block
*
* @param errorStr ..
*/
__strong typedef void(^failBlock)(NSString *errorStr);
__strong typedef void(^searchAreaBlock)(BMKDistrictResult *result);
__strong typedef void(^searchGeoCodeBlock)(BMKGeoCodeSearchResult *result);
/**
行政区域检索
@param city 检索对象 城市
@param district 检索对象 区县
@param success 检索结果成功
@param failure 检索结果失败
*/
- (void)search_area_city:(NSString*)city
district:(NSString*)district
success:(searchAreaBlock)success
failure:(failBlock)failure;
/**
@param city 检索对象 城市
@param address 检索对象 标准的结构化地址信息
@param success 检索结果成功
@param failure 检索结果失败
*/
-(void)search_geoCode_city:(NSString*)city
address:(NSString*)address
success:(searchGeoCodeBlock)success
failure:(failBlock)failure;
//根据多个点生成多边形
- (BMKPolygon *)transferPathStringToPolygon:(NSString *)path;
@end
#import "TDMapTool.h"
#import <MapKit/MapKit.h>
#import "CLLocation+ABLocationTransform.h"
@interface TDMapTool()<BMKDistrictSearchDelegate,BMKGeoCodeSearchDelegate>
@property(nonatomic,copy) failBlock failure;
@property(nonatomic,copy) searchAreaBlock success_area;
@property(nonatomic,copy) searchGeoCodeBlock success_geoCode;
@property(nonatomic,strong)BMKDistrictSearch * districtSearch;//行政区域检索
@property(nonatomic,strong)BMKDistrictSearchOption * districtSearchOption;//行政区域检索
@property(nonatomic,strong)BMKDistrictResult * districtResult;//检索result
@property(nonatomic,strong)BMKGeoCodeSearch * geoCode;//geo搜索服务
@property(nonatomic,strong)BMKGeoCodeSearchOption * geoCodeOption;//geo搜索服务 正地理编码参数信息类
@end
@implementation TDMapTool
#pragma --- init
- (instancetype)init {
self = [super init];
if (self) {
self.districtSearch = [[BMKDistrictSearch alloc] init];
self.districtSearchOption = [[BMKDistrictSearchOption alloc] init];
self.geoCode = [[BMKGeoCodeSearch alloc] init];
self.geoCodeOption = [[BMKGeoCodeSearchOption alloc]init];
self.geoCode.delegate = self;
self.districtSearch.delegate = self;
}
return self;
}
- (void)dealloc {
self.districtSearch.delegate = nil;
self.geoCode.delegate = nil;
}
/***********************************************************************************************************************************************************************************************************/
/**
行政区域检索
@param city 检索对象 城市
@param district 检索对象 区县
@param success 检索结果成功
@param failure 检索结果失败
*/
-(void)search_area_city:(NSString*)city
district:(NSString*)district
success:(searchAreaBlock)success
failure:(failBlock)failure
{
self.districtSearchOption.city = city;
self.districtSearchOption.district = district;
[self.districtSearch districtSearch:self.districtSearchOption];
self.success_area = [success copy];
self.failure = [failure copy];
}
#pragma mark - 行政区域检索
#pragma mark - BMKDistrictSearchDelegate
/**
行政区域检索结果回调
@param searcher 检索对象
@param result 行政区域检索结果
@param error 错误码,@see BMKCloudErrorCode
*/
- (void)onGetDistrictResult:(BMKDistrictSearch *)searcher result:(BMKDistrictResult *)result errorCode:(BMKSearchErrorCode)error {
if (error == BMK_SEARCH_NO_ERROR) {
self.success_area(result);
} else {
NSString *err = [self returnSearchError:error];
self.failure(err);
}
}
- (BMKPolygon *)transferPathStringToPolygon:(NSString *)path {
NSUInteger pathCount = [path componentsSeparatedByString:@";"].count;
if (pathCount > 0) {
BMKMapPoint points[pathCount];
NSArray *pointsArray = [path componentsSeparatedByString:@";"];
for (NSUInteger i = 0; i < pathCount; i ++) {
if ([pointsArray[i] rangeOfString:@","].location != NSNotFound) {
NSArray *coordinates = [pointsArray[i] componentsSeparatedByString:@","];
points[i] = BMKMapPointMake([coordinates.firstObject doubleValue], [coordinates .lastObject doubleValue]);
}
}
/**
根据多个点生成多边形
points 直角坐标点数组,这些点将被拷贝到生成的多边形对象中
count 点的个数
新生成的多边形对象
*/
BMKPolygon *polygon = [BMKPolygon polygonWithPoints:points count:pathCount];
return polygon;
}
return nil;
}
/***********************************************************************************************************************************************************************************************************/
#pragma mark -正向地理编码检索
#pragma mark - Search Data
/**
@param city 检索对象 城市
@param address 检索对象 标准的结构化地址信息
@param success 检索结果成功
@param failure 检索结果失败
*/
-(void)search_geoCode_city:(NSString*)city
address:(NSString*)address
success:(searchGeoCodeBlock)success
failure:(failBlock)failure
{
self.geoCodeOption.city = city;
self.geoCodeOption.address = address;
[self.geoCode geoCode:self.geoCodeOption];
self.success_geoCode = [success copy];
self.failure = [failure copy];
}
#pragma mark - BMKGeoCodeSearchDelegate
/**
正向地理编码检索结果回调
@param searcher 检索对象
@param result 正向地理编码检索结果
@param error 错误码,@see BMKCloudErrorCode
*/
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeSearchResult *)result errorCode:(BMKSearchErrorCode)error {
//BMKSearchErrorCode错误码,BMK_SEARCH_NO_ERROR:检索结果正常返回
if (error == BMK_SEARCH_NO_ERROR) {
self.success_geoCode(result);
} else {
NSString *err = [self returnSearchError:error];
self.failure(err);
}
}
- (NSString*)returnSearchError:(BMKSearchErrorCode)error {
NSString *errorMes;
switch (error) {
case BMK_SEARCH_AMBIGUOUS_KEYWORD:
errorMes = @"检索词有岐义";
break;
case BMK_SEARCH_AMBIGUOUS_ROURE_ADDR:
errorMes = @"检索地址有岐义";
break;
case BMK_SEARCH_NOT_SUPPORT_BUS:
errorMes = @"该城市不支持公交搜索";
break;
case BMK_SEARCH_NOT_SUPPORT_BUS_2CITY:
errorMes = @"不支持跨城市公交";
break;
case BMK_SEARCH_RESULT_NOT_FOUND:
errorMes = @"没有找到检索结果";
break;
case BMK_SEARCH_ST_EN_TOO_NEAR:
errorMes = @"起终点太近";
break;
case BMK_SEARCH_KEY_ERROR:
errorMes = @"key错误";
break;
case BMK_SEARCH_NETWOKR_ERROR:
errorMes = @"网络连接错误";
break;
case BMK_SEARCH_NETWOKR_TIMEOUT:
errorMes = @"网络连接超时";
break;
case BMK_SEARCH_PERMISSION_UNFINISHED:
errorMes = @"还未完成鉴权,请在鉴权通过后重试";
break;
case BMK_SEARCH_INDOOR_ID_ERROR:
errorMes = @"室内图ID错误";
break;
case BMK_SEARCH_FLOOR_ERROR:
errorMes = @"室内图检索楼层错误";
break;
case BMK_SEARCH_INDOOR_ROUTE_NO_IN_BUILDING:
errorMes = @"起终点不在支持室内路线的室内图内";
break;
case BMK_SEARCH_INDOOR_ROUTE_NO_IN_SAME_BUILDING:
errorMes = @"起终点不在同一个室内";
break;
case BMK_SEARCH_PARAMETER_ERROR:
errorMes = @"参数错误";
break;
case BMK_SEARCH_SERVER_ERROR:
errorMes = @"服务器错误";
break;
default:
break;
}
return errorMes;
}
@end