0
  • I am trying to get the address from the google maps api and return this address in the function.What do I need to change to make these show up?

$(document).ready(function() {
  var lat = 17.4807865;
  var lng = 78.4921649;
  var returnAddress = getAddressFromlatlng(function(backaddr,lat,lng){
     alert(" Inside : "+backaddr)
  });
  alert(returnAddress+" returned address ")
});
function getAddressFromlatlng(callback,lat,lng){
  alert(lat+" : "+lng)
  var geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(lat,lng);
  geocoder.geocode({'latLng': latlng}, function(results, status){   
    alert(results[0].formatted_address)
    callback(results[0].formatted_address);
  });
}
1

2 Answers 2

1

That's not possible, as the function returns before the value exists.

The geocode call is asynchronous, so you have to handle the result asynchronously. You already display the value successfully in the callback function, that is the way to handle the result.

You could return a promise from the getAddressFromlatlng function, but that doesn't really change anything. That's just wrapping the callback function in an object so that you can do the asynchronous handling of the result separate from the call.

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

2 Comments

thanks @Guffa, please can you look out this code for solution [](jsfiddle.net/mapsaceclouds/brnecrfw) or [](stackoverflow.com/questions/27873908/…)
@acecloudsmaps: The solution to that isn't to try to return the value from the function. The problem is that the loop variable can't be used to put the data in the array, as the data arrives after the loop has finished.
1

You should be taking the address from the result of the callback. Once the coordinates have been resolved you call a function that continues processing the returned value because only then do you have the return value. In the case your going through an array use the array index and update the address field once you have the required data. or process them individualy. work through the array making a request to google for every item. once the response comes back from google, move onto the next item in your array. If you can save the index so its known when the response arrives then it would be possible to wait for multiple requests.

var currentIndex = null;
function notifyAddressResolved (address) {
    yourArray[currentIndex].address = address;
    resolveNextAddress();
}
function getAddressFromlatlng(callback, lat, lng){
    var geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(lat, lng);  
    geocoder.geocode({'latLng': latlng}, function (results, status) {           
        callback(results[0].formatted_address);
    });
}
function resolveNextAddress () {
    if (currentIndex == null) {
        currentIndex = 0;
    } else {
        currentIndex++;
    }
    getAddressFromlatlng(notifyAddressResolved, yourArray[currentIndex].lat, yourArray[currentIndex].lng);
}

Comments

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.