ios百度地图地址搜索功能-在线建议查询
来源:互联网 发布:如何编写电脑软件 编辑:程序博客网 时间:2024/05/01 13:53
最近使用到了百度地图,总结一下,话不多说直接上代码:
使用的是searchBar+在线建议查询
一、准备工作
导入头文件#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
二、创建searchBar
1、添加代理<UISearchBarDelegate>
2、创建searchBar
//searchBar- (UISearchBar *)searchBarInit{ if (!_searchBar) { _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 40)]; _searchBar.backgroundColor = [UIColor clearColor]; _searchBar.delegate = self; _searchBar.placeholder = @"请输入目的地"; _searchBar.showsCancelButton = NO; [self.view addSubview:_searchBar]; } return _searchBar;}3、调用代理方法
#pragma mark - searchBar代理- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{ searchBar.showsCancelButton = YES; if (!searchView) { searchView = [[FJSearchView alloc]init]; [self.view addSubview:searchView]; [searchView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.mas_equalTo(self.view); make.top.mas_equalTo(searchBar.mas_bottom).offset(0); }]; }else{ searchView.hidden = NO; } return YES;}- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{ return YES;}- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar __TVOS_PROHIBITED{ searchBar.showsCancelButton = NO; if (!searchView.isHidden) {// [searchView removeFromSuperview]; searchView.hidden = YES; } [searchBar resignFirstResponder]; [searchResultArray removeAllObjects]; [searchView.resultTableView reloadData];}- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ //option是百度搜索类的BMKSuggestionSearchOption,searcher是BMKSuggestionSearch option = [[BMKSuggestionSearchOption alloc] init]; option.cityname = @"重庆市"; option.keyword = searchText; [_searcher suggestionSearch:option];}- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ option = [[BMKSuggestionSearchOption alloc] init]; option.cityname = @"重庆市"; option.keyword = searchBar.text; [_searcher suggestionSearch:option]; [searchBar resignFirstResponder];}
三、创建tableview展示结果
//tableview- (UITableView *)resultTableViewInit{ if (!_resultTableView) { _resultTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; _resultTableView.delegate = self; _resultTableView.dataSource = self; [self addSubview:_resultTableView]; [_resultTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.top.mas_equalTo(self); }]; } return _resultTableView;}#pragma mark - tableView代理#pragma mark - tableView datasource-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _dataArray.count;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"resultCell"]; //自定义的model存储搜索结果的地址名和坐标字段 FJSearchResultModel *model = [[FJSearchResultModel alloc]init]; for (int i = 0; i < _dataArray.count; i++) { if (i == indexPath.row) { model = _dataArray[i]; cell.textLabel.text = model.name; } } return cell;}
四、调用百度地图的搜索接口-最关键部分
添加代理<BMKSuggestionSearchDelegate>
//初始化搜索- (void)setupSearcher{ //初始化检索对象 _searcher =[[BMKSuggestionSearch alloc]init]; _searcher.delegate = self; option = [[BMKSuggestionSearchOption alloc] init]; option.cityname = @"重庆市"; option.keyword = @""; BOOL flag = [_searcher suggestionSearch:option]; if(flag) { NSLog(@"建议检索发送成功"); } else { NSLog(@"建议检索发送失败"); }}
#pragma mark - 在线建议查询代理//实现Delegate处理回调结果- (void)onGetSuggestionResult:(BMKSuggestionSearch*)searcher result:(BMKSuggestionResult*)result errorCode:(BMKSearchErrorCode)error{ if (error == BMK_SEARCH_NO_ERROR) { if (searchResultArray.count != 0) { [searchResultArray removeAllObjects]; } //在此处理正常结果 for (int i = 0; i < result.keyList.count; i++) { FJSearchResultModel *model = [[FJSearchResultModel alloc]init]; model.name = result.keyList[i]; CLLocationCoordinate2D coor; [result.ptList[i] getValue:&coor]; model.kCoordinate = coor; [searchResultArray addObject:model]; } searchView.dataArray = searchResultArray; [searchView.resultTableView reloadData]; } else { NSLog(@"抱歉,未找到结果"); }}
-(void)viewWillDisappear:(BOOL)animated{ _searcher.delegate = nil;}
阅读全文
1 0
- ios百度地图地址搜索功能-在线建议查询
- Android 百度地图 SDK v3_3_0 (五) ---POI搜索和在线建议查询功能
- iOS百度地图在线建议api做搜索区域推荐
- 百度地图SDK-----百度地图在线建议查询,结合AutoCompleteTextView实现搜索下拉列表。
- android 百度地图 在线建议查询
- ios百度地图附近搜索
- 百度地图之Poi搜索功能
- 百度地图3.3带搜索功能
- 百度地图JS 搜索悬浮窗功能
- iOS基于百度地图的开发(3)——地址搜索
- iOS基于百度地图的开发(3)——地址搜索
- 高仿百度外卖地址添加功能(百度地图拖动定位,poi搜索,设置配送范围)
- ios调用百度地图智能联想搜索
- ios百度地图选择地址返回经纬度
- C#调用百度地图精确地址建议API
- 百度地图,搜索功能,地图的高级引用
- 【百度地图API】搜索建议,如何使用suggestion--下拉列表方式的搜索建议
- iOS 百度地图 小功能demo
- GitChat·Python | 零基础小白如何入门 Python 编程
- bzoj2303洛谷3631 异或推理+并查集
- java中的包装类过渡类型转换
- 【LeetCode】661. Image Smoother
- poj 2752 (找既是前缀又是后缀的串的长度)kmp/拓展kmp
- ios百度地图地址搜索功能-在线建议查询
- zookeeper的热备
- eclipse:the method of type must override a superclass method
- Java中 HashMap和Hashtable的区别以及对线程安全的理解
- Android自定义view之ViewPager指示器——2
- RF GBDT XGBOOST的区别与联系
- 深入理解Java的Annotation系列-第四部分 注解的应用-使用注解实现权限管理
- 圣诞树
- Ecmall 基础类分析