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

时间:2017-02-10 13:13:00

标签: angular google-api ionic2 google-maps-markers

在暂停代码中我想将拖动的标记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。

1 个答案:

答案 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);
    });
}