我在视图控制器viewDidLoad
中添加了一个MKUserTrackingBarButtonItem实例,如下所示:
MKUserTrackingBarButtonItem *userTrackingButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self->mapView];
NSMutableArray *toolbarItems = [[NSMutableArray alloc] initWithArray:[mapToolBar items]];
[toolbarItems insertObject:userTrackingButton atIndex:0];
[mapToolBar setItems:toolbarItems];
位置服务已在“设置”中启用,但未在应用中启用。当点击MKUserTrackingBarButtonItem按钮时,模态对话框会询问用户是否允许该应用使用当前位置。当"不允许"如果选中,MKUserTrackingBarButtonItem将变为灰色虚线旋转图标,并且不会恢复原始罗盘图标。
如何在地图应用中完成恢复按钮的状态?
所需的行为与地图应用中的行为相同。
恢复原始罗盘图标。
我在iPhone上测试它,而不是在模拟器上测试。打开位置服务并允许应用程序使用当前位置时,上面的代码可以正常运行。
答案 0 :(得分:3)
实施CLLocationManagerDelegate
方法:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
当CLAuthorizationStatus不是kCLAuthorizationStatusAuthorized
时,您可以将MKMapview的userTrackingMode
属性更新为MKUserTrackingModeNone
答案 1 :(得分:1)
我无法在互联网上找到很多帮助,但这篇文章帮了很多忙。这是对olicarbo所说的快速实施:
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
mapView.showsUserLocation = (status == .AuthorizedAlways)
//if location services are turned off for this app.
if(status == CLAuthorizationStatus.AuthorizedAlways || status == CLAuthorizationStatus.AuthorizedWhenInUse){
mapView.userTrackingMode = MKUserTrackingMode.None
}
}
func mapView(mapView: MKMapView, didChangeUserTrackingMode mode: MKUserTrackingMode, animated: Bool) {
if(CLLocationManager.locationServicesEnabled() == false || !(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways)){
//location services are disabled or
//user has not authorized permissions to use their location.
mapView.userTrackingMode = MKUserTrackingMode.None
}
}