2012-08-09 75 views
0

我正在嘗試使用距離矩陣API來計算兩個位置之間的距離。 因爲這是我第一次嘗試使用這個API,所以我在腸道上覆制了我在Google Documentation's page上找到的代碼並粘貼在我的頁面上。 但是,我看不到任何形式的輸出,我不知道爲什麼。 這是my pageGoogle Distance Matrix API no output

這是源:

<!DOCTYPE html> 
<html> 
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 

<!-- Google Api --> 
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDDMkiaVfr1y0QCsx_qUsJKIBAWpkyXZrY&sensor=false"> 
</script> 

<script type="text/javascript"> 
var origin1 = new google.maps.LatLng(55.930385, -3.118425); 
var origin2 = "Greenwich, England"; 
var destinationA = "Stockholm, Sweden"; 
var destinationB = new google.maps.LatLng(50.087692, 14.421150); 

var service = new google.maps.DistanceMatrixService(); 
service.getDistanceMatrix(
    { 
    origins: [origin1, origin2], 
    destinations: [destinationA, destinationB], 
    travelMode: google.maps.TravelMode.DRIVING, 
    avoidHighways: false, 
    avoidTolls: false 
    }, callback); 

function callback(response, status) { 
    if (status == google.maps.DistanceMatrixStatus.OK) 
    { 
    var origins = response.originAddresses; 
    var destinations = response.destinationAddresses; 
    document.writeln(response.originAddresses[1]);  
    } 

    else { 
    document.writeln("error"); 

     } 
    } 

</script> 
</html> 

回答

0

好了,所以我不知道爲什麼不起作用。首先,我建議您使用jQuery。你可以找到它here.

首先,包裝你的代碼在$(document).ready()功能,使得它執行當頁面就緒

$(document).ready(function() { 
    // Everything in your original script block 
    var origin1 = new google.maps.LatLng(55.930385, -3.118425); 
    // etc etc  
}); 

其次,它容易得多書面文檔你的時候知道你在寫信給。製作一個div,它將保存您的位置數據。

<body> 
<div id="location"/> 
</body> 

第三,當你從谷歌有自己的位置,在你div改變文本匹配:

$("#location").text(response.originAddresses[1]); 

有沒有jQuery的文檔處理是一場噩夢,我強烈建議使用這個庫。

+0

那麼我該如何解決這個顯示問題? 錯誤在哪裏? – user1305336 2012-08-09 09:16:11

+0

幫我一個忙,試試看你的''。也許你沒有把數據寫入身體? – Aesthete 2012-08-09 09:19:17

+0

完成,但結果是一樣的:沒有輸出。 – user1305336 2012-08-09 09:23:19

相關問題