【问题标题】:How to "turn off" location services when a map view with no location manager disappears?当没有位置管理器的地图视图消失时,如何“关闭”位置服务?
【发布时间】:2015-10-27 11:15:53
【问题描述】:

我在我调用的视图控制器中有一个MKMapView

[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

当我导航到另一个视图控制器并且带有地图视图的视图控制器消失时,我仍然在运行位置服务(我看到状态栏中的箭头)。一旦我不再看到地图,我想将其关闭,但如果没有CLLocationManager,我不知道该怎么做...

谢谢

【问题讨论】:

    标签: ios uiviewcontroller mkmapview cllocationmanager location-services


    【解决方案1】:
    -(void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    
    if ([CLLocationManager significantLocationChangeMonitoringAvailable])
        {
            if (OUTPUT_LOGS) NSLog(@"Stopped monitoring for significant changes");
            [[self locationManager] stopMonitoringSignificantLocationChanges];
        }
    
        [[self locationManager] startUpdatingLocation];
    
    }
    
    
    -(void)viewWillDisappear:(BOOL)animated {
    
        [super viewWillDisappear:animated];
    
        if ([CLLocationManager significantLocationChangeMonitoringAvailable])
                {
                    [self.locationManager startMonitoringSignificantLocationChanges];
    
    
                }
    
     [[self locationManager] stopUpdatingLocation];
    }
    

    【讨论】:

    • 谢谢,但关键是我没有任何locationManager 来调用它的stopUpdatingLocation 方法...
    • -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[[CLLocationManager alloc]init] stopUpdatingLocation]; } 试试上面的方法..
    • 好像不行,位置服务箭头还在状态栏中显示...
    • 那么请创建一个 CLLocationManager 对象并设置所需的属性并添加我共享的代码 ....
    • 我在另一个班级有一个可以运行的位置管理器...这不会引起问题吗?有两个并发的位置管理器?
    【解决方案2】:

    这似乎有效:

    - (void)viewDidDisappear:(BOOL)animated
    {
       [super viewDidDisappear:animated];
    
       self.mapView.showsUserLocation = NO;
       [self.mapView setUserTrackingMode:MKUserTrackingModeNone animated:YES];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多