更改视图后无法在IOS应用程序中禁用位置服务。

问题描述 投票:0回答:1

我试图让位置管理器在用户改变视图时停止跟踪用户的位置,因为我只需要在谷歌地图视图的第一个标签页中显示用户位置。

我试图以下列方式启用位置管理器。

 override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
      navigationController?.setNavigationBarHidden(true, animated: animated)

      // get location manager if dissapepared
      if self.locationManager == nil {
        checkLocationServices()
      }
  }

// check if location services is working
func checkLocationServices(){
    if CLLocationManager.locationServicesEnabled(){
        // set up
        setupLocationService()
        checkLocationAuthoization()
    }else{
        showAlert("Location services not available", "Please enable location services!")
    }
}

private func setupLocationService(){
    self.locationManager = CLLocationManager() // LocationServices.getLocationManager()

    // set delegate to viewcontroller here
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

    // efficient location usage 
    self.locationManager.pausesLocationUpdatesAutomatically = true

    self.locationManager.activityType = .otherNavigation

    // enable background update - crashed application
    // locationManager.allowsBackgroundLocationUpdates = true
}

private func checkLocationAuthoization() {
    // Request location authorization
    self.locationManager.requestAlwaysAuthorization()

    switch CLLocationManager.authorizationStatus() {
        case .authorizedWhenInUse:
            self.locationManager.startUpdatingLocation()
            // Do mapp stuff
            break
        case .denied:
            // show alert instructing on how to turn on permissions
            showAlert("Activate location", "Please allow Flype applciation to use your location.")
            break
        case .notDetermined:
            self.locationManager.requestAlwaysAuthorization()
            break
        case .restricted:
            break
        case .authorizedAlways:
            self.locationManager.startUpdatingLocation()
            break
        default:
            break
    }
}

// called every time location changed
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.last
    customMap.camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)! , zoom: 15.0)
    customMap.animate(to: customMap.camera)
    self.locationManager.stopUpdatingLocation()
}

我试图用以下方式关闭它。

override func viewWillDisappear(_ animated: Bool) {
    if self.locationManager != nil {
        self.locationManager.stopUpdatingHeading()
        self.locationManager = nil
        print("SET LOCATION TO NIL!")
    }
}
ios swift cllocationmanager location-services
1个回答
1
投票

为了停止位置更新,你应该调用 stopUpdatingLocation 而不是 stopUpdatingHeading. 那就换吧

if self.locationManager != nil {
        self.locationManager.stopUpdatingHeading()
        self.locationManager = nil
        print("SET LOCATION TO NIL!")
}

随着

self.locationManager.stopUpdatingLocation()

0
投票

呼叫 locationManager.stopUpdatingLocation() 就足以停止位置更新。这是一个不错的省电方式。

© www.soinside.com 2019 - 2024. All rights reserved.