ios百度地图地址搜索功能-在线建议查询

这篇博客介绍了如何在iOS应用中实现百度地图的地址搜索功能,通过创建searchBar和tableview展示搜索建议,详细步骤包括导入相关头文件、设置searchBar、展示搜索结果,并重点讲解了调用百度地图搜索接口的关键代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近使用到了百度地图,总结一下,话不多说直接上代码:

使用的是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;
}



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值