如何将标记的新位置传递给函数

How to pass new position of marker into a function

在休闲代码中,我想将拖动的标记 LatLng 传递到函数中以获取地址,但它不起作用

public LastLat : any; 
public LastLng : any; 

。 . .

lastLatLng(marker){
  google.maps.event.addListener(marker, 'dragend', function() { 
  this.LastLat= marker.position.lat();
  this.LastLng= marker.position.lng();
  });

console.log(this.LastLat,this.LastLng);

      this.Getaddress(this.LastLat,this.LastLng);

}

Getaddress(LastLat, LastLng){
this.http.get('http://nominatim.openstreetmap.org/reverse?format=json&lat='+LastLat+ '&lon='+LastLng + '&zoom=18&addressdetails=1')
        .map(res => res.json())
        .subscribe(data => {
        this.data = data.display_name;
        console.log(this.data);
});
}

当 运行 它说 LastLat,LastLng 未定义。 Getaddress 失败,因为 this.LastLat,this.LastLng 也没有数据 console.log.

您应该将对 GetAddress 的调用移动到您的事件侦听器中:

lastLatLng(marker){
    google.maps.event.addListener(marker, 'dragend', () => { 
        this.LastLat= marker.position.lat();
        this.LastLng= marker.position.lng();
        console.log(this.LastLat,this.LastLng);
        this.Getaddress(this.LastLat,this.LastLng);
    });
}