2017-02-26 3 views
0

私は場所のlat値とlng値を保持するように更新するpointという変数を持っています。しかし、if関数内で再割り当てすると、私はpointの値は更新されません。 if関数の中に入ると、pointの値は再割り当てされ、正しい値が出力されますが、if関数を出ると、未定義に戻ります。レットポイントの値が再割り当てされないのはなぜですか

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; 

}

答えて

4

これは、letでは何の関係もありません。

ジオコード関数は非同期と呼ばれるため、初めてポイントをプリントするときは実際に割り当てられません。

関連する問題