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
原创粉丝点击