Search code examples
angularjsgoogle-mapsaddeventlistenereventtriggerangular-google-maps

Angular google.maps.event.addListener('place_changed', function() {}); not working


I am adding 'place_changed' listener it's not working first time when I search the address while it's working perfectly afterwards. Can anyone tell me what should be done. here is my code. Thanks in advance for your support

var events = {
      places_changed: function (searchBox) {
        var gPlace = new google.maps.places.Autocomplete(document.getElementById('map-search-box'));
        google.maps.event.addListener(gPlace, 'place_changed', function() {

          var place = gPlace.getPlace();

          var a =place.geometry.location;

    $scope.myLocation = {
      lng : place.geometry.location.D,
      lat: place.geometry.location.k
    }
    abc = {
      coords: {
        latitude: place.geometry.location.k,
        longitude:place.geometry.location.D
      }

  }
     $scope.drawMap(abc); 
}); 

}

}


Solution

  • Thanks KayAnn I have solved the issue. Actually I never checked in the function (searchBox) {} searchBox it's object which were proving all the data I required so here is updated code that is working perfectly

    var events = {
          places_changed: function (searchBox) {
           var lat = searchBox.getPlaces()[0].geometry.location.k;
           var lgn = searchBox.getPlaces()[0].geometry.location.D;
    
            $scope.myLocation = {
            lng : searchBox.getPlaces()[0].geometry.location.D,
            lat: searchBox.getPlaces()[0].geometry.location.k
          }
    
          abc = {
            coords: {
            latitude: searchBox.getPlaces()[0].geometry.location.k,
            longitude:searchBox.getPlaces()[0].geometry.location.D
            } 
           }
               $scope.drawMap(abc); } }