2017-07-25 3 views
0

Ich versuche zu animieren, wo der Benutzer ist, wenn er die Standortverfolgung akzeptiert. Der Code funktioniert, wenn er bereits die Standortverfolgung akzeptiert hat und lädt dann die Ansicht, aber ich möchte die Ansicht neu laden lassen, wenn Sie auf Standortverfolgung bestätigen klicken.Standort aktualisieren, wenn der Benutzer die Standortverfolgung zulässt

override func viewDidLoad() { 
    super.viewDidLoad() 


    //Prepare to get User 
    if CLLocationManager.authorizationStatus() != .authorizedAlways { 
     // May need to change to support location based notifications 
     locationManager.requestAlwaysAuthorization() 
     print("hello") 

     mapView.setVisibleMapRect(mapView.visibleMapRect, animated: true) 
    }else { 
     locationManager.startUpdatingLocation() 
    } 
    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 

    mapView.delegate = self 

} 

Animation zu Benutzerstandort-Code

//Get user location 
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    //Show the map at the users current location 
    let location = locations[0] 
    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.02,0.02) 
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 
    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span) 
    mapView.setRegion(region, animated: true) 
    self.mapView.showsUserLocation = true 

    locationManager.stopUpdatingLocation() 


} 

Antwort

2

können Sie verwenden:

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 
    switch status { 
    case .notDetermined: 
     locationManager.requestAlwaysAuthorization() 
     break 
    case .authorizedWhenInUse: 
     locationManager.startUpdatingLocation() 
     break 
    case .authorizedAlways: 
     locationManager.startUpdatingLocation() 
     break 
    default: 
     break 
    } 
} 
+0

Hey ich habe den Code hinzugefügt, aber die Funktion scheint nicht aufgerufen zu werden. –

+0

Ah, Entschuldigung! Ich habe den Beitrag bearbeitet. Dies ist für alte schnelle. Es sollte Func locationManager sein (_ Manager: CLLocationManager, didChangeAuthorization Status: CLAuthorizationStatus) {} –

+0

Vielen Dank! Es funktioniert jetzt, aber kann ich es zum aktuellen Standort animieren? –

0

Alle verwendeten Methoden Standorte aktualisieren in CLLocationManager asynchron sind, so könnte es eine Verzögerung von Der Benutzer erlaubt den Standortzugriff auf die Standortaktualisierung unabhängig von der Implementierung Ihres Codes.

Durch den Aufruf CLLocationManager().requestLocation() innerhalb locationManager(didChangeAuthorizationStatus) können Sie jedoch sicherstellen, dass Sie eine Standortaktualisierung so nah wie möglich an die Akzeptanz der Standortnutzung erhalten.

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
    switch status { 
    case .notDetermined: 
     locationManager.requestAlwaysAuthorization() 
    case .authorizedWhenInUse: 
     locationManager.requestLocation() 
    case .authorizedAlways: 
     locationManager.requestLocation() 
    default: 
     break 
    } 
} 

Dies wird Ihre CLLocationManagerDelegate Methode, sobald die asynchrone requestLocation Funktion beendet die Ausführung automatisch aufrufen.

Verwandte Themen