-2

I am trying to return lat , lon for address it prints array on console.log but does not work in function return

var markers = new Array();
var cityCircles = new Array();
var polycordinates = new Array();
var previousepolygones = new Array();
var infoxboxs = new Array();
function initialize() {
    var mapProp = {
        center:new google.maps.LatLng(44.03121020078675, 23.41763112193103),
        zoom: <?= (isset($zoom) and $zoom != '') ? $zoom : '7' ?>,
        mapTypeId:google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("googleMap"),mapProp);

    console.log(getcountrylatlng("Romania"));
}
function getcountrylatlng(countryname){
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode(
        {'address': countryname}, 
        function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]) {
                    //var result = results[0].formatted_address;
                    //var city = result.split(" - ");
//console.log(results[0]);
                    return results[0];
                }
            }
        }
    );
}

on console.log it returns an object but if i return value as in above function it returns undefined

Need help with this ,

  • possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – geocodezip May 11 '16 at 13:23

1 Answers1

0

You return from the wrong position the retrun of console log is inside a inner function so is not returned to the caller function .

    function getcountrylatlng(countryname){
    var geocoder = new google.maps.Geocoder();
    var myValue;
    geocoder.geocode(
      {'address': countryname}, 
        function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]) {
                //var result = results[0].formatted_address;
                //var city = result.split(" - ");
                //console.log(results[0]);
                //return results[0];
                myValue =  results[0];
            }
        }
    }
  );
    return myValue;
}
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107