Question: I am using `CLLocationManager` and `MKMapView` in same `UIViewController`.
I want to call an API only when the significant location change. 

```
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)")
 }

}

```

In this, the main issue is whenever I make the app background and foreground `didUpdateLocations` get called. I want it to be called only when significant location change, not every time when `viewWillAppear` called. 

I found its because of `MKMapView`, `didUpdateLocations` is being called.

Answer: 
You can manually check the distance with last saved location. Try this.

```
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
}

```
You can save the last locations and check inside `didUpdateLocations` method, if not same the take appropriate action.

```
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let location : CLLocation = locations.last!

    if location.coordinate.latitude != lastLat && location.coordinate.longitude != lastLon{
        // take action
    }
}

```