/*
*我们这边的需求是,定位到一个城市,用户每输入一个字,就调起百度地图的查找方法,百度地图有提供方法
*/
//关于百度地图的模糊搜索...废话不多讲,马上上代码
//首先,导入
#import <BaiduMapAPI_Search/BMKGeocodeSearch.h>
#import <BaiduMapAPI_Location/BMKLocationService.h>
#import <BaiduMapAPI_Search/BMKPoiSearch.h>
// 我项目下的
#import "MCPressActiviesLocatViewController.h"
// 我项目下的
#import "MCAdressCell.h"
//需要的delegate
//<BMKGeoCodeSearchDelegate,BMKLocationServiceDelegate,BMKPoiSearchDelegate>
@interface MCPressActiviesLocatViewController () <BMKGeoCodeSearchDelegate,BMKLocationServiceDelegate,BMKPoiSearchDelegate,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UISearchControllerDelegate,UISearchResultsUpdating>
// 我项目下的
@property (nonatomic,weak)UITableView *tab;
// 我项目下的
@property (nonatomic,strong)NSMutableArray *datas;
// 我项目下的
@property (nonatomic,strong)UISearchController *searchController;
// 需要用到的属性
@property (nonatomic,strong)BMKPoiSearch *search;
@property (nonatomic,strong)BMKCitySearchOption *op;
@end
@implementation MCPressActiviesLocatViewController
/// 这里cityName = 当前城市 (我这边是东莞) Province = 当前省份 (我这边是广东)
- (instancetype)initWithCityName:(NSString *)cityName Province:(NSString *)province{
if (self = [super init]) {
self.province = province;
self.cityName = cityName;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
self.title = @"请输入地址";
/// 初始化 BMKPoiSearch
self.search = [[BMKPoiSearch alloc]init];
self.search.delegate = self;
[self setupSearchBar];
[self setupTab];
/// 初始化 BMKCitySearchOption
self.op = [[BMKCitySearchOption alloc]init];
self.op.pageIndex = 0;
self.op.pageCapacity = 30;// 结果页数
self.op.city = self.cityName;// 置顶城市
}
/// 获得用户输入的字,并发起关键字搜索
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString *str = searchController.searchBar.text;
if (str.length != 0) {
self.op.keyword = str;
[self.search poiSearchInCity:self.op];
}
}
/**
*返回POI搜索结果
*@param searcher 搜索对象
*@param poiResult 搜索结果列表
*@param errorCode 错误号,@see BMKSearchErrorCode
*/
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResult errorCode:(BMKSearchErrorCode)errorCode{
NSArray *poiInfoList = poiResult.poiInfoList;
[self.datas removeAllObjects];
for (BMKPoiInfo *poi in poiInfoList) {
[self.datas addObject:poi];
}
if (self.datas.count == 0) {
BMKPoiInfo *poi = [[BMKPoiInfo alloc]init];
poi.name = @"暂无结果";
[self.datas addObject:poi];
}
[self.tab reloadData];
}
- (void)setupSearchBar{
self.definesPresentationContext = NO;
_searchController=[[UISearchController alloc]initWithSearchResultsController:nil];
//设置背景不透明
_searchController.searchBar.translucent=NO;
// _searchController.searchBar.barTintColor=[UIColor colorWithHexString:@"#f2f2f2"];
//设置searchbar的边框颜色和背景颜色一致
// _searchController.searchBar.layer.borderWidth=1;
// _searchController.searchBar.layer.borderColor=[[UIColor colorWithHexString:@"#f2f2f2"] CGColor];
_searchController.searchBar.placeholder=@"搜索";
_searchController.searchResultsUpdater = self;
_searchController.dimsBackgroundDuringPresentation = NO;
_searchController.hidesNavigationBarDuringPresentation = NO;
_searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44);
_searchController.searchBar.delegate=self;
_searchController.delegate = self;
_searchController.searchResultsUpdater = self;
//清空tableview多余的空格线
[self.tab setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]];
self.navigationItem.titleView = _searchController.searchBar;
}
/// 我项目下
- (void)dealloc{
self.search.delegate = nil;
}
- (NSMutableArray *)datas{
if (!_datas) {
_datas = [NSMutableArray array];
}
return _datas;
}
- (void)setupTab{
BasicTableView *tab = [BasicTableView basicTableView];
tab.delegate = self;
tab.dataSource = self;
[self.view addSubview:tab];
[tab mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(UIEdgeInsetsZero);
}];
self.tab = tab;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self.searchController.searchBar endEditing:true];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.searchController.searchBar endEditing:true];
BMKPoiInfo *poi = self.datas[indexPath.row];
if ([poi.name isEqualToString:@"暂无结果"]) {
return;
}else{
self.adress = poi.name;
if (self.userEndEditLocat) {
self.userEndEditLocat(self.province,self.cityName,self.adress);
[self.navigationController popViewControllerAnimated:true];
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.datas.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MCAdressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[MCAdressCell alloc]initWithStyle:0 reuseIdentifier:@"cell"];
}
BMKPoiInfo *poi = self.datas[indexPath.row];
cell.left = poi.name;
cell.right = poi.address;
return cell;
}