baidumap demo(三)

定位

您可以通过以下代码来开启定位功能:

  1. //开启定位功能  
  2. [_mapView setShowsUserLocation:YES];  

定位成功后,可以通过mapView.userLocation来获取位置数据。

您也可以通过以下代码来使用定位三态效果,包括普通态、跟随态和罗盘态:

  1. //普通态  
  2. -(IBAction)startLocation:(id)sender  
  3. {  
  4.     NSLog(@"进入普通定位态");  
  5.     _mapView.showsUserLocation = NO;//先关闭显示的定位图层  
  6.     _mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态  
  7.     _mapView.showsUserLocation = YES;//显示定位图层  
  8. }  
  9.   
  10. //跟随态  
  11. -(IBAction)startFollowing:(id)sender  
  12. {  
  13.     NSLog(@"进入跟随态");  
  14.     _mapView.showsUserLocation = NO;  
  15.     _mapView.userTrackingMode = BMKUserTrackingModeFollow;  
  16.     _mapView.showsUserLocation = YES;  
  17. }  
  18.   
  19. //罗盘态  
  20. -(IBAction)startFollowHeading:(id)sender  
  21. {  
  22.     NSLog(@"进入罗盘态");  
  23.     _mapView.showsUserLocation = NO;  
  24.     _mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;  
  25.     _mapView.showsUserLocation = YES;     
  26. }  

完整的示例代码请参考相关下载demo工程中的LocationDemoViewController.mm文件

示例效果如下:

原文地址:https://www.cnblogs.com/yulang314/p/3549751.html