didUpdateLocations
被多次调用。所以我怎么能处理这个。我尝试在locationManager.stopUpdatingLocation()
中使用didUpdateLocations
,但效果不佳。
override func viewDidLoad()
{
super.viewDidLoad()
//for Authorisation from the User.
isAuthorizedtoGetUserLocation()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}
func isAuthorizedtoGetUserLocation() {
if CLLocationManager.authorizationStatus() != .authorizedWhenInUse {
locationManager.requestWhenInUseAuthorization()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locationManager.stopUpdatingLocation()
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
let camera = GMSCameraPosition.camera(withLatitude: locValue.latitude, longitude: locValue.longitude, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.view = mapView
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: locValue.latitude, longitude: locValue.longitude)
marker.title = "name"
marker.map = mapView
}
错误的错误代码
func locationManager( _ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
manager.stopUpdatingLocation()
manager = nil // Here it is giving error "Cannot assign to value: 'manager' is a 'let' constant"
var newLocation: CLLocation? = locations.last
var locationAge: TimeInterval? = -(newLocation?.timestamp.timeIntervalSinceNow)!
if Double(locationAge!) > 5.0 {
return
}
if Double((newLocation?.horizontalAccuracy)!) < 0 {
return
}
//manager = nil
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
let camera = GMSCameraPosition.camera(withLatitude: locValue.latitude, longitude: locValue.longitude, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.view = mapView
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: locValue.latitude, longitude: locValue.longitude)
marker.title = "name"
marker.map = mapView
}
答案 0 :(得分:2)
您需要设置CLLocation manager的distanceFilter属性,如下所示:
/* Notify changes when device has moved x meters.
* Default value is kCLDistanceFilterNone: all movements are reported.
*/
self.locationManager.distanceFilter = 10.0f; // you can change as per your require ment
答案 1 :(得分:0)
这是另一种方法。 我的应用程序需要在更改后上传位置。 即使我这样声明距离过滤器:
locationManager.distanceFilter = 300
首次创建CLLocationManager或调用startUpdatingLocation()时,它可能会多次调用。
所以我将它们放在这样的任务队列中:
if let locationUploadTask = self.locationTask { // if there is a task waiting, cancel it
locationUploadTask.cancel()
}
self.locationTask = DispatchWorkItem { //create a new task for upload
//do your task
}
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: self.locationTask!) //execute the task 1 second after
此代码强制您的代码等待1秒。如果在一秒钟内有新的位置更新,则取消当前任务,再次开始等待1秒钟。
答案 2 :(得分:0)
将属性distanceFilter
设置为最大恒定距离CLLocationDistanceMax
,以指示只有在设备移动了相当大的距离后才应触发新的更新。