Javascript 传递参数以回调或设置 DistanceMatrixStatus 中的变量值 [英] Javascript Pass Parameter to Callback or Set Variable Value in DistanceMatrixStatus

查看:17
本文介绍了Javascript 传递参数以回调或设置 DistanceMatrixStatus 中的变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Google 的 DistanceMatrixService.下面的代码有效,但是,如何将另一个参数传递给回调函数或从回调中获取其中一个值?

例如:我有两个 div,我想在(Results1 和 Results2)中显示不同的结果,所以我想我需要要么
将另一个值传递给 GoogleMapDistance 函数,例如 GoogleMapDistance(YourLatLong,DestLatLong,TheDiv)

能够在回调之外从外部获取 ResultStr document.getElementById("Results1").innerHTML = ResultStr;

将innerHTM设置为函数document.getElementById("Results1").innerHTML = GoogleMapDistance(YourLatLong,DestLatLong)的返回值;

我被卡住了.我怎样才能做到这一点?现在看来,我只能运行所有这些代码一次,并且只将它写入一个 div.

<div id="Results2"></div><script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script><脚本>函数 GoogleMapDistance(YourLatLong,DestLatLong){var service = new google.maps.DistanceMatrixService();service.getDistanceMatrix({起源:[YourLatLong],目的地:[DestLatLong],旅行模式:google.maps.TravelMode.DRIVING,单位系统:google.maps.UnitSystem.IMPERIAL,避开高速公路:假,避免通行费:假}, 打回来);}函数回调(响应,状态){如果(状态 == google.maps.DistanceMatrixStatus.OK){var origins = response.originAddresses;var 目的地 = response.destinationAddresses;for (var i = 0; i < origins.length; i++){var 结果 = response.rows[i].elements;for (var j = 0; j

解决方案

你不能改变谷歌调用回调的方式,但你可以让它调用你自己的本地函数作为回调,然后让它(通过闭包)添加所需的额外参数后调用另一个回调,如下所示:

function GoogleMapDistance(YourLatLong,DestLatLong, item){var service = new google.maps.DistanceMatrixService();service.getDistanceMatrix({起源:[YourLatLong],目的地:[DestLatLong],旅行模式:google.maps.TravelMode.DRIVING,单位系统:google.maps.UnitSystem.IMPERIAL,避开高速公路:假,避免通行费:假}, function(response, status) {callback(response, status, item)});}

或者,您可以直接定义您的回调内联,以便它可以直接访问父函数变量:

function GoogleMapDistance(YourLatLong,DestLatLong, item){var service = new google.maps.DistanceMatrixService();service.getDistanceMatrix({起源:[YourLatLong],目的地:[DestLatLong],旅行模式:google.maps.TravelMode.DRIVING,单位系统:google.maps.UnitSystem.IMPERIAL,避开高速公路:假,避免通行费:假}, 函数回调(响应,状态){//你可以在这里访问像 item 这样的父作用域参数如果(状态 == google.maps.DistanceMatrixStatus.OK){var origins = response.originAddresses;var 目的地 = response.destinationAddresses;for (var i = 0; i < origins.length; i++){var 结果 = response.rows[i].elements;for (var j = 0; j 

I have been playing a little bit with Google's DistanceMatrixService. The code below works, but, how can I pass another parameter to the callback function or grab one of the values out of the callback?

For example: I have two divs that I want to show different results in (Results1 and Results2), so I am thinking I need to either
pass another value to the GoogleMapDistance function like GoogleMapDistance(YourLatLong,DestLatLong,TheDiv)
or
be able to grab the ResultStr externally outside of the callback document.getElementById("Results1").innerHTML = ResultStr;
or
set the innerHTM to the returned value of the function document.getElementById("Results1").innerHTML = GoogleMapDistance(YourLatLong,DestLatLong);

I'm stuck. How can I do accomplish this? The way it looks right now is that I am only going to be able to run all this code once and have it only write to one 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 + "&nbsp; (<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>

解决方案

You can't change how Google calls the callback, but you can let it call your own locally function as the callback and then have that (via a closure) call another callback after adding the desired extra argument like this:

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)});
}

Or, you could just define your callback inline so it has access to the parent function variables directly:

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 + "&nbsp; (<i>" + duration + "</i>)";
              }
          }
        document.getElementById("Results1").innerHTML = ResultStr;
        }
    }

)}

这篇关于Javascript 传递参数以回调或设置 DistanceMatrixStatus 中的变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