didUpdateLocations 每次当 viewWillAppear 发生重大位置变化时调用
didUpdateLocations called every time when viewWillAppear for Significant Location Change
我在同一个 UIViewController
中使用 CLLocationManager
和 MKMapView
。
我只想在重要位置发生变化时调用 API 。
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
。
您可以手动检查与上次保存位置的距离。试试这个。
var lastLocation: CLLocation?
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let lastLocation = lastLocation, let newLocation = locations.last {
if (lastLocation.distance(from: newLocation) < manager.distanceFilter) {
return
}
}
print("locations \(locations)")
lastLocation = locations.last
}
您可以保存最后的位置并在 didUpdateLocations
方法中检查,如果不相同则采取适当的措施。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location : CLLocation = locations.last!
if location.coordinate.latitude != lastLat && location.coordinate.longitude != lastLon{
// take action
}
}
我在同一个 UIViewController
中使用 CLLocationManager
和 MKMapView
。
我只想在重要位置发生变化时调用 API 。
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
。
您可以手动检查与上次保存位置的距离。试试这个。
var lastLocation: CLLocation?
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let lastLocation = lastLocation, let newLocation = locations.last {
if (lastLocation.distance(from: newLocation) < manager.distanceFilter) {
return
}
}
print("locations \(locations)")
lastLocation = locations.last
}
您可以保存最后的位置并在 didUpdateLocations
方法中检查,如果不相同则采取适当的措施。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location : CLLocation = locations.last!
if location.coordinate.latitude != lastLat && location.coordinate.longitude != lastLon{
// take action
}
}