百度地圖檢索周圍小喫添加大頭針

效果圖:



代碼:

1.導入頭文件

#import <BaiduMapAPI_Search/BMKPoiSearch.h>


2.設置代理

    <BMKPoiSearchDelegate.h>

    BMKPoiSearch *_searcher;      

3.初始化檢索對象

    _searcher =[[BMKPoiSearchalloc]init];

    _searcher.delegate =self;

    //發起檢索

    BMKNearbySearchOption *option = [[BMKNearbySearchOptionalloc]init];

    option.pageCapacity = 10;

    option.location = coor;

    option.keyword = @"小喫";

    BOOL flag = [_searcherpoiSearchNearBy:option];

    if(flag)

    {

        NSLog(@"周邊檢索發送成功");

    }

    else

    {

        NSLog(@"周邊檢索發送失敗");

    }

4.設置回調方法

//實現PoiSearchDeleage處理回調結果

- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error

{

    if (error ==BMK_SEARCH_NO_ERROR) {

        //在此處理正常結果

        NSLog(@"成功:%@", poiResultList.poiInfoList);

        [poiResultList.poiInfoList enumerateObjectsUsingBlock:^(BMKPoiInfo *_Nonnull obj,NSUInteger idx,BOOL *_Nonnull stop) {

            NSLog(@"%@----%@", obj.name, obj.address); //由於設置檢索時,每頁指定了10條,所以此處檢索出10條相關信息

            [self addAnnoWithPT:obj.pt andTitle:obj.name andAddress:obj.address];

        }];


    }

    elseif (error ==BMK_SEARCH_AMBIGUOUS_KEYWORD){

        //當在設置城市未找到結果,但在其他城市找到結果時,回調建議檢索城市列表

        // result.cityList;

        NSLog(@"起始點有歧義");

    } else {

        NSLog(@"抱歉,未找到結果");

    }

}

//不使用時將delegate設置爲 nil

-(void)viewWillDisappear:(BOOL)animated

{

    _searcher.delegate =nil;

}


//添加大頭針

- (void)addAnnoWithPT:(CLLocationCoordinate2D)pt andTitle:(NSString *)name andAddress:(NSString *)address{

    BMKPointAnnotation *pointAnnotations = [[BMKPointAnnotationalloc]init];

    CLLocationCoordinate2D coords;

    

    coords.latitude = pt.latitude;

    coords.longitude = pt.longitude;

    pointAnnotations.coordinate = coords;

    pointAnnotations.title = @"巴士互聯歡迎您!";

    [_mapView addAnnotation:pointAnnotations];

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章