0
didUpdateLocationsが何度も呼び出されています。だから私はこれを止めることができます。 didUpdateLocationsでlocationManager.stopUpdatingLocation()を使用しようとしましたが、正常に動作していません。didUpdateLocationsがすぐに何度も呼び出されています3
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
}
Udatedコードあなたは以下のようにCLLocationマネージャのdistanceFilterプロパティを設定する必要が
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
}
どのようにstartUpdatingLocation()が複数回呼び出されていることを確信していますか?コードの外観からは、ビューをロードするたびに1度だけ呼び出す必要があります。 – dylanthelion
その場合、これは予想される動作です。そのメソッドは、あなたのkCLLocationAccuracyで決められた頻度で呼び出されます。 @ anbuの答えを参照してください。基本的には、正確さが高いほど、デリゲートでそのメソッドが呼び出される頻度が高くなり、携帯電話が使用する電力が増えます(GPSはバッテリーを非常に迅速に消費します)。 – dylanthelion