百度地图检索周围小吃添加大头针

效果图:



代码:

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];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值