iOS 百度POI检索

代码demo,GitHub:(https://github.com/songfeiSF/SFBaiDuPoiTool/tree/master)

QQ20181001-215345.gif
iOS 百度POI检索_第1张图片
QQ20181001-220430.gif

   公司提了新的需求,用百度地图定位检索周边,要求仿照微信的地理位置分享。我看了下,封装了几个工具类。实现效果如上图。

百度地理位置定位工具

   百度地图API已经把定位功能和地图功能分开了。下面定位工具只需要导入定位API:

//
//  BaiDuLocationTool.h
//
//  Created by songfei on 2018/4/3.
//

#import 
#import 
#import 
#import "AddressModel.h"
@interface BaiDuLocationTool : NSObject
+(instancetype) sharedInstance;

/**
 获取地理位置

 @param block 返回
 */
-(void)getAddressDetail:(void(^)(AddressModel *addressModel)) block;

//获取两个坐标间的距离
- (CGFloat)BMKPointToPointDistence:(CLLocationCoordinate2D)point1 point2:(CLLocationCoordinate2D)point2;
@end

//
//  BaiDuLocationTool.m
//
//  Created by songfei on 2018/4/3.
//  Copyright © 2018年
//

#import "BaiDuLocationTool.h"
#import "Constant.h"
static BaiDuLocationTool * _instance = nil;
@interface BaiDuLocationTool()
@property (nonatomic ,strong) BMKLocationManager *locationManager;
@end
@implementation BaiDuLocationTool
+(instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone ];
        [_instance locationManager];
    });
    return _instance;
}

+(instancetype) sharedInstance{
    if (_instance == nil) {
        _instance = [[super alloc]init];
    }
    return _instance;
}

- (BMKLocationManager *)locationManager{
    if(!_locationManager){
        [[BMKLocationAuth sharedInstance] checkPermisionWithKey:[MyAPPKeys getBaiDuKey] authDelegate:self];
        //初始化实例
        _locationManager = [[BMKLocationManager alloc] init];
        //设置delegate
        _locationManager.delegate = self;
        //设置返回位置的坐标系类型
        _locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
        //设置距离过滤参数
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        //设置预期精度参数
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        //设置应用位置类型
        _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
        //设置是否自动停止位置更新
        _locationManager.pausesLocationUpdatesAutomatically = NO;
        //设置是否允许后台定位
        _locationManager.allowsBackgroundLocationUpdates = NO;
        //设置位置获取超时时间
        _locationManager.locationTimeout = 10;
        //设置获取地址信息超时时间
        _locationManager.reGeocodeTimeout = 10;
    }
    return _locationManager;
}


//获取当前详细地址
-(void)getAddressDetail:(void(^)(AddressModel *addressModel)) block{
    [self.locationManager requestLocationWithReGeocode:true withNetworkState:true completionBlock:^(BMKLocation * _Nullable location, BMKLocationNetworkState state, NSError * _Nullable error) {
        AddressModel *moel = [[AddressModel alloc]init];
        
        if (error) {
//            SHOW_MSG([error.userInfo valueForKey:@"NSLocalizedDescription"]);
        }else{
            NSLog(@"location --- %@",location);
            moel.latitude = location.location.coordinate.latitude;
            moel.longitude = location.location.coordinate.longitude;
            moel.province = location.rgcData.province;
            moel.city = location.rgcData.city;
            moel.district = location.rgcData.district;
            moel.street = location.rgcData.street;
            moel.streetNumber = location.rgcData.streetNumber;
            moel.locationDescribe = location.rgcData.locationDescribe;
            moel.userName = location.rgcData.locationDescribe;
        }
        block(moel);
    }];
}


#pragma mark --BMKLocationAuthDelegate
//定位授权
- (void)onCheckPermissionState:(BMKLocationAuthErrorCode)iError{
    if(iError == BMKLocationAuthErrorSuccess){
        NSLog(@"定位授权成功");
    }else{
        NSLog(@"定位授权失败");
    }
}

