iOS 百度地圖3-檢索

百度地圖SDK提供的檢索服務包括以下功能模塊:
POI檢索,
公交方案檢索,
駕車路線檢索,
步行路線檢索,
行政區邊界數據檢索,
地理編碼,
反地理編碼,
公交詳情檢索,
在線建議查詢,
短串分享(包括POI搜索結果分享、駕車/公交/騎行/步行路線規劃分享、反向地理編碼結果分享)。

每個檢索功能模塊都包括一個主檢索對象,一個用于構造檢索參數的Option結構體,和一個用于接收檢索結果回調的Delegate,所有檢索服務都使用異步回調模式。使用檢索服務時,需要先初始化主檢索對象,然后通過主檢索對象以包含檢索參數的Option做為參數發起檢索,最后實現相應的檢索功能模塊的Delegate處理返回結果 。
更加具體的使用詳情可以參看 [百度地圖檢索功能]
(http://lbsyun.baidu.com/index.php?title=iossdk/guide/retrieval)

1 POI周邊檢索

POI(Point of Interest),中文可以翻譯為“興趣點”。在地理信息系統中,一個POI可以是一棟房子、一個商鋪、一個郵筒、一個公交站等。
百度地圖SDK提供三種類型的POI檢索:周邊檢索、區域檢索和城市內檢索。下面將以周邊檢索為例,向大家介紹如何使用檢索服務。

1.1 在頁面上定義一個輸入框,內容改變時發起檢索

注意檢查 nearBySearchOption.location 內容是否正確,否則總是檢索不到結果

UITextField *poiTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 30, 100, 20)];
poiTextField.backgroundColor = [UIColor lightGrayColor];
[poiTextField addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventEditingChanged];
[self.view addSubview:poiTextField];
- (void)valueChange:(UITextField *)textField {
    
    _poiSearch = [[BMKPoiSearch alloc]init];
    _poiSearch.delegate = self;
    
    // 附近云檢索
    BMKNearbySearchOption *nearBySearchOption = [[BMKNearbySearchOption alloc]init];
    // 檢索關鍵字
    nearBySearchOption.keyword = textField.text;
    // 檢索中心點及半徑
    nearBySearchOption.location = _locService.userLocation.location.coordinate;
    nearBySearchOption.radius = 10000;
    // 搜索結果排序,距離由近到遠
    nearBySearchOption.sortType = BMK_POI_SORT_BY_DISTANCE;
    // 分頁索引及數量
    nearBySearchOption.pageIndex = 0;
    nearBySearchOption.pageCapacity = 10;
    
    // 開始檢索,結果在代理方法 onGetPoiResult 中返回
    BOOL flag = [_poiSearch poiSearchNearBy:nearBySearchOption];
    if (flag) {
        NSLog(@"搜索成功--%@", textField.text);
    }else {
        NSLog(@"搜索失敗");
    }
}

1.2 結果監聽

調用 poiSearchNearBy 方法后,檢索結果 在代理方法 onGetPoiResult 中返回,poiResult.poiInfoList 就是檢索到的結果,是一個 BMKPoiInfo 的集合,每個BMKPoiInfo包含該檢索點的名稱,地址,uid等詳細信息。

- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode {
    if (errorCode == BMK_SEARCH_NO_ERROR) {
        for (int i = 0; i < poiResult.poiInfoList.count; i++) {
            BMKPoiInfo *info = [poiResult.poiInfoList objectAtIndex:i];
            NSLog(@"地址:%@---%@---%@", info.city ,info.name, info.address);
        }
    }else {
        NSLog(@"檢索異常-%d", errorCode);
    }
}
檢索.PNG
檢索結果.jpeg

1.3 展示

1.3.1 把這些數據顯示到tableview上,就是我們常見的搜索框搜索得到對應的結果。

檢索結果.PNG

1.3.2 或者將搜索出來的結果通過大頭針展示在地圖上:

- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode {
    
    // 清楚屏幕中所有的annotation
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    
    if (errorCode == BMK_SEARCH_NO_ERROR) {
        NSMutableArray *arrM = [NSMutableArray array];
        for (int i = 0; i < poiResult.poiInfoList.count; i++) {
            BMKPoiInfo *info = [poiResult.poiInfoList objectAtIndex:i];
            NSLog(@"地址:%@---%@---%@", info.city ,info.name, info.address);
            
            BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
            item.coordinate = info.pt;
            item.title = info.name;
            [arrM addObject:item];
        }
        [_mapView addAnnotations:arrM];
        [_mapView showAnnotations:arrM animated:YES];
        
        self.poiResultArr = poiResult.poiInfoList;
        [self.tableView reloadData];
        
    }else {
        NSLog(@"檢索異常-%d", errorCode);
    }
}
大頭針展示.PNG

2 公交詳情信息檢索-基于POI

基于上面POI檢索返回的POI結果中,BMKPoiInfo有一屬性epoitype,表示POI類型,epoitype字段值為2標示公交路線,4表示地鐵路線,把這兩種類型的POI的 uid 傳給公交信息檢索接口,可以得到該POI所代表的路線的詳細信息(如:該公交線有多少個站點,每個站點的名稱,位置、參考票價和上下線行信息)。

點擊tableView調用 公交詳情信息檢索,代碼如下:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.poiResultArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
    }
    
    BMKPoiInfo *info = self.poiResultArr[indexPath.row];
    cell.textLabel.text = info.name;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BMKPoiInfo *info = self.poiResultArr[indexPath.row];
    if (info.epoitype == 2 || info.epoitype == 4) {
        // 發起公交檢索
        [self startBusLineSearchWithCity:info.city uid:info.uid];

    }else {
        NSLog(@"這不是一條公交線路");
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark
#pragma mark -- 公交方案檢索

- (void)startBusLineSearchWithCity:(NSString *)city uid:(NSString *)uid{
    _busLineSearch = [[BMKBusLineSearch alloc]init];
    _busLineSearch.delegate = self;
    
    BMKBusLineSearchOption *option = [[BMKBusLineSearchOption alloc]init];
    option.city = city;
    option.busLineUid = uid;
    
    BOOL flag = [_busLineSearch busLineSearch:option];
    if(flag)
    {
        NSLog(@"busline檢索發送成功    %@---%@", city, uid);
    }
    else
    {
        NSLog(@"busline檢索失敗");
    }
}

- (void)onGetBusDetailResult:(BMKBusLineSearch *)searcher result:(BMKBusLineResult *)busLineResult errorCode:(BMKSearchErrorCode)error {
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此處理正常結果
        NSLog(@"%@,共有%zd站",busLineResult.busLineName, busLineResult.busStations.count);
    }
    else {
        NSLog(@"抱歉,未找到Bus結果%d",error);
    }
}

注意代理控制:

-(void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用時,置nil,否則影響內存的釋放
    _locService.delegate = nil;
    _poiSearch.delegate = nil;
    _busLineSearch.delegate = nil;
}

分別點擊下面第1條,第3條,第4條,打印結果:

檢索結果.PNG
公交檢索結果.jpeg
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容