使用以下代码,我的currentLat和currentLong不会在第一个位置之外进行更新。位置永远不会有超过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具有必要的条目。
当您要更新位置时,一旦获得新位置,则应停止现有更新。您可以stopUpdatingLocation()
在您的didUpdateLocations:
方法中使用。
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()
在需要新位置时致电给您。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句