禁用MKMapView上的指南针

Bla*_*ckM 15 macos mapkit ios compass-geolocation

我在我的应用程序中使用MapView来显示一些注释.在iOS 7中,指南针随机出现在地图上.我无法重现错误,因为它随机出现,但我想禁用它.任何想法如何禁用它?

更新:我发现不是随机出现,而是出现在特定的手势上.当您使用2个手指并向右滑动而另一个向左滑动时.

Mar*_*ery 28

您可以使用以下showsCompass属性在OSX 10.9/iOS 9及更高版本上轻松禁用指南针:

yourMapView.showsCompass = NO;
Run Code Online (Sandbox Code Playgroud)

在iOS 8或更早版本中,您的选择是:

  1. 把它吸干并与它一起生活.

  2. 使用hack,如:

  3. 如果您没有以编程方式旋转地图并且尚未旋转,请使用完全禁用旋转

    mapView.rotateEnabled = NO;
    
    Run Code Online (Sandbox Code Playgroud)

    指南针仅在地图旋转时显示,因此通过这样做可确保永远不会触发指南针.

我不清楚为什么苹果等待这么长时间才允许将指南针隐藏在iOS上,并且上述选项都不是理想选择.选择你认为最不好的情况.


Dav*_*sky 7

我找到了解决问题的方法,使用Mark Amery关于遍历MKMapView实例子视图以查找指南针的想法,以及使用手势识别来触发删除事件.

为了找到指南针,我打印出了对视图的描述,发现其中一个视图是一个实例MKCompassView,这显然是指南针.

我已经提出了以下适合您的代码.它检查旋转手势,然后在手势事件触发的方法中删除视图.

我已经测试了这种方法,它适用于我:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIRotationGestureRecognizer *rotateGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];

    [self.mapView addGestureRecognizer:rotateGesture];
}

-(void)rotate:(UIRotationGestureRecognizer *)gesture
{
    if ([gesture state] == UIGestureRecognizerStateBegan || [gesture state] == UIGestureRecognizerStateChanged) {
        // Gets array of subviews from the map view (MKMapView)
        NSArray *mapSubViews = self.mapView.subviews;

        for (UIView *view in mapSubViews) {
            // Checks if the view is of class MKCompassView
            if ([view isKindOfClass:NSClassFromString(@"MKCompassView")]) {
                // Removes view from mapView
                [view removeFromSuperview];
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)