Reputation: 45
The problem is that i have this code:
function calcDist() {
var calcdirection = document.getElementById('street').value + "+" + document.getElementById('number').value + "+UK";
for (var x = 0; x < dbdirection.length; x++) {
var dirdest = dbdirection[x].replace(" ","+")+ "+UK";
$.getJSON("http://maps.googleapis.com/maps/api/distancematrix/json?origins="+calcdirection+"&destinations="+dirdest+"&mode=walking&language=es-ES&sensor=false", function(datos) {
var distance = datos.rows[0].elements[0].distance.value;
// return distance;
});
// alert(distance);
}
}
And I can't get the distance value outside the getJSON
. I was searching about this and I found that i can use "&callback="
at the end of the url, but it doesn't work for me (probably because I'm not sure about how to use it).
How can I solve it?
Thanks in advance
Upvotes: 1
Views: 762
Reputation: 1125
Store the result in an HTML element with JQuery.data()
function:
$('#div_id').data('distance', distance)
And after that you can load the results:
var distance = $('#div_id').data('distance')
use the async: false
option in $.getJSON()
to avoid undefined
errors
Upvotes: 1