ios百度地图地址搜索功能-在线建议查询
最近使用到了百度地图,总结一下,话不多说直接上代码:
使用的是searchBar+在线建议查询
一、准备工作
导入头文件#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
二、创建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;
}
四、调用百度地图的搜索接口-最关键部分
//初始化搜索
- (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;
}
ios百度地图地址搜索功能-在线建议查询相关推荐
- java安卓百度地图查找便利店_Android 百度地图POI搜索功能实例代码
在没介绍正文之前先给大家说下poi是什么意思. 由于工作的关系,经常在文件中会看到POI这三个字母的缩写,但是一直对POI的概念和含义没有很详细的去研究其背后代表的意思.今天下班之前,又看到了POI这 ...
- android 百度地图 在线建议查询,Android 百度地图 SDK v3_3_0 (五) ---POI搜索和在线建议查询功能...
目前百度地图SDK所集成的检索服务包括:POI检索.公交信息查询.线路规划.地理编码.在线建议查询.短串分享. 本篇博客将先介绍POI检索和在线建议查询(在地图地位功能基础上实现的,还不知道定位的童靴 ...
- 最新百度地图—POI搜索功能讲解
这几天都在学习百度地图的事例Demo,后续都会有其它功能的讲解: 对了 我们这里的最新只是代指现在哦!百度地图更新的很快的,最好还是看百度api最权威. 由于上一篇已经讲解过注册文件,这里我就不写那 ...
- vue+百度地图实现搜索功能
这次整理了两个地图,这里的是百度的地图,另一个高德的地图地址https://blog.csdn.net/qq_42165062/article/details/92782197 效果图比较low.需要 ...
- vue 用BMap百度地图 即时搜索功能
功能如下: 搜索框搜索---自动下拉---点击数据---数据显示在搜索框里---点击新增--数据显示在下方--点击删除--删除当前 代码: 首先去百度开发者申请一个key 然后将key引入到项目的 i ...
- vue + 百度地图 地址搜索并定位
1.先上效果图 2.实现方法: <el-input v-model="searchKey" clearable placeholder="请输入搜索地址" ...
- vue中调用百度地图实现搜索等功能
vue中调用百度地图实现搜索等功能 在最近做电商项目时用户订单等物流信息想做的更加详细点所以加入了地图这个小功能,不是很难只是在一个未知的领域可能有点迷茫 现在也是体会到了,学新的东西还是得看官方文档 ...
- iOS百度地图的使用
为什么80%的码农都做不了架构师?>>> 项目最近对地图整体模块进行了重构, 为了和我们的安卓同学保持统一,放弃了原本就很6的高德地图,全部改用百度地图(虽然我觉得百度地图不好 ...
- 百度地图开发-检索功能
转载请注明作者. 注:本人在找工作,地点在北京. 百度地图SDK开发(三)检索功能 目前百度地图SDK所集成的检索服务包括:POI检索.公交信息查询.线路规划.地理编码.行政区边界数据检索.在线建议查 ...
最新文章
- React系列---React+Redux工程目录结构划分
- Python程序退出方式小结(亲测)
- 你真的会搜索?低效的你简直在浪费生命(三)(终结篇)
- Pyhton函数式编程简介(四)装饰器
- 培养沙雕要从娃娃抓起
- java 日志乱码_【开发者成长】JAVA 线上故障排查完整套路!
- 【bzoj1010-toy】斜率优化入门模板
- linux下面实时查看进程,内存以及cpu使用情况使用命令
- 51nod 1185 || 51nod 1072 威佐夫博弈
- Linux下源码安装ElasticResearch
- php 图片水印删除,PHP图片水印
- eclipse下提交job时报错mapred.JobClient: No job jar file set. User classes may not be found.
- 欧姆龙PLC程序 欧姆龙NX系列PLC程序,ST语言和梯形图配合使用,数据处理使用ST语言,逻辑用梯形图
- 谈刺蛇c语言程序,C语言程设计实验内容与答案.doc
- web前端知识——常见布局方案、文章排版、图片排版、某宝列表
- Linux的常用命令就是记不住,还在百度找?于是推出了这套教程,
- ES6 import命令和import()函数区别
- html鼠标放上虚化背景图片,如何将网页CSS背景图高斯模糊且全屏显示
- 解决YOLOv5算法中的中文标签显示问题
- 互联网晚报|12/27星期二| ​​国家卫健委:取消入境后全员核酸检测和集中隔离;新冠肺炎更名为新冠感染;知网回应被罚8760万...