javascript - 从距离矩阵获取变量,Google map v3

标签 javascript json google-maps google-maps-api-3

问题是我有这段代码:

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);
    }
}

而且我无法获取 getJSON 之外的距离值。我正在搜索这个,我发现我可以在网址末尾使用 "&callback=" ,但它对我不起作用(可能是因为我不确定如何使用它)。 怎么解决呢?

提前致谢

最佳答案

使用 JQuery.data() 函数将结果存储在 HTML 元素中:

$('#div_id').data('distance', distance)

之后您可以加载结果:

var distance = $('#div_id').data('distance')

$.getJSON()中使用async: false选项来避免未定义错误

关于javascript - 从距离矩阵获取变量,Google map v3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19583360/

相关文章:

javascript - D3 力向图 : update node position

javascript - 无法在 JSON 字符串上使用 .indexof,因为它没有方法 'indexof' javascript

ios - swift 2 : NSData(contentsOfURL:url) returning nil

javascript - Google map API v3 - 如何检测客户端负载?

java - map 上未显示位置

javascript - DIV 中支持 jQueryMobile 的站点

javascript - IE 中的 document.url 问题

javascript - 暂停/关闭 three.js TrackballControls

java - 将具有重复键的 JSON 对象转换为 JSON 数组

javascript - 带有 PhoneGap 的 iPhone 版谷歌地图