CLLocationManager
とMKMapView
と同じUIViewController
を使用しています。 重要な場所が変更された場合にのみ、APIを呼び出す必要があります。この中didUpdateLocationは、大規模なロケーション変更のためにviewWillAppearが呼び出されるたびに呼び出されます
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.locationManager.requestAlwaysAuthorization()
self.locationManager.delegate = self
self.locationManager.startMonitoringSignificantLocationChanges()
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
self.locationManager.distanceFilter = 500
mapView.showsUserLocation = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("locations \(locations)")
}
}
私はアプリの背景と前景が呼び出されるdidUpdateLocations
作る時はいつでも、主な問題はあります。私はそれが呼び出されるのは、重要な場所の変更時にではなく、毎回ではなく、viewWillAppear
が呼び出されます。
MKMapView
のためdidUpdateLocations
が見つかりました。
は、なぜあなたは唯一の重要な位置情報の更新をしたい場合は、 'self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation'を持っているのですか?ユーザーの場所をマップビューに表示していますか? – Paulw11
@ Paulw11 - はいMapViewでユーザーの場所を表示しています – Cintu
これにより、マップからユーザーの場所の更新が要求され、これが代理人に配信されます。以前の場所の記録を保存し、場所の更新でその距離を確認することができます。距離がある閾値より大きい場合にのみサーバーを更新する – Paulw11