iOS 百度地图SDK使用小结

来源:互联网 发布:网络接收器价格 编辑:程序博客网 时间:2024/05/09 18:50

官方的集成介绍虽然很多,但是本人 用到的比较少,除了基本库的导入 和在AppDelegate离 regist外 其他用到的比较少,至少获取当前地理位置的代码 个人认为写的不够清除。


这里主要介绍下如何开启定位,反编码地理坐标 和城市云搜索。


BMKLocationServiceDelegate, BMKGeoCodeSearchDelegate


首页导入上面的代理方法

其次开启百度定位服务

 [BMKLocationServicesetLocationDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];

    [BMKLocationServicesetLocationDistanceFilter:kCLLocationAccuracyBest];

    

    //初始化BMKLocationService

    _locService = [[BMKLocationServicealloc]init];

    _locService.delegate =self;

    //启动LocationService

    [_locServicestartUserLocationService];

调用代理方法会获取当前经纬度,获得经纬后 使用搜索 搜索坐标

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation

{


    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

    // 建议获取完经纬后停止位置更新  否则会一直更新坐标

   if (userLocation.location.coordinate.latitude !=0) {

        [_locServicestopUserLocationService];

    }

    //调用搜索

    BMKGeoCodeSearch *search = [[BMKGeoCodeSearchalloc]init];

    search.delegate =self;

    BMKReverseGeoCodeOption *rever = [[BMKReverseGeoCodeOptionalloc]init];

    rever.reverseGeoPoint = userLocation.location.coordinate;

    //这段代码不要删

   NSLog(@"%d",[searchreverseGeoCode:rever]);

}

搜索代理方法里就能返回具体地址了

#pragma mark GeoCodeResult 返回地理位置

-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error

{

   NSLog(@"%@",result.address);

}



以上就是获取当前地理位置的代码。



云搜索这块我是在使用系统正常的SearchBar,在它的代理方法里触发云搜索 导入代理

BMKGeoCodeSearchDelegate, BMKLocationServiceDelegate,BMKPoiSearchDelegate


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

{

    //初始化poi搜索

   _poiSearch = [[BMKPoiSearchalloc]init];

    _poiSearch.delegate =self;

    

    BMKCitySearchOption *option = [[BMKCitySearchOptionalloc]init];

    option.city =@"北京市";

    _searchTextFiled.placeholder =@"请输入要切换的地址";

    option.keyword =_searchTextFiled.text;

   BOOL flag = [_poiSearchpoiSearchInCity:option];

   if(flag)

    {

//        NSLog(@"周边检索发送成功");

    }

   else

    {

//        NSLog(@"周边检索发送失败");

    }


}

- (void)onGetPoiResult:(BMKPoiSearch*)searcher

                result:(BMKPoiResult*)poiResultList

             errorCode:(BMKSearchErrorCode)error

{

    if (error ==BMK_SEARCH_NO_ERROR) {

       //在此处理正常结果  poiResultList.totalPoiNum, poiResultList.poiInfoList 这两个分别代表搜索结果数量和存地址信息的数组, forin 遍历poiResultList.poiInfoList这个数组即可


        NSLog(@"%d %@", poiResultList.totalPoiNum, poiResultList.poiInfoList);

        NSArray *array = poiResultList.poiInfoList;

                

    }

    elseif (error ==BMK_SEARCH_AMBIGUOUS_KEYWORD){

        //当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表

        // result.cityList;

        NSLog(@"起始点有歧义");

    } else {

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

        

    }

}

以上为城市云搜索代码,其他热点搜索等大家可以参考百度地图开发者中心里的类参考 查找即可。

如有疑问欢迎留言。


0 0
原创粉丝点击