基本的谷歌地图 API 和回调
Basic google maps api and callback
我在使用service.getDistanceMatrix调用时遇到问题。
<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>
其中大部分直接取自谷歌样本。但是,我正在尝试添加一个按钮来刷新表单。服务1.getDistanceMatrix中似乎出了点问题。它似乎说错误 0。
有什么想法可以解决这个问题吗?
谢谢
这些引用需要在末尾.value
:
service1.getDistanceMatrix(
{
origins: [origin1.value],
destinations: [destination1.value],
就个人而言,我会在下面的orig, dest
和dist
中使用document.getElementById
(我觉得它更安全,因为它更精确),但它的编写方式也可以。我有点困惑,因为我认为它不起作用。在上面的前callback
中定义了orig, 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;
相关文章:
- MeteorJS:在带有回调的vzaar api上正确使用wrapAsync
- 回调函数中传递参数的困难(Google Map API Markers)
- 如何通过bxSlider回调api处理事件
- Google Data API上的OOP Javascript回调方法
- JavaScript回调函数和Google Feed API
- Google API 的客户端库不会执行回调
- Google Maps JavaScript API v3:getDetails(请求,回调),用于多个位置
- 如何向 chrome 扩展 API 添加回调函数
- 用于Javascript的Skydrive API中的回调URL
- Chrome API未在嵌套回调中注入cookie
- 如何在Node.js上使用twitter api实现回调函数
- 回调返回响应,但当在API中发送时,响应在节点JS中显示为空白
- 谷歌地图API v3:缩放更改后是否有回调或事件侦听器
- 使用 Javascript/JQuery 使用 Ajax 加载 Google Maps API,回调设置不正确
- Google JS API: gapi.auth.signIn 回调函数问题
- 如果失败,角度回调 API 将添加错误消息
- 无需回调即可使用通知 API 的正确方法
- 创建回调 API
- 如何使用QUnit回调api
- 如何将现有回调 API 转换为承诺