我正在使用找到的代码here来计算地点之间的距离。
这是fiddle和代码:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
origin = new google.maps.LatLng(40.689844,-74.044874),
destination = "Washington, DC",
service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: false,
avoidTolls: false
},
callback
);
function callback(response, status) {
orig = document.getElementById("orig"),
dest = document.getElementById("dest"),
dist = document.getElementById("dist");
if(status=="OK") {
orig.value = response.destinationAddresses[0];
dest.value = response.originAddresses[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>
Destination: <input id="dest" type="text" style="width:35em"><br>
Distance from Statue of Liberty to Washington DC, USA (by car): <input id="dist" type="text" style="width:35em"><br>
</body>
</html>
一切正常,但我想用div标签更改输入标签。但是,如果我更改“输入ID”,请执行“div id”,doesn't work:
Origin: <div id="orig" type="text" style="width:35em"></div>
Destination: <div id="dest" type="text" style="width:35em"></div>
Distance from Statue of Liberty to Washington DC, USA (by car): <div id="dist" type="text" style="width:35em"></div>
如果它是div,输入,跨度或其他任何东西,只要它具有该ID,它不应该是无关紧要的吗?应该更改什么来输出div中的结果?谢谢。
答案 0 :(得分:1)
<div>
没有value
属性,只有input fields。您可以设置其innerHTML
或textContent
属性。
orig.innerHTML = response.destinationAddresses[0];