如何在javascript(google Map)中将值传递给函数

时间:2012-09-22 06:02:47

标签: javascript google-maps google-maps-api-3

我关注https://developers.google.com/maps/documentation/javascript/directions 到目前为止,地图计算特定点之间的距离,但我需要它来接收不同的值并根据这些值计算距离,我可以使用select标签执行以下操作,但我需要它自动完成,以便用户打开时在页面上,应将值发送到函数以计算距离。 (我试图将值传递给calcRoute函数,但它不起作用)

另一个问题是如何将列表(对于航点部分)传递给函数?

  function calcRoute() {
            var travel = document.getElementById("mode").value;   << currently it is done manually
            var origin = document.getElementById("origin").value; << manually
    var request = {
      origin: origin,
      destination: '238 Elizabeth St,Los Angeles, CA',
      waypoints:[{location: "George Street,Los Angeles,CA"}],
      travelMode: travel
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });
  }

1 个答案:

答案 0 :(得分:0)

尝试更改calcRoute函数定义,如下所示。

function calcRoute(){
  var travel = document.getElementById("mode").value; 
  var origin = document.getElementById("origin").value;

function calcRoute(x,y){
  if(x && y){
    // your code to process your parameter values
    var travel = x
    var origin = y
  }else{
    // your code to process, if no parameters given (for default functionality)
    var travel = document.getElementById("mode").value;   << currently
    var origin = document.getElementById("origin").value; << manually
  }

并尝试使用参数调用calcRoute函数,当您想要传递不同的值时

希望有所帮助:)