Search code examples
javascriptgoogle-mapsgoogle-maps-api-3geojson

Google Maps reverse geocoding address appears after second click?


Currently if I click on a marker, error in console shows "address undefined" but if i click it again the address shows up, why is this happening?

What my listener looks like:

map.data.addListener('click', function(event) {

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

      function getReverseGeocodingData() {
      var latlng = new google.maps.LatLng(lat, lng);
      // This is making the Geocode request
      var geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'latLng': latlng }, function (results, status) {
            if (status !== google.maps.GeocoderStatus.OK) {
                alert(status);
            }
            // This is checking to see if the Geoeode Status is OK before proceeding
            if (status == google.maps.GeocoderStatus.OK) {
                console.log(results[0].formatted_address);
                address = (results[0].formatted_address);
            }
        });
    }
      getReverseGeocodingData(lat, lng);

      infoWindow.setContent("Address: " + address + "<br>Vehicle: " + event.feature.getProperty('deviceID')+"<br> Speed: "+event.feature.getProperty('speedKPH'));
      infoWindow.setPosition(event.latLng);
      infoWindow.setOptions({pixelOffset: new google.maps.Size(0,-34)});
      infoWindow.open(map);

      });

thank you for your time and help in advance.


Solution

  • So thanks thriqon i understood the problem,

    and have come up with this solution which i'm not sure how correct it is, but it does what i need it to do. It calls for the address once they hover over the point in the background without popping up the infowindow and when they click, tada, the address is shown in the infowindow! hope this helps some people! messsssy code

     map.data.addListener('mouseover', function(event) {
    
      var lat = event.latLng.lat(); 
      var lng = event.latLng.lng(); 
    
       function getReverseGeocodingData(lat, lng) {
      var latlng = new google.maps.LatLng(lat, lng);
      // This is making the Geocode request
      var geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'latLng': latlng }, function (results, status) {
            if (status !== google.maps.GeocoderStatus.OK) {
                alert(status);
            }
            // This is checking to see if the Geoeode Status is OK before proceeding
            if (status == google.maps.GeocoderStatus.OK) {
                console.log(results[0].formatted_address);
               address = (results[0].formatted_address);
               return address;
            } 
        });
    }
      getReverseGeocodingData(lat, lng);
    
      map.data.addListener('click', function(event) {
    
      infoWindow.setContent("Address: " + address + "<br>Vehicle: " + event.feature.getProperty('deviceID') +"<br> Speed: "+event.feature.getProperty('speedKPH')+"<br> Heading:"+event.feature.getProperty('heading'));
      infoWindow.setPosition(event.latLng);
      infoWindow.setOptions({pixelOffset: new google.maps.Size(0,-34)});
      infoWindow.open(map);
    
      });
    
    });