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.getElementByIdしますが(よorig, dest​​り dist正確なので安全だと思います)、書き方もうまくいきます。うまくいくとは思わなかったので少し戸惑いました。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 に答える