2017-02-26 2 views
0

Ich habe eine Variable mit dem Namen Punkt, die aktualisiert wird, um die lat und lng Werte eines Ortes zu halten. Allerdings wird der Wert von Punkt nicht aktualisiert, wenn ich innerhalb der if-Funktion neu zuweisen. Innerhalb der if-Funktion wird der Wert des Punktes neu zugewiesen und druckt den korrekten Wert, aber wenn er die if-Funktion verlässt, wird er wieder undefiniert.Warum wird der Wert des Punktes nicht neu zugewiesen?

generateWayPoint(address: string): string { 
    let point; 
    const point4 = new google.maps.LatLng(51.496144, -3.182328); 

    const geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'address': address }, function (results, status) { 

    if (status === google.maps.GeocoderStatus.OK) { 

     point = new google.maps.LatLng(results[0].geometry.location.lat(), 
            results[0].geometry.location.lng()); 
     this.wayPoint = point; 
     console.log('a point in the making', this.wayPoint); 
     console.log('a wayPoint in the making', this.wayPoint); 

    } else { 
     console.log('Geocode was not successful for the followin reason:', status); 

    } 
    }); 


    console.log('a point how it should be:', point4); 
    console.log('comparing a point', point); 
    console.log('comparing a wayPoint', this.wayPoint); 
    return point; 

}

Antwort

4

Das hat nichts mit let zu tun.

Die Geocode-Funktion wird Async genannt, so dass sie zum Zeitpunkt der ersten Ausgabe tatsächlich nicht zugewiesen wird.

Verwandte Themen