-1

I am trying to get the address component from geo reverse coding and return the actual caller. But even the address is getting correctly the returned value is always undefined. This is how i am trying to get the address.

var geocoder;
var addresss = codeLatLng(23.750875259244058, 56822900823215);
alert(address);

//Getting the address through reverse geo coding
var address;

function codeLatLng(lat, lng) {
    var latlng = new google.maps.LatLng(lat, lng);

    geocoder.geocode({
        'latLng': latlng
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            console.log(results)
            if (results[1]) {
                //formatted address
                alert(results[0].formatted_address)

                //find country name
                for (var i = 0; i < results[0].address_components.length; i++) {
                    for (var b = 0; b < results[0].address_components[i].types.length; b++) {

                        //there are different types that might hold a city    
                        admin_area_lvl_1 usually does in come cases looking
                        for sublocality type will
                        be more appropriate
                        if (results[0].address_components[i].types[b] ==
                            "administrative_area_level_1") {
                            //this is the object you are looking for
                            city = results[0].address_components[i];
                            break;
                        }
                    }
                }
                //city data
                alert(city.short_name + " " + city.long_name)
                address = city.short_name;

            } else {
                alert("No results found");
            }
        } else {
            alert("Geocoder failed due to: " + status);
        }
    });
    return address;
}
MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
Shahid Aleem
  • 73
  • 1
  • 1
  • 8
  • where you are getting issue in code (at which line)? – cracker Jan 06 '15 at 06:49
  • Hello Cracker, When i am calling my function var addresss = codeLatLng(this.getPosition().lat(), this.getPosition().lng()); It is not returning the address. Inside my Function i am assigning the address like this address = city.short_name; and returning at the end return address; – Shahid Aleem Jan 06 '15 at 06:52
  • plz post the cod of getPosition() also – cracker Jan 06 '15 at 06:53
  • @Cracker, i have modified the code, the getPosition is just for getting current latitude and longitude coordinates. I am getting the address component inside the jquery function when i use alert(city.short_name + " " + city.long_name). But the same if i assign to a global variable and returning. Its not returning. – Shahid Aleem Jan 06 '15 at 06:58
  • duplicate of [Why is my variable unaltered after I modify it inside of a function?](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – geocodezip Jan 06 '15 at 11:50
  • As a side note, `56822900823215` doesn't look like a valid longitude. – MrUpsidown Jan 06 '15 at 15:18

2 Answers2

0

You must use jQuery.Deferred(), beacuse this is an asynchronous function.

 var geocoder;
 var addresss = codeLatLng(this.getPosition().lat(),      
 this.getPosition().lng());

 // now your addresss wariable is a promise you can access it like this

addresss.done(function(data){
    //data is your resolved addres
    //do your staff here
    //Getting the address through reverse geo coding
    var address = data;
})


function codeLatLng(lat, lng) {
    var latlng = new google.maps.LatLng(lat, lng);

    //define jquery deffered
    var promise= new jQuery.Deferred();

    geocoder.geocode({ 'latLng': latlng }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            console.log(results)
            if (results[1]) {
                //formatted address
                console.log(results[0].formatted_address)

                //find country name
                for (var i = 0; i < results[0].address_components.length; i++) {
                    for (var b = 0; b < 
 results[0].address_components[i].types.length; b++) {

                        //there are different types that might hold a city    
   admin_area_lvl_1 usually does in come cases looking for sublocality type will 
   be more appropriate
                        if (results[0].address_components[i].types[b] == 
    "administrative_area_level_1") {
                            //this is the object you are looking for
                            city = results[0].address_components[i];
                            break;
                        }
                    }
                }
                //city data
                console.log(city.short_name + " " + city.long_name)
                 promise.resolve( city.short_name);

            } else {
                console.log("No results found");
            }
        } else {
            console.log("Geocoder failed due to: " + status);
        }
    });
    return promise;

}

OPTION 2 you can to it with a callback function like below

 var geocoder;
 var addresss = 
 codeLatLng(this.getPosition().lat(), this.getPosition().lng(), function(address){
    //do your staff here
    //Getting the address through reverse geo coding
 });


function codeLatLng(lat, lng, callback) {
    var latlng = new google.maps.LatLng(lat, lng);

    geocoder.geocode({ 'latLng': latlng }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            console.log(results)
            if (results[1]) {
                //formatted address
                console.log(results[0].formatted_address)

                //find country name
                for (var i = 0; i < results[0].address_components.length; i++) {
                    for (var b = 0; b < 
 results[0].address_components[i].types.length; b++) {

                        //there are different types that might hold a city    
   admin_area_lvl_1 usually does in come cases looking for sublocality type will 
   be more appropriate
                        if (results[0].address_components[i].types[b] == 
    "administrative_area_level_1") {
                            //this is the object you are looking for
                            city = results[0].address_components[i];
                            break;
                        }
                    }
                }
                //city data
                console.log(city.short_name + " " + city.long_name)
                //callback your address here
                callback( city.short_name);

            } else {
                console.log("No results found");
            }
        } else {
            console.log("Geocoder failed due to: " + status);
        }
    });
    callback(false);

}
Mehmet Otkun
  • 1,374
  • 9
  • 22
0

The geocoder.geocode() function is asynchronous, with a callback function that is run after the response from Google has been received. But in your case, the return address; statement is outside the callback function, so codeLatLang(lat,lng) returns the address before the response is completely received and before the address variable has been assigned a value.

Moving your return address statement inside the callback function would fix that.

(Make sure to return appropriate error values in the case of a failed response.)

nishanthshanmugham
  • 2,967
  • 1
  • 25
  • 29