1

到目前为止,我正在关注https://developers.google.com/maps/documentation/javascript/directions地图计算特定点之间的距离,但我需要它接收不同的值并根据这些值计算距离,我可以使用选择标签按如下方式执行,但我需要自动完成,因此当用户打开页面时,应将值发送到函数以计算距离。(我试图将值传递给 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);
      }
    });
  }
4

1 回答 1

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 函数

希望能帮助到你 :)

于 2012-09-22T07:01:21.093 回答