简体   繁体   中英

Google Maps API v3 travelMode as variable

I'm trying to pass values from a HTML form (the values are from radio button input) to my google maps API script. I can console.log the travelMode and it looks correct and should be working, but I keep getting this error: "Uncaught InvalidValueError: in property travelMode: google.maps.TravelMode.DRIVING". If I take out the variable and directly input the driving mode, it works fine.

What gives? here's my code I can supply more if needed:

// 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,
  };

Try this way:

var method = 'DRIVING';

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

remove quotes

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM