-3
var hhhhhhh = '';
        function displayLocation(latitude, longitude) {
            var request = new XMLHttpRequest();

            var method = 'GET';
            var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&sensor=true';
            var async = true;

            request.open(method, url, async);
            request.onreadystatechange = function () {
                if (request.readyState == 4 && request.status == 200) {
                    var data = JSON.parse(request.responseText);
                    var addressComponents = data.results[0].address_components;
                    for (i = 3; i < 4; i++) {
                        hhhhhhh = addressComponents[i].long_name;
                    }
                }
            };
            request.send();
        }
        console.log(hhhhhhh);

var hhhhhhh is not getting any value out of displayLocation function. In console,outside the function its null while inside the function its getting its value. Please Help.thank you

Vivek Choudhary
  • 634
  • 8
  • 14
  • `console.log(hhhhhhh)` is executed right after `var hhhhhhh = ''`. You have to call the function and then output the value of your variable – Roxoradev Jun 30 '16 at 06:59

2 Answers2

0

you need to call the function and then console the value:

var hhhhhhh = ''; function displayLocation(latitude, longitude) { var request = new XMLHttpRequest();

            var method = 'GET';
            var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + latitude + ',' + longitude + '&sensor=true';
            var async = true;

            request.open(method, url, async);
            request.onreadystatechange = function () {
                if (request.readyState == 4 && request.status == 200) {
                    var data = JSON.parse(request.responseText);
                    var addressComponents = data.results[0].address_components;
                    for (i = 3; i < 4; i++) {
                        hhhhhhh = addressComponents[i].long_name;
                    }
                }
            };
            request.send();
        }

   displayLocation(lat,long) //  you need to call the function and then console the value

        console.log(hhhhhhh);
Jayesh Chitroda
  • 4,987
  • 13
  • 18
  • Thanks for the Quick response ,sir.But still there is Problem.Now when i call the function,an error occurs "Uncaught Reference Error: latitude is not defined".Is there any solution for this? i'm sorry but i am at my learning stage. – Vivek Choudhary Jun 30 '16 at 08:49
0

You have to call your function before the console.log output

var hhhhhhh = '';
function displayLocation(latitude, longitude) {
  //your code
   hhhhhhh = "Value";
}
displayLocation(0, 0);
console.log(hhhhhhh);
Roxoradev
  • 863
  • 4
  • 13
  • Thanks for the Quick response ,sir.But still there is Problem.Now when i call the function,an error occurs "Uncaught Reference Error: latitude is not defined".Is there any solution for this? i'm sorry but i am at my learning stage. – Vivek Choudhary Jun 30 '16 at 08:50
  • Probably you are not passing value for latitude and longitude properly when you call the function – Roxoradev Jun 30 '16 at 08:53