• iOS_SN_地图的使用(2)


    上一篇讲的是地图的基本使用,和注意事项,这一篇主要讲POI检索。百度地图SDK提供三种类型的POI检索:周边检索、区域检索和城市内检索。下面将以周边检索为例,向大家介绍如何使用检索服务。

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        self.mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, kwidth, kheigth)];
        [self.mapView setZoomLevel:16];
        [self.view addSubview:self.mapView];
    //    BMKPoiResult
        
        //1. 初始化检索对象
        _searcher =[[BMKPoiSearch alloc]init];
        _searcher.delegate = self;
        
        //2. 发起检索 --> 拼接参数
        BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
        //分页索引,可选,默认为0
        option.pageIndex = 0;
        //分页数量,可选,默认为10,最多为50
        option.pageCapacity = 10;
        
        option.location = CLLocationCoordinate2DMake(39.915, 116.404);
        option.keyword = @"停车场";
        
        BOOL flag = [_searcher poiSearchNearBy:option];
        if(flag)
        {
            NSLog(@"周边检索发送成功");
        }
        else
        {
            NSLog(@"周边检索发送失败");
        }
    }
    

      检索结果在代理中

    //实现PoiSearchDeleage处理回调结果
    - (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
    

      然后就是基于基本检索的详情检索要在上面一个方法的代理中实现详情检索,

    - (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
    {
        if (error == BMK_SEARCH_NO_ERROR) {
            //在此处理正常结果
            
            //初始化检索服务
            _poisearch = [[BMKPoiSearch alloc] init];
            _poisearch.delegate = self;
            //POI详情检索
            BMKPoiDetailSearchOption* option = [[BMKPoiDetailSearchOption alloc] init];
            
            for (BMKPoiInfo *poiInfo in poiResultList.poiInfoList) {
                
              NSString *uids = poiInfo.uid;
              option.poiUid = uids;
    
            }
    
            BOOL flag = [_poisearch poiDetailSearch:option];
            if(flag)
            {
                //详情检索发起成功
                NSLog(@"%@",@"详情检索发起成功");
            }
            else
            {
                //详情检索发送失败
                NSLog(@"%@",@"详情检索发送失败");
            }
            
            // 添加大头针 / 显示一个列表给用户
            for (BMKPoiInfo *poiInfo in poiResultList.poiInfoList) {
                BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
                annotation.coordinate = poiInfo.pt;
                annotation.title = poiInfo.name;
                [self.mapView addAnnotation:annotation];
            }
            
        }
        
        else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
            //当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表
            // result.cityList;
            NSLog(@"起始点有歧义");
        } else {
            NSLog(@"error: %zd",error);
            NSLog(@"抱歉,未找到结果");
        }
        
    }
    

      然后在代理方法中得到详情检索结果。

    -(void)onGetPoiDetailResult:(BMKPoiSearch *)searcher result:(BMKPoiDetailResult *)poiDetailResult errorCode:(BMKSearchErrorCode)errorCode
    {
        if(errorCode == BMK_SEARCH_NO_ERROR){
            //在此处理正常结果
            NSLog(@"%@",@"在此处理正常结果");
            
            NSLog(@"%@--%@--%@--%@--%f--%d",poiDetailResult.address,poiDetailResult.name,poiDetailResult.tag,poiDetailResult.phone,poiDetailResult.price,poiDetailResult.favoriteNum);
    
        }
    }
    

      还有公交检索和路线检索,没有实际用过在demo中有写,但是没有把路线画出来,只是打印了一些信息在控制台,然后就是正向地理编码和反向地理编码示例如下:

    -(void)viewDidLoad    
    {      
        //初始化检索对象    
        _searcher =[[BMKGeoCodeSearch alloc]init];    
        _searcher.delegate = self;      
        BMKGeoCodeSearchOption *geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];    
        geoCodeSearchOption.city= @"北京市";    
        geocodeSearchOption.address = @"海淀区上地10街10号";    
        BOOL flag = [_searcher geoCode:geoCodeSearchOption];    
        [geoCodeSearchOption release];    
        if(flag)    
        {    
            NSLog(@"geo检索发送成功");    
        }    
        else    
        {    
            NSLog(@"geo检索发送失败");    
        }    
     
        //发起反向地理编码检索    
        //CLLocationCoordinate2D pt = (CLLocationCoordinate2D){39.915, 116.404};    
        //BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[  
        //BMKReverseGeoCodeOption alloc]init];  
        //reverseGeoCodeSearchOption.reverseGeoPoint = pt;    
        //BOOL flag = [_searcher reverseGeoCode:reverseGeoCodeSearchOption];    
        //[reverseGeoCodeSearchOption release];    
        //if(flag)    
        //{    
        //  NSLog(@"反geo检索发送成功");    
        //}    
        //else    
        //{    
        //  NSLog(@"反geo检索发送失败");    
        //}    
     }    
     
    //实现Deleage处理回调结果    
    //接收正向编码结果   
     
    - (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{    
        if (error == BMK_SEARCH_NO_ERROR) {    
             //在此处理正常结果    
        }    
        else {    
            NSLog(@"抱歉,未找到结果");    
        }    
    }    
     
    //接收反向地理编码结果    
    //-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:  
    //(BMKReverseGeoCodeResult *)result   
    //errorCode:(BMKSearchErrorCode)error{    
    //  if (error == BMK_SEARCH_NO_ERROR) {    
    //      在此处理正常结果    
    //  }    
    //  else {    
    //      NSLog(@"抱歉,未找到结果");    
    //  }    
    //}    
     
    //不使用时将delegate设置为 nil      
    -(void)viewWillDisappear:(BOOL)animated      
    {      
        _searcher.delegate = nil;      
    }
    

      

    本文GitHub地址https://github.com/zhangkiwi/iOS_SN_BDMap-Test

  • 相关阅读:
    局域网文件实时同步工具
    SQLServer备份恢复助手(太强大了!)
    SQLServer 统计查询语句消耗时间
    如何将Sql server数据库中的模型图转化到Word中--并能够查看字段的属性信息
    创建Database Diagrams时遇到的问题
    bat 操作数据库(附加,分离,删除,还原)
    Bat 多个执行操作选择
    Redis Windows下查看版本号
    ThinkPHP 数据库操作(七) : 视图查询、子查询、原生查询
    ThinkPHP 数据库操作(六) : 查询事件、事务操作、监听SQL
  • 原文地址:https://www.cnblogs.com/zhang-kiwi/p/5263527.html
Copyright © 2020-2023  润新知