在DistanceMatrixStatus中传递参数回调或设置变量值
google maps api 3 - Javascript Pass Parameter to Callback or Set Variable Value in DistanceMatrixStatus
我一直在玩一点谷歌的DistanceMatrixService。下面的代码可以工作,但是,我如何将另一个参数传递给回调函数或从回调中获取一个值呢?
例如:我有两个div,我想在(Results1和Results2)中显示不同的结果,所以我认为我需要
传递另一个值给GoogleMapDistance函数,如GoogleMapDistance(YourLatLong,DestLatLong,TheDiv)
或
能够在回调document.getElementById("Results1")外部获取ResultStr。innerHTML = ResultStr;
或
将innerHTM设置为函数document.getElementById("Results1")的返回值。innerHTML = GoogleMapDistance(YourLatLong,DestLatLong);
我卡住了。我怎样才能做到这一点呢?现在的情况是,我只能运行所有这些代码一次,并且只写入一个div。
<div id="Results1"></div>
<div id="Results2"></div>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function GoogleMapDistance(YourLatLong,DestLatLong)
{
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [YourLatLong],
destinations: [DestLatLong],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL,
avoidHighways: false,
avoidTolls: false
}, callback);
}
function callback(response, status)
{
if (status == google.maps.DistanceMatrixStatus.OK)
{
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
for (var i = 0; i < origins.length; i++)
{
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++)
{
var element = results[j];
var from = origins[i];
var to = destinations[j];
var distance = element.distance.text;
var duration = element.duration.text;
var ResultStr = distance + " (<i>" + duration + "</i>)";
}
}
document.getElementById("Results1").innerHTML = ResultStr;
}
}
var YourLatLong = "45.4049,-122.797997";
var DestLatLong1 = "47.468893,-122.227978";
var DestLatLong2 = "61.221274,-149.831545";
GoogleMapDistance(YourLatLong,DestLatLong1);
</script>
你不能改变Google调用回调的方式,但是你可以让它调用你自己的本地函数作为回调,然后让它(通过闭包)在添加所需的额外参数后调用另一个回调,像这样:
function GoogleMapDistance(YourLatLong,DestLatLong, item)
{
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [YourLatLong],
destinations: [DestLatLong],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL,
avoidHighways: false,
avoidTolls: false
}, function(response, status) {callback(response, status, item)});
}
或者,你可以内联定义回调,这样它就可以直接访问父函数变量:
function GoogleMapDistance(YourLatLong,DestLatLong, item)
{
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [YourLatLong],
destinations: [DestLatLong],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL,
avoidHighways: false,
avoidTolls: false
}, function callback(response, status)
{
// you can access the parent scope arguments like item here
if (status == google.maps.DistanceMatrixStatus.OK)
{
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
for (var i = 0; i < origins.length; i++)
{
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++)
{
var element = results[j];
var from = origins[i];
var to = destinations[j];
var distance = element.distance.text;
var duration = element.duration.text;
var ResultStr = distance + " (<i>" + duration + "</i>)";
}
}
document.getElementById("Results1").innerHTML = ResultStr;
}
}
)}
相关文章:
- 在es6中,将带有回调的事件侦听器设置为可迭代的
- 为什么我可以't直接将console.log()设置为回调函数
- Mapbox,无法在jquery回调中设置GeoJSON
- 如何通过回调函数设置值
- Angular,设置一个回调函数,用于在工厂和控制器之间更新
- 回调以设置postdata,而无需重新加载网格
- 如何在Angular中设置异步HTTP服务的回调,以便在不定义数组的情况下在控制器中填充数组
- setTimeout()在其设置时间之前连续调用回调函数
- 传递参数以设置回调函数的动画
- Javascript:如何在回调中设置父对象变量
- 为什么ajax回调在我的本地变量中设置undefined
- 在jqueryajax中使用回调来设置在下拉菜单中选择的选定文本
- 为什么在定义回调/侦听器函数(异步消息传递,port.on)后没有立即设置全局变量
- 无法在 $.post 回调中设置作用域变量
- 使用 Javascript/JQuery 使用 Ajax 加载 Google Maps API,回调设置不正确
- 我可以在chrome扩展中为oauth2回调设置什么重定向URL
- jQuery回调设置rails用户值为true
- 通过回调设置object的成员奇怪地失败了
- 如何将事件侦听器回调设置为成员函数
- 回调设置变量晚于函数触发器