定位
您可以通过以下代码来开启定位功能:
- //开启定位功能
- [_mapView setShowsUserLocation:YES];
定位成功后,可以通过mapView.userLocation来获取位置数据。
您也可以通过以下代码来使用定位三态效果,包括普通态、跟随态和罗盘态:
- //普通态
- -(IBAction)startLocation:(id)sender
- {
- NSLog(@"进入普通定位态");
- _mapView.showsUserLocation = NO;//先关闭显示的定位图层
- _mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态
- _mapView.showsUserLocation = YES;//显示定位图层
- }
- //跟随态
- -(IBAction)startFollowing:(id)sender
- {
- NSLog(@"进入跟随态");
- _mapView.showsUserLocation = NO;
- _mapView.userTrackingMode = BMKUserTrackingModeFollow;
- _mapView.showsUserLocation = YES;
- }
- //罗盘态
- -(IBAction)startFollowHeading:(id)sender
- {
- NSLog(@"进入罗盘态");
- _mapView.showsUserLocation = NO;
- _mapView.userTrackingMode = BMKUserTrackingModeFollowWithHeading;
- _mapView.showsUserLocation = YES;
- }
完整的示例代码请参考demo工程中的LocationDemoViewController.mm文件
示例效果如下: