1

What I want to do is that I need to create markers from stored location of user. So for that I need latitude and longitude. I got the lat & lon in my function get_current_user_lat_lon . Now I want to return both lat & lon through Object but my return is not working....

function initialize(){
    var var_get_current_user_lat_lon = get_current_user_lat_lon(); 
    var latitutde = var_get_current_user_lat_lon.latitutde;
    alert(latitutde);// not even alert
    var longitutde = var_get_current_user_lat_lon.longitutde;
    alert(longitutde); // not even alert
}        

function get_current_user_lat_lon(){
    var address = $("#hidden_current_location").val();//Working fine
    geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var variable = results[0].geometry.location;
            var lat  = variable.lat();
            var lon  = variable.lng();
            alert(lat); //Working fine
            alert(lon); //Working fine
            //return not working here
        }
        //return not working here
    });
    return {latitutde: lat , longitutde : lon};// giving error lat.lon is not defined means return not working here
}

1 Answer 1

1

Your "lat" and "lon" variables are declared inside the callback from "geocode()". Even if you declared them outside it, however, the overall idea would not work. The "geocode()" function is asynchronous; that's the reason there's a callback function in the first place.

Instead of structuring your code so that your "get_current_user_lat_lon()" function returns a value, follow the lead of the Google architecture and make your own function take a callback:

function get_current_user_lat_lon( callback ){
  var address = $("#hidden_current_location").val();//Working fine
  geocoder = new google.maps.Geocoder();
  geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var variable = results[0].geometry.location;
        var lat  = variable.lat();
        var lon  = variable.lng();
        callback({latitude: lat, longitude: lon});
      }

  });

}
Sign up to request clarification or add additional context in comments.

2 Comments

so what to do now ??shall i go to do file_get_contents(googlemaps etc etc) docodeing n etc....
Yes - you have to do the things you need to do after the geocoding returns from inside the callback function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.