1

I am having a problem with the service.getDistanceMatrix call.

<html>
<head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">    </script>
<script type="text/javascript">

origin = "Chicago, IL";
destination = "Springfield, IL";
service = new google.maps.DistanceMatrixService();

service.getDistanceMatrix(
{
    origins: [origin],
    destinations: [destination],
    travelMode: google.maps.TravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.IMPERIAL,
    avoidHighways: false,
    avoidTolls: false
}, 
callback
);

function callback(response, status) {
var orig = document.getElementById("orig"),
    dest = document.getElementById("dest"),
    dist = document.getElementById("dist");

if(status=="OK") {
    orig.value = response.originAddresses[0];
    dest.value = response.destinationAddresses[0];
    dist.value = response.rows[0].elements[0].distance.text;
} else {
    alert("Error: " + status);
}
}

function doIt() {
origin1 = document.getElementById("orig");
destination1 = document.getElementById("dest");
service1 = new google.maps.DistanceMatrixService();


service1.getDistanceMatrix(
   {
        origins: [origin1],
        destinations: [destination1],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.IMPERIAL,
        avoidHighways: false,
        avoidTolls: false
    }, 
callback1
);


}

function callback1(response, status) {

if(status=="OK") {
    orig.value = response.originAddresses[0];
    dest.value = response.destinationAddresses[0];
    dist.value = response.rows[0].elements[0].distance.text;
} else {
    alert("Error: " + status);
}
}



</script>
</head>
<body>
<br>
Basic example for using the Distance Matrix.<br><br>
Origin: <input id="orig" type="text" style="width:35em"><br><br>
Destination: <input id="dest" type="text" style="width:35em"><br><br>
Distance: <input id="dist" type="text" style="width:35em">
<button onClick="doIt()">Distance</button>
</body>
</html>

Most of this was taken directly from a google sample. However, I am trying to add a button to refresh the form. Something seems to be wrong in service1.getDistanceMatrix. It seems to say error 0.

Any ideas to fix this?

thank you

4

1 回答 1

1

这些参考资料.value最后需要:

service1.getDistanceMatrix(
   {
    origins: [origin1.value],
    destinations: [destination1.value],

我个人会使用document.getElementByIdfororig, destdistbelow(我觉得它更安全,因为它更精确),但它的编写方式也有效。我有点困惑,因为我认为它不会起作用。上面orig, dest, dist第一个定义了callback,但我认为它们在callback1.

function callback1(response, status) {

if(status=="OK") {
    orig.value = response.originAddresses[0];
    dest.value = response.destinationAddresses[0];
    dist.value = response.rows[0].elements[0].distance.text;
于 2012-06-08T18:23:24.417 回答