iOS百度地图API的使用---定位功能

                老板最近非得整百度地图,没办法找了些资料琢磨琢磨。使用百度地图API首先要去百度官网下载API,并申请Key(按照百度官网的提示就行了)。

              申请了key后,把百度的Demo下载下来,把里面的key改成刚申请的,运行下试试。(提醒下,如果写自己的程序时,编译有很多错误的话,首先看看所需要的框架是否已全部加进去了,其次选择工程->Target-> build settings -> Linking -> other linker flags  添加-all_load

               定位主要用到的是BMKMapView委托的函数-(void)mapView:(BMKMapView *)mapView didUpdateLocation:(BMKUserLocation *)userLocation,通过这个函数可以获取到用户的当前经纬度。

                可能经纬度并不能满足大家的要求,而是希望直接看到自己的位置是什么地名。这就要用到BMKSerarc的委托类BMKSearchDelegate,先调用BMKSerachreverseGeocode函数,然后在BMKSearchDelegate-(void) onGet

AddrResult: (BMKAddrInfo *)result errorCode: (int)error函数中获得结果。

               以下贴几段关键代码:

- (void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation

{

    localLatitude = userLocation.coordinate.latitude;   //保存获取的经纬度

    localLongitude = userLocation.coordinate.longitude;


    if (userLocation != nil) 

    {

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

     }

    

    [_mapView setCenterCoordinate:userLocation.coordinate animated:YES];   //使地图移动到定位的地方

   

    BOOL flag = [_search reverseGeocode:pt];                   //反地理编码      自动调用下面的委托函数获取结果

    if (!flag) {

        NSLog(@"search failed!");

    }

}

- (void)onGetAddrResult:(BMKAddrInfo *)result errorCode:(int)error

{

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

    cityStr = result.addressComponent.streetName;               //得到的街道名称

    cityStreetName = result.addressComponent.city;             //所在城市名称

    NSLog(@"%@",cityStreetName);                                    

    NSLog(@"%@",cityStr);

    NSLog(@"%@",result.addressComponent.streetNumber);   //输出所在城市街道门牌号

}

                百度地图还是很强大的,现在仅仅是管中窥豹而已,后面继续添加功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值