【问题标题】:MKUserTrackingBarButtonItem Spinning After The App Is Disallowed To Use Current LocationMKUserTrackingBarButtonItem 在应用被禁止使用当前位置后旋转
【发布时间】:2014-08-18 20:25:47
【问题描述】:

我在视图控制器的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 按钮时,模式对话框询问用户是否允许应用使用当前位置。 When "Don't Allow" is selected, MKUserTrackingBarButtonItem turns into a grey dashed spinning icon and the original compass icon is not restored.

如何恢复按钮在地图应用中的状态?

所需的行为与地图应用程序中的行为相同。

  1. 确保在“设置”>“隐私”>“定位服务”中开启定位服务。
  2. 在“设置”>“通用”>“重置”>“重置位置和隐私”中重置位置和隐私。
  3. 启动地图应用。
  4. 点击左下角的指南针图标。
  5. 将出现一个对话框,其中包含“地图”希望使用您的当前位置”消息。此时,指南针图标已变成灰色的虚线旋转图标。
  6. 选择“不允许”。

恢复原来的指南针图标。

我在 iPhone 上而不是在模拟器上进行测试。上述代码在定位服务开启且允许应用使用当前位置时正常运行。

【问题讨论】:

    标签: ios iphone objective-c core-location


    【解决方案1】:

    实现CLLocationManagerDelegate 方法:

    - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

    当 CLAuthorizationStatus 不是 kCLAuthorizationStatusAuthorized 时,您可以将 MKMapview 的 userTrackingMode 属性更新为 MKUserTrackingModeNone

    【讨论】:

    • 执行kCLAuthorizationStatusAuthorized后,指南针恢复。如果再次点击它,它会变成旋转图标。第一次请求被拒绝后如何处理第二次点击?
    • 您可以在您的 MKMapViewDelegate 中实现- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated。当模式更改为MKUserTrackingModeFollowMKUserTrackingModeFollowWithHeading 并且来自LocationManager 的状态未授权时,您将跟踪模式设置为MKUserTrackingModeNone,就像您在其他方法中所做的那样。
    • 我刚刚尝试过。如果实现了- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated,则不再要求用户允许/禁止使用当前位置,这意味着不会通知用户需要做什么。
    【解决方案2】:

    我在互联网上找不到很多关于这方面的帮助,但这篇文章很有帮助。这是 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 2015-05-02
      • 2012-12-15
      相关资源
      最近更新 更多