mehul
mehul

Reputation: 55

How to call my custom function inside Google maps event, in angular 2

I am using Angular 2. Using basic google maps, all works fine ,now i want to call my custom function from

google.maps.event.addListener(map123,'click' ,(event)=>  {

    let id=123;


    var lat=event.latLng.lat();
    var lng=event.latLng.lng();


    var marker = new google.maps.Marker({
    position:  new google.maps.LatLng(parseFloat(lat),parseFloat(lng)),
    map: map123
  });
   this.serverinteraction(this.id_actual,this.lat_actual,this.lng_actual);
    });

 My custom function is in same typescript file:

serverinteraction(id , lat, lng)
        {

        }

Error: _this.serverinteraction is not a function.

How Can i call my custom function inside that event.

Upvotes: 1

Views: 628

Answers (1)

Mala
Mala

Reputation: 115

This should work

var that = this;
google.maps.event.addListener(map123,'click' ,(event)=>  
{
    let id=123;

    var lat=event.latLng.lat();
    var lng=event.latLng.lng();
    var marker = new google.maps.Marker({
        position:  new google.maps.LatLng(parseFloat(lat),parseFloat(lng)),
        map: map123
   });

   that.serverinteraction(this.id_actual,this.lat_actual,this.lng_actual);
});

Upvotes: 1

Related Questions