來來來實(shí)現(xiàn)百度地圖模糊搜索,先看一下效果
主要用到了BMKCitySearchOption
這個城市內(nèi)檢索腿箩,下面我們來說一下實(shí)現(xiàn)過程
實(shí)現(xiàn)步驟
1.引入頭文件,代理愕掏、協(xié)議度秘,創(chuàng)建搜索全局屬性和需要的屬性
#import "TJMapAddressViewController.h"
#import "TJAddressTableIViewCell.h"
#import "TJAddressModel.h"
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
@interface TJMapAddressViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,BMKPoiSearchDelegate,BMKGeoCodeSearchDelegate,UIScrollViewDelegate>
{
BMKPoiSearch *_poiSearch; //poi搜索
BMKGeoCodeSearch *_geocodesearch; //geo搜索服務(wù)
}
@property (nonatomic,strong) UISearchBar *inputAddTF;//輸入地址框
@property (nonatomic,strong) NSMutableArray *addressArray;//搜索的地址數(shù)組
@property (nonatomic , strong)UITableView *addressTableView;
@property (nonatomic , strong)NSMutableArray *dataArr;
@property (nonatomic,strong) TJAddressModel *model;
@property (nonatomic , strong)NSString *addressString;
/****
<保存用戶輸入的地址信息>
*****/
@property (nonatomic , strong)NSString *inPutAddressStr;
@end
2.設(shè)置取消代理
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
_poiSearch.delegate = self;
_geocodesearch.delegate = self;
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
_poiSearch.delegate = nil; // 不用時,置nil
_geocodesearch.delegate = nil;
}
<br />
3.實(shí)現(xiàn)代理方法
#pragma mark --------- poi 代理方法
-(void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode
{
if(errorCode == BMK_SEARCH_NO_ERROR)
{
/***
<這一步很重要饵撑,清空裝載數(shù)據(jù)數(shù)組>
***/
self.addressArray = [NSMutableArray array];
[self.addressArray removeAllObjects];
for (BMKPoiInfo *info in poiResult.poiInfoList) {
_model = [[TJAddressModel alloc] init];
_model.titleStr = info.name;
_model.detailStr = info.address;
[self.addressArray addObject:_model];
}
[self.addressTableView reloadData];
}
NSLog(@"self.addressArray === %@",self.addressArray);
}
搜索searchBar 的代理方法
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
self.inPutAddressStr = searchText;
NSLog(@"searchBarText === %@",self.inPutAddressStr);
_poiSearch = [[BMKPoiSearch alloc]init];
_poiSearch.delegate = self;
BMKCitySearchOption *citySearchOption = [[BMKCitySearchOption alloc]init];
citySearchOption.pageIndex = 0;
citySearchOption.pageCapacity = 30;
/***
<此處的city屬性需要定位城市傳過來剑梳,我寫了固定值>
***/
citySearchOption.city= @"成都市";
citySearchOption.keyword = searchText;
BOOL flag = [_poiSearch poiSearchInCity:citySearchOption];
if(flag)
{
NSLog(@"城市內(nèi)檢索發(fā)送成功");
}
else
{
NSLog(@"城市內(nèi)檢索發(fā)送失敗");
}
}
<br />