如何修复“未捕获的SyntaxError:参数列表后缺少”)

时间:2019-06-12 14:49:00

标签: javascript google-maps-api-3

我目前正在尝试制作一个显示距离,路线和价格的网站(针对想要乘坐出租车的客户),但是当我添加了一个将距离乘以乘数的功能时,一切都开始不起作用。 我在控制台中得到的错误是:

'未捕获的SyntaxError:缺少)参数列表后    脚本:42'

未捕获(承诺)的Vc {消息:“ initMap不是函数”,名称:“ InvalidValueError”,堆栈:“新Vc处的错误↵(Errors: Linux System Errors…rGYVNAJV_cj_TFD9lhMIKy51o&callback = initMap:122:96“ }'

function initMap() {
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
var map = new google.maps.Map(document.getElementById("map"), {
  zoom: 7,
  center: { lat: 45.75, lng: 4.85 }
});
directionsDisplay.setMap(map);

var onChangeHandler = function() {
  calculateAndDisplayRoute(directionsService, directionsDisplay);
};
document.getElementById("start").addEventListener("change", onChangeHandler);
document.getElementById("end").addEventListener("change", onChangeHandler);




}function calculateAndDisplayRoute(directionsService, directionsDisplay) {
directionsService.route(
  {
    origin: document.getElementById("start").value,
    destination: document.getElementById("end").value,
    travelMode: "DRIVING"
  },
  function(response, status) {
    if (status === "OK") {
      document.getElementById("distance").innerHTML =
        "la distance total du trajet est: " +
        (response.routes[0].legs[0].distance.value / 1000).toFixed(2) +
        " km";
        /*
      document.getElementById("price").innerHTML =
        "Vous devrez donc payer:" +
        ((response.routes[0].legs[0].distance.value / 1000) * 2.5).toFixed(
          2
        ) +
        "€";*/
      //alert('Total travel distance is: ' + (response.routes[0].legs[0].distance.value / 1000).toFixed(2) + ' km');
      directionsDisplay.setDirections(response);
    } else {
      //window.alert('Directions request failed due to ' + status);
    }
  )}
  function tarifs(response, status){
    var hours = document.querySelector('#hours');
    if(7<=hours.value<=19){
      document.getElementById("price").innerHTML =
        "Vous devrez donc payer:" +
        ((response.routes[0].legs[0].distance.value / 1000) * 1.66).toFixed(2) + "€";
    }else if(19<=hours.value<=23){
      document.getElementById("price").innerHTML =
        "Vous devrez donc payer:" +
        ((response.routes[0].legs[0].distance.value / 1000) * 2.50).toFixed(2) + "€";
    }else if(0<=hours.value<=7){
      document.getElementById("price").innerHTML =
        "Vous devrez donc payer:" +
        ((response.routes[0].legs[0].distance.value / 1000) * 2.50).toFixed(2) + "€";
    }
  }
);}

1 个答案:

答案 0 :(得分:0)

我认为这就是您要寻找的。如果您美化代码并使用VSCode这样的语法突出显示工具,则更容易查明错误:

  function initMap() {
    var directionsService = new google.maps.DirectionsService();
    var directionsDisplay = new google.maps.DirectionsRenderer();
    var map = new google.maps.Map(document.getElementById("map"), {
      zoom: 7,
      center: {
        lat: 45.75,
        lng: 4.85
      }
    });
    directionsDisplay.setMap(map);

    var onChangeHandler = function () {
      calculateAndDisplayRoute(directionsService, directionsDisplay);
    };
    document.getElementById("start").addEventListener("change", onChangeHandler);
    document.getElementById("end").addEventListener("change", onChangeHandler);
  }

  function calculateAndDisplayRoute(directionsService, directionsDisplay) {
    directionsService.route({
      origin: document.getElementById("start").value,
      destination: document.getElementById("end").value,
      travelMode: "DRIVING"
    },
      function (response, status) {
        if (status === "OK") {
          document.getElementById("distance").innerHTML =
            "la distance total du trajet est: " +
            (response.routes[0].legs[0].distance.value / 1000).toFixed(2) +
            " km";
          /*
          document.getElementById("price").innerHTML =
            "Vous devrez donc payer:" +
            ((response.routes[0].legs[0].distance.value / 1000) * 2.5).toFixed(
              2
            ) +
            "€";*/
          //alert('Total travel distance is: ' + (response.routes[0].legs[0].distance.value / 1000).toFixed(2) + ' km');
          directionsDisplay.setDirections(response);
        } else {
          //window.alert('Directions request failed due to ' + status);
        }
      });
  }

  function tarifs(response, status) {
    var hours = document.querySelector('#hours');
    if (7 <= hours.value <= 19) {
      document.getElementById("price").innerHTML =
        "Vous devrez donc payer:" +
        ((response.routes[0].legs[0].distance.value / 1000) * 1.66).toFixed(2) + "€";
    } else if (19 <= hours.value <= 23) {
      document.getElementById("price").innerHTML =
        "Vous devrez donc payer:" +
        ((response.routes[0].legs[0].distance.value / 1000) * 2.50).toFixed(2) + "€";
    } else if (0 <= hours.value <= 7) {
      document.getElementById("price").innerHTML =
        "Vous devrez donc payer:" +
        ((response.routes[0].legs[0].distance.value / 1000) * 2.50).toFixed(2) + "€";
    }
  }