0

I have a getJSON request. This request is working fine, but i can't store the output data in array.

Here is the current code:

var loc = [];

jQuery.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address=Los Angeles, CA 90012&sensor=false', null, function (data) {
            var p = data.results[0].geometry.location
            var latlng = (p.lat, p.lng);

            loc.push(latlng);                   
 });

console.log('Final: '+loc);

The console is showing this: 'Final: ', so the loc.push(latlng) isn't working.

How can I store getJSON datas in the outside array?

JSFIDDLE

Joci93
  • 803
  • 3
  • 10
  • 24
  • put console.log('Final: '+loc); just after loc.push(latlng); – Parth Patel Dec 09 '15 at 08:39
  • @ParthPatel I want the result outside the getJSON. – Joci93 Dec 09 '15 at 08:44
  • loc is globally initialized so you can use any where but what you are doing is that you are printing console.log('Final: '+loc); before you get response from getJSON(). so you can not find any result on your console. Javascript is asynchronous when AJAX. Please refer it http://stackoverflow.com/questions/2035645/when-is-javascript-synchronous – Parth Patel Dec 09 '15 at 08:50

0 Answers0