iOS 百度POI检索

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

QQ20181001-215345.gif
QQ20181001-220430.gif

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

百度地理位置定位工具

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

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

#import <Foundation/Foundation.h>
#import <BMKLocationkit/BMKLocationAuth.h>
#import <BMKLocationkit/BMKLocationComponent.h>
#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()<BMKLocationManagerDelegate,BMKLocationAuthDelegate>
@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 <Foundation/Foundation.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BMKLocationkit/BMKLocationAuth.h>
#import <BMKLocationkit/BMKLocationComponent.h>
#import <BaiduMapAPI_Location/BMKLocationService.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Utils/BMKGeometry.h>
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()<BMKPoiSearchDelegate,BMKGeoCodeSearchDelegate,BMKSuggestionSearchDelegate>
@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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,542评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,822评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,912评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,449评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,500评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,370评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,193评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,074评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,505评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,722评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,841评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,569评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,168评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,783评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,918评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,962评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,781评论 2 354

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明先生_X自主阅读 15,980评论 3 119
  • 每逢八月,无论是大街小巷还是林荫小道都会闻到一股清郁的香气,沁人心脾,那这芳香又是从哪里飘来的呢?原来平日里总是...
    慧蜜xing阅读 484评论 1 4