Google Maps API v3 travelMode作为变量

时间:2014-03-26 14:31:38

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

我正在尝试将HTML表单中的值(值来自单选按钮输入)传递到我的google maps API脚本。我可以在console.log中找到travelMode并且它看起来正确并且应该正常工作,但我不断收到此错误:“未捕获的InvalidValueError:在属性travelMode:google.maps.TravelMode.DRIVING”。如果我取出变量并直接输入驱动模式,它就可以正常工作。

是什么给出的?这是我的代码,如果需要,我可以提供更多代码:

// Get the travel method
  if (document.getElementById('bicycle').checked) {
    var method = "google.maps.TravelMode.BICYCLING";
  } else if (document.getElementById('motorcycle').checked) {
    var method = "google.maps.TravelMode.DRIVING";
  } else {
    var method = "google.maps.TravelMode.DRIVING";
  };

  console.log(method);

  var request = {
      origin: start,
      destination: end,
      travelMode: method,
  };

2 个答案:

答案 0 :(得分:2)

尝试这种方式:

var method = 'DRIVING';

var request = {
    origin: start,
    destination: end,
    travelMode: google.maps.TravelMode[method]
};

答案 1 :(得分:1)

删除引号

  if (document.getElementById('bicycle').checked) {
    var method = google.maps.TravelMode.BICYCLING;
  } else if (document.getElementById('motorcycle').checked) {
    var method = google.maps.TravelMode.DRIVING;
  } else {
    var method = google.maps.TravelMode.DRIVING;
  };