2017-08-09 4 views
2

Ich verwende CLLocationManager und MKMapView in derselben UIViewController. Ich möchte eine API nur aufrufen, wenn sich der signifikante Standort ändert.didUpdateLocations aufgerufen jedes Mal wenn viewWillAppear für signifikante Standortänderung

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

} 

Dabei ist das Hauptproblem, wenn ich die app Hintergrund und Vordergrund machen didUpdateLocations aufgerufen. Ich möchte es nur bei signifikanter Standortänderung aufgerufen werden, nicht jedes Mal wenn viewWillAppear aufgerufen wird.

Ich fand es wegen MKMapView, didUpdateLocations wird aufgerufen.

+0

Warum haben Sie 'self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation', wenn Sie nur wichtige Standortaktualisierungen wünschen? Zeigen Sie den Benutzerstandort in Ihrer Kartenansicht an? – Paulw11

+0

@ Paulw11 - Ja, ich zeige den Benutzerstandort in MapView an – Cintu

+0

Dies wird die Map veranlassen, eine Benutzerstandortaktualisierung anzufordern, die an Ihren Delegaten geliefert wird. Sie können Ihren vorherigen Standort aufzeichnen und die Entfernung in der Standortaktualisierung überprüfen. Aktualisieren Sie den Server nur, wenn die Entfernung größer als ein Schwellenwert ist. – Paulw11

Antwort

1

Sie können die Entfernung mit der zuletzt gespeicherten Position manuell überprüfen. Versuche dies.

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

Sie können die letzten Orte speichern und innerhalb didUpdateLocations Methode überprüfen, wenn nicht identisch die entsprechenden Maßnahmen ergreifen.

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

    let location : CLLocation = locations.last! 

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