google maps api 3 - returning variable from javascript function -
i'm trying return longitude , latitude function, can console.log both of them, when try return 1 of them, undefined.
how can return latitude, longitude?
function latlong(location) { var geocoder = new google.maps.geocoder(); var address = location; var longitude; var latitude; geocoder.geocode({ 'address': address }, function (results, status) { if (status == google.maps.geocoderstatus.ok) { latitude = results[0].geometry.location.lat(); longitude = results[0].geometry.location.lng(); } else { alert("geocode not successful following reason: " + status); } console.log(longitude); }); }
you don't.
the used technique pass callback latlong
function parameter, , run function when receive result.
something like:
function latlong(location, callback) { var geocoder = new google.maps.geocoder(); var address = location; var longitude; var latitude; geocoder.geocode({ 'address': address }, function (results, status) { if (status == google.maps.geocoderstatus.ok) { latitude = results[0].geometry.location.lat(); longitude = results[0].geometry.location.lng(); callback(latitude, longitude); } else { alert("geocode not successful following reason: " + status); } console.log(longitude); }); }
Comments
Post a Comment