#pragma mark --BMKLocationManagerDelegate
//定位返回 : 移动定位时 多次
-(void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error{
    
}

- (CGFloat)BMKPointToPointDistence:(CLLocationCoordinate2D)point1 point2:(CLLocationCoordinate2D)point2{
    CLLocation *orig=[[CLLocation alloc] initWithLatitude:point1.latitude longitude:point1.longitude];
    CLLocation* dist=[[CLLocation alloc] initWithLatitude:point2.latitude longitude:point2.longitude];

    CLLocationDistance meters=[orig distanceFromLocation:dist];
    NSLog(@"距离:%f",meters);
    return meters;
}

@end

百度Poi检索服务Tool

   百度Poi检索 分以自己为中心检索,以城市建设,还有一个是 地点输入提示检索(Sug检索,与Poi检索平行关系,但是为了方便,写到一个封装里了)
注意:Poi和sug检索需要关键词,没有关键词是检索不成功的

//
//  PoiSearchHelpTool.h
//
//  Created by songfei on 2018/9/27.
//
#import 
#import 
#import 
#import 
#import 
#import 
#import 
NS_ASSUME_NONNULL_BEGIN
typedef void (^PoiSearchBlcok)(id object);
@interface PoiSearchHelpTool : NSObject

/**
 查询的页码
 */
@property (nonatomic ,assign) int pageIndext;

/**
 单利

 @return 返回
 */
+(instancetype) sharedInstance;



/**
 开启代理 在viewWillAppear里调用
 */
- (void)setDelegate;


/**
 移除代理 在viewWillDisappear里调用
 */
- (void)removeDelegate;

/**
 开始以城市poi搜索

 @param city 城市
 @param keyword 关键词
 @param blcok 返回
 */
- (void)startPoiSearchWihtCity:(NSString*)city keyword:(NSString*)keyword block:(PoiSearchBlcok)blcok;


/**
 开始搜索周围

 @param centerCoordinate 坐标
 @param keyword 关键词
 @param blcok 返回
 */
- (void)startPoiSearchWihtNearyBy:(CLLocationCoordinate2D)centerCoordinate keyword:(NSString*)keyword block:(PoiSearchBlcok)blcok;



/**
 地点输入提示检索(Sug检索)

 @param city 城市
 @param keyword 关键词
 @param blcok 返回
 */
- (void)startSugSearchWihtCity:(NSString*)city  keyword:(NSString*)keyword block:(PoiSearchBlcok)blcok;

/**
 开始反地理编码

 @param centerCoordinate 经纬度坐标
 @param blcok 返回
 */
- (void)startGeoSearchWithLocation:(CLLocationCoordinate2D)centerCoordinate block:(PoiSearchBlcok)blcok;
@end

NS_ASSUME_NONNULL_END



//
//  PoiSearchHelpTool.m
//
//  Created by songfei on 2018/9/27.
//

#import "PoiSearchHelpTool.h"

static PoiSearchHelpTool * _instance = nil;

@interface PoiSearchHelpTool()
@property(nonatomic,strong) BMKPoiSearch * poisearch;
@property (nonatomic ,strong)BMKGeoCodeSearch *geoSearcher;

@property (nonatomic ,strong)BMKSuggestionSearch *sugSearcher;

@property (nonatomic ,copy)PoiSearchBlcok poiSearchBlcok;
@end

@implementation PoiSearchHelpTool

+(instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone ];
        [_instance setSearcherInit];
    });
    return _instance;
}

+(instancetype) sharedInstance{
    if (_instance == nil) {
        _instance = [[super alloc]init];
    }
    return _instance;
}

- (void)setSearcherInit{
    //初始化POI搜索服务
    self.poisearch =[[BMKPoiSearch alloc]init];
    //初始化GEO检索服务
    self.geoSearcher = [[BMKGeoCodeSearch alloc]init];
    //初始化 地点输入提示服务
    self.sugSearcher = [[BMKSuggestionSearch alloc] init];
}

- (void)setDelegate{
    self.poisearch.delegate = self;
    self.geoSearcher.delegate = self;
    self.sugSearcher.delegate = self;
}

- (void)removeDelegate{
    self.poisearch.delegate = nil;
    self.geoSearcher.delegate = nil;
    self.sugSearcher.delegate = nil;
}


#pragma mark --开始POI检索
- (void)startPoiSearchWihtCity:(NSString*)city keyword:(NSString*)keyword block:(PoiSearchBlcok)blcok{
    //请求参数类BMKCitySearchOption
    BMKCitySearchOption *citySearchOption = [[BMKCitySearchOption alloc]init];
    citySearchOption.pageIndex = self.pageIndext;
    citySearchOption.pageCapacity = 20;
    citySearchOption.city= city;
    citySearchOption.keyword = keyword;
    //发起城市内POI检索
    BOOL flag = [self.poisearch poiSearchInCity:citySearchOption];
    if(flag) {
        NSLog(@"POI城市内检索发送成功");
        self.poiSearchBlcok = blcok;
    }
    else {
        NSLog(@"POI城市内检索发送失败");
    }
}

