0

I have this function that should return the country starting from the latitude and longitude and assign it to a global variable declare outside the function. I know that the a in Ajax stand for asynchronous and I have read every single answer on stackoverflow but I can't fix it.

    function getLatLongDetail(myLatlng) {

    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'latLng': myLatlng },
      function (results, status) {
        var country = "";

          if (status == google.maps.GeocoderStatus.OK) {
              if (results[0]) {
                  for (var i = 0; i < results[0].address_components.length; i++) {
                      var addr = results[0].address_components[i];
                      // check if this entry in address_components has a type of country
                      if (addr.types[0] == 'country')
                          country = addr.long_name;

                  }

                  }
                  return country; // this doesn't work

              }

          }

      });
}


var Country = getLatLongDetail(myLatlng);
alert(Country);// undefined

I know there are hundreds of questions about callback function but none of them worked for me.

Mat
  • 6,236
  • 9
  • 42
  • 55

1 Answers1

0

Finally I made it! Here the code:

   function getLatLongDetail(myLatlng, fn) {

    var geocoder = new google.maps.Geocoder(); 
    var country = "";
    var city = "";
    var address = "";
    var zip = "";
    var state = "";


    geocoder.geocode({ 'latLng': myLatlng },
      function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
              if (results[0]) {
                  for (var i = 0; i < results[0].address_components.length; i++) {
                      var addr = results[0].address_components[i];
                      // check if this entry in address_components has a type of country
                      if (addr.types[0] == 'country')
                      country = addr.long_name;
                     else if (addr.types[0] == ['locality'])       // City
                       city = addr.long_name;
                        else if (addr.types[0] == 'street_address') // address 1
                          address = address + addr.long_name;
                      else if (addr.types[0] == 'establishment')
                          address = address + addr.long_name;
                      else if (addr.types[0] == 'route')  // address 2
                          address = address + addr.long_name;
                      else if (addr.types[0] == 'postal_code')       // Zip
                          zip = addr.short_name;
                      else if (addr.types[0] == ['administrative_area_level_1'])       // State
                          state = addr.long_name;

                  }
              }
            fn(country,city, address, zip, state);
          }
      });
   }

   var mCountry; //global variable to store the country 
   vat mCity; //global variable to store the city 

getLatLongDetail(event.latLng, function(country,city, address, zip, state){
    mCountry = country; // now mCountry store the value from  getLatLongDetail so I can save it in the database
    mCity  = city;
    alert(address + zip + state);
   });
Mat
  • 6,236
  • 9
  • 42
  • 55