显示定位
1.定位简介
由于系统原因,iOS不允许使用第三方定位,因此地图SDK中的定位方法,本质上是对原生定位的二次封装。通过封装,开发者可更便捷的使用。
2.配置定位权限
在 Info.plist 文件中增加定位权限配置,值得注意的是,定位权限有三种,您可根据需求进行选择。
权限 | 描述 |
---|---|
Privacy - Location Always Usage Description | 始终允许访问位置信息 |
Privacy - Location Usage Description | 永不允许访问位置信息 |
Privacy - Location When In Use Usage Description | 使用应用期间允许访问位置信息 |
3.定位模式
目前为止,FMGLMapView
的定位模式(userTrackingMode
)有4种分别是:
类型 | 描述 |
---|---|
FMGLUserTrackingModeNone | 地图不跟随用户位置 |
FMGLUserTrackingModeFollow | 地图跟随用户位置 |
FMGLUserTrackingModeFollowWithHeading | 地图跟随用户位置,并在方向更改时旋转。 |
FMGLUserTrackingModeFollowWithCourse | 地图跟随用户位置,并在航向变化时旋转。 |
4.获取位置信息
定位功能通过设置地图的属性实现定位信息的获取,
// 开始定位
- (IBAction)startLocation:(UIButton *)sender {
self.mapView.showsUserLocation = YES;
[self.mapView setUserTrackingMode:FMGLUserTrackingModeFollow animated:YES];
}
// 关闭定位
- (IBAction)stopLocation:(UIButton *)sender {
self.mapView.showsUserLocation = NO;
[self.mapView setUserTrackingMode:FMGLUserTrackingModeNone animated:NO];
}
#pragma mark - 追踪
- (void)mapViewWillStartLocatingUser:(FMGLMapView *)mapView
{
NSLog(@"开始追踪位置");
}
- (void)mapViewDidStopLocatingUser:(FMGLMapView *)mapView
{
NSLog(@"停止追踪用户位置");
}
- (void)mapView:(FMGLMapView *)mapView didUpdateUserLocation:(FMGLUserLocation *)userLocation
{
NSLog(@"更新用户位置");
}
- (void)mapView:(FMGLMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
NSLog(@"追踪位置失败");
}
- (void)mapView:(FMGLMapView *)mapView didChangeUserTrackingMode:(FMGLUserTrackingMode)mode animated:(BOOL)animated
{
NSLog(@"追踪模式改变");
}