-2
我試圖從Google地圖提供的JSON中訪問持續時間數據。用戶輸入2個郵政編碼,我應該顯示時間。
function getData() {
var origin = $("#origin_pc").val() + ",australia";
var destination = $("#dest_pc").val() + ",australia";
// var origin="3135,australia";
// var destination="3155,australia";
url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&sensor=false";
return url;
}
function calculateDistance() {
// console.log(getData());
var my_json;
$.getJSON(getData(), function (json) {
my_json = json;
});
console.log(my_json.routes[0].legs[0].duration.value);
}
這裏是我的HTML
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form>
<input type="text" id="origin_pc" />
<input type="text" id="dest_pc" />
<input type="submit" onclick="calculateDistance();">
</form>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="distance.js"></script>
</body>
</html>
我安慰結果網址,打開的時候,它給了正確的JSON對象。