Json Google使用JS映射矩阵数据

时间:2016-02-08 17:30:58

标签: javascript json ajax parsing

我想在googlemaps上获取特定路线的实际持续时间。但我真的不明白。到目前为止,这是我在JS中的代码:

$(document).ready(function Duration() {
    var d = new Date();
$.ajax({
    url: 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=91058%20Haupt%20Str.%6+DE&destinations=91058%20Cauerstr.%208+DE&mode=driving&language=de-DE&sensor=false&traffic_model=best_guess&departure_time='+(d.getTime()+300000),
    dataType: "json"
}).done(function(data) {
        var dur = data.rows[0].elements[0].duration.text;
        $("#duration").text(dur);         
    });
});

在html中我只想用

显示
<div id="duration"></div>
你能帮帮我吗?! :) 不管怎样,谢谢

1 个答案:

答案 0 :(得分:0)

要正确执行此操作,您需要关注Google的文档。跨域规则不允许您加载您请求的资源。此代码可以使用:https://jsfiddle.net/Lw3Ls4yd/

首先,您应该注册Google Maps API密钥:https://developers.google.com/maps/documentation/javascript/get-api-key

然后将此资源添加到您的网页,将密钥添加到正确的位置。

$client = new Predis\Client();
$client->set($key, $value, 'ex', 60);

然后这个本地JS会做的伎俩:

<script async defer src="https://maps.googleapis.com/maps/api/js?key'KEY HERE' type="text/javascript"></script>