位置管理器停止更新位置

时间:2017-01-15 17:33:11

标签: swift3 core-location

enter image description here我在更新用户位置时遇到问题。我正在构建应用程序,它显示最近的垃圾桶以及它们与用户之间的距离。我正在使用这个位置管理器,但它永远不会更新位置。即使用户移动,存储的位置也保持不变

Xamarin.iOS

我用它来计算距离

let manager = CLLocationManager()


func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {


    let location: CLLocation = locations.last!
    let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)

    //store myLocation in UserDefaults
    UserDefaults(suiteName: "group.Afvalbakfinder.Jordadema.userLocation")?.set(myLocation.latitude, forKey: "latitude")
    UserDefaults(suiteName: "group.Afvalbakfinder.Jordadema.userLocation")?.set(myLocation.longitude, forKey: "longitude")

    UserDefaults.standard.set(myLocation.latitude, forKey: "latitude")
    UserDefaults.standard.set(myLocation.longitude, forKey: "longitude")
    UserDefaults().synchronize()


    self.mapView.showsUserLocation = true

}
在应用程序内部这不是一个很大的问题,但我需要一个不断更新的位置,以便小部件正常工作。这个小部件显示了三个最近的垃圾桶,并且如果使用旧的位置数据,它实际上并没有做它应该做的事情。

这是我完整的ViewController:

let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(UserDefaults(suiteName: "group.Afvalbakfinder.Jordadema.userLocation")?.value(forKey: "latitude") as! CLLocationDegrees, UserDefaults(suiteName: "group.Afvalbakfinder.Jordadema.userLocation")?.value(forKey: "longitude") as! CLLocationDegrees)



    // geeft zin met afstand van locatie1 tot myLocation
    func afstandPuntTotUser(locatie1: CLLocationCoordinate2D) -> String {
        manager.startUpdatingLocation()
        let cllocatie1 = converter(locatie: locatie1)
        let myLocation = converter(locatie: myLocation)
        let afstand = cllocatie1.distance(from: myLocation)
        let afgerondeafstand = afstand - afstand.truncatingRemainder(dividingBy: 1.0)                                   //zorgt voor 1 kommagetal
        if afstand < 1000.0 {
            return "De afstand tot de bak is \(afgerondeafstand) meter"
        }
        else {
            return "De afstand tot de bak is \(afgerondeafstand / 1000 - (afgerondeafstand / 1000).truncatingRemainder(dividingBy: 0.1)) kilometer"
        }
    }

2 个答案:

答案 0 :(得分:3)

在同一个Project中创建一个新类,并在新类中添加此代码。然后检查是否会调用didUpdateLocations方法。让我知道这段代码是否适合你。

@IBOutlet weak var mapView: MKMapView!

let manager = CLLocationManager()

   override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestAlwaysAuthorization()
    manager.startUpdatingLocation()
    // Do any additional setup after loading the view, typically from a nib.
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let location: CLLocation = locations[0]
    let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    //let span: MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
    //let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)

    //store myLocation in UserDefauls


    UserDefaults(suiteName: "group.Afvalbakfinder.Jordadema.userLocation")?.set(myLocation.latitude, forKey: "latitude")
    UserDefaults(suiteName: "group.Afvalbakfinder.Jordadema.userLocation")?.set(myLocation.longitude, forKey: "longitude")

    UserDefaults.standard.set(myLocation.latitude, forKey: "latitude")
    UserDefaults.standard.set(myLocation.longitude, forKey: "longitude")
    UserDefaults().synchronize()


    //region wel of niet? nog even over hebben
    //mapView.setRegion(region, animated: true)

    self.mapView.showsUserLocation = true
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print("Failed to find user's location: \(error.localizedDescription)")
}

答案 1 :(得分:0)

检查以下内容:

  1. 启用后台模式并在应用目标的功能部分中选择位置更新。
  2. 在您的应用信息plist文件中添加NSLocationUsageDescription。
  3. 添加隐私 - 位置始终用法描述在您的信息plist中如果您将在应用程序处于后台或未运行时使用位置服务其他添加隐私 - 位置何时使用用法说明如果您将仅使用位置服务用户使用您的应用。