在暂停代码中我想将拖动的标记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。
答案 0 :(得分:1)
您应该将对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);
});
}