- (void)startPoiSearchWihtNearyBy:(CLLocationCoordinate2D)centerCoordinate keyword:(NSString*)keyword block:(PoiSearchBlcok)blcok{
    //请求参数类BMKCitySearchOption
    BMKNearbySearchOption *nearbySearchOption = [[BMKNearbySearchOption alloc]init];
    nearbySearchOption.pageIndex = self.pageIndext;
    nearbySearchOption.pageCapacity = 20;
    nearbySearchOption.location= centerCoordinate;
    nearbySearchOption.radius = 500;
    nearbySearchOption.sortType = BMK_POI_SORT_BY_DISTANCE;
    nearbySearchOption.keyword = keyword;
    //发起城市内POI检索
    BOOL flag = [self.poisearch poiSearchNearBy:nearbySearchOption];
    if(flag) {
        NSLog(@"POI附近范围检索发送成功");
        self.poiSearchBlcok = blcok;
    }
    else {
        NSLog(@"POI附近范围检索发送失败");
    }
}



- (void)startSugSearchWihtCity:(NSString*)city  keyword:(NSString*)keyword block:(PoiSearchBlcok)blcok{
    //初始化检索对象
    BMKSuggestionSearchOption* option = [[BMKSuggestionSearchOption alloc] init];
    option.cityname = city;
    option.keyword  = keyword;
    BOOL flag = [self.sugSearcher suggestionSearch:option];
    if(flag)
    {
        NSLog(@"Sug检索发送成功");
        self.poiSearchBlcok = blcok;
    }
    
    else
    {
        NSLog(@"Sug检索发送失败");
    }
}


//反地理编码查找周边 根据坐标查询 详细地址
- (void)startGeoSearchWithLocation:(CLLocationCoordinate2D)centerCoordinate block:(PoiSearchBlcok)blcok{
    //反地理编码 根据坐标找地址
    BMKReverseGeoCodeOption *geoCodeOption = [[BMKReverseGeoCodeOption alloc]init];
    geoCodeOption.reverseGeoPoint = centerCoordinate;
    BOOL flag = [self.geoSearcher reverseGeoCode:geoCodeOption];
    if(flag){
        NSLog(@"geo检索发送成功");
        self.poiSearchBlcok = blcok;
    }else{
        NSLog(@"geo检索发送失败");
    }
}

#pragma mark --BMKPoiSearchDelegate POI检索回调代理
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode{
    if (errorCode == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        if (self.poiSearchBlcok) {
            self.poiSearchBlcok(poiResult.poiInfoList);
        }
    }
    else if (errorCode == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        //当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表
        // result.cityList;
        NSLog(@"起始点有歧义");
        if (self.poiSearchBlcok) {
            self.poiSearchBlcok(@[]);
        }
    }
    else {
        NSLog(@"抱歉,未找到结果");
        if (self.poiSearchBlcok) {
            self.poiSearchBlcok(@[]);
        }
    }
    
}

#pragma mark --BMKSuggestionSearchDelegate 地点输入提示检索(Sug检索)
- (void)onGetSuggestionResult:(BMKSuggestionSearch *)searcher result:(BMKSuggestionResult *)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        if (self.poiSearchBlcok) {
            self.poiSearchBlcok(result.keyList);
        }
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        //当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表
        // result.cityList;
        NSLog(@"起始点有歧义");
        if (self.poiSearchBlcok) {
            self.poiSearchBlcok(@[]);
        }
    }
    else {
        if (self.poiSearchBlcok) {
            self.poiSearchBlcok(@[]);
        }
        NSLog(@"抱歉,未找到结果");
    }
}

#pragma mark --BMKGeoCodeSearchDelegate 反地理编码回调代理
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
        if (self.poiSearchBlcok) {
            self.poiSearchBlcok(result);
        }
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        //当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表
        // result.cityList;
        NSLog(@"起始点有歧义");
    }
    else{
        NSLog(@"geo检索结果有误");
    }
}

@end

你可能感兴趣的:(iOS 百度POI检索)