RSA
RSA

Reputation: 1449

How to pass new position of marker into a function

in fallowing code I want to pass dragged marker LatLng into a function to get address but it is not working

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

when running it says LastLat, LastLng undefined. Getaddress fails because this.LastLat,this.LastLng has no data also console.log.

Upvotes: 1

Views: 151

Answers (1)

Alexander Leonov
Alexander Leonov

Reputation: 4794

You should to move call to GetAddress into your event listener:

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

Upvotes: 1

Related Questions