2012-08-09 76 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

相关问题