iOS 和 Swift:CLLocationManagerDelegate 不更新超出第一个位置的位置坐标

iOS and Swift: CLLocationManagerDelegate not updating location coordinates beyond the first location

使用以下代码,我的 currentLat 和 currentLong 不会更新到第一个位置之外。 locations 从来没有超过 1 个项目,而且总是完全相同。

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let loc: CLLocation = locations[locations.count - 1]
    currentLat = loc.coordinate.latitude
    currentLong = loc.coordinate.longitude
}

我一直在做的所有搜索只显示了如果它根本不起作用,如何让它起作用,但这确实起作用,但只有一次。

我希望它的工作方式是不断更新 GPS 坐标,并在位置更新时关闭此委托。该位置是否一直在更新?我认为这是由于 "startUpdatingLocation()" 的术语,并且有一个 "stopUpdatingLocation()." 我是不是用错了?

就像我说的,它是第一次工作,所以它加载和启动都很好,允许权限,info.plist 有必要的条目。

当您想更新您的位置时,您应该在获得新位置后停止现有更新。您可以在 didUpdateLocations: 方法中使用 stopUpdatingLocation() 来执行此操作。

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

    // This should match your CLLocationManager()
    locManager.stopUpdatingLocation()

    let loc: CLLocation = locations[locations.count - 1]
    currentLat = loc.coordinate.latitude
    currentLong = loc.coordinate.longitude
}

然后在需要新位置时给您的 locManager.startUpdatingLocation() 打电话。