2017-09-17 4 views
1

Ich mache eine Standort-App, in der ich meinen Standort auf FireBase speichern, bekomme ich meine aktuellen Koordinaten in einer Funktion..aber wie sende ich diese Koordinaten an eine andere Funktion, die sie online speichert.Wie übergebe ich Wert von einer Funktion zu einer anderen?

Hier ist mein Code:

var latPass: Double! 
var longPass: Double! 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
{ 
    var location=locations[0] 
    let span:MKCoordinateSpan=MKCoordinateSpanMake(0.01, 0.01) 
    var myLocation:CLLocationCoordinate2D=CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 
    let region:MKCoordinateRegion=MKCoordinateRegionMake(myLocation, span) 
    latPass=location.coordinate.latitude 
    longPass=location.coordinate.longitude 

} 

func post(){ 

    let lat=latPass 
    let long=longPass 

    let post : [String: Double]=["lat":lat, "long":long] 
    let dataBaseRef=FIRDatabase.database().reference() 
    dataBaseRef.child("Location").childByAutoId().setValue(post) 

} 

mein latPass & Langpass- sind die Koordinaten, die ich post()

Wie passieren will, kann ich es tun? Hilfe!

Antwort

0

Sie haben sie global in Ihrer Klasse deklariert, so dass Sie überall darauf zugreifen können, wie Sie es jetzt getan haben. Keine Notwendigkeit, zusätzliche Variablen zu erstellen.

func post() { 
    let post : [String: Double]=["lat":latPass, "long":longPass] 
    let dataBaseRef=FIRDatabase.database().reference() 
    dataBaseRef.child("Location").childByAutoId().setValue(post) 
} 

Sonst könnte man Parameter in Ihrer Post-Funktion eingestellt:

func post(lat: Double, long: Double) { 
    let post : [String: Double]=["lat":lat, "long":long] 
    let dataBaseRef=FIRDatabase.database().reference() 
    dataBaseRef.child("Location").childByAutoId().setValue(post) 
} 

Beitrag call now:

post(lat: 11.000, long: 12.000) 

Update:
Aktualisieren Sie Ihre locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) dazu:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    // if you pass this guard, then you have a valid loction 
    guard let latitude = manager.location?.coordinate.latitude, let longitude = manager.location?.coordinate.longitude else { return } 
    let span:MKCoordinateSpan=MKCoordinateSpanMake(0.01, 0.01) 
    let myLocation:CLLocationCoordinate2D=CLLocationCoordinate2DMake(latitude, longitude) 
    let region:MKCoordinateRegion=MKCoordinateRegionMake(myLocation, span) 
    latPass = latitude 
    longPass = longitude 
    // Call post here because now you have valid a location 
    post() 
} 
+0

Ich bekomme 'nil', wenn ich' latPass' in meinem 'post()' –

+0

@NaveenSaini drucke, überprüfen Sie das Update. Aktualisieren Sie diese Funktion. –

Verwandte Themen