2012-06-08 2 views
1

service.getDistanceMatrix 호출에 문제가 있습니다.기본 google maps api 및 콜백

<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> 

대부분이 Google 샘플에서 직접 가져 왔습니다. 그러나 양식을 새로 고치기 위해 단추를 추가하려고합니다. service1.getDistanceMatrix에서 문제가있는 것 같습니다. 오류 0을 말합니다.

이 문제를 해결할만한 아이디어가 있습니까?

는 이러한 참조는 말에 .value 필요

답변

1

감사합니다 :

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

는 개인적으로 내가 orig, dest 아래 dist에 대한 document.getElementById를 사용합니다 (I가 더 정확 있기 때문에 더 안전 느낌)하지만 방법은 그것도 작동합니다 쓰여집니다. 나는 그것이 작동 할 것이라고 생각하지 않기 때문에 의아해했다. 위의 첫 번째 callbackorig, dest, dist이 정의되어 있지만 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; 
+0

위대한 작품입니다. 감사 – Greg