Как я могу упростить код маршрутизации Google с несколькими путевыми точками - PullRequest
0 голосов
/ 30 мая 2018

У меня есть строка, которая содержит от 0 до 26 путевых точек:

location1=Frankfurt&location2=Berlin&location3=Paris

Следующий код может обрабатывать различное количество путевых точек и отображает их на карте, но я хочу упростить его.

  function calculateAndDisplayRoute(directionsService, directionsDisplay) {

    waypoint_dict = parseStringToDictionary(document.getElementById('waypoints').value);
    var waypoint_array = [];
    counter_waypoints = 0;
    for(var property in waypoint_dict) {
        waypoint_array.push(waypoint_dict[property]);
        counter_waypoints += 1
    }

    switch (counter_waypoints) {
        case 0:
            directionsService.route({
            origin: document.getElementById('start').value,
            destination: document.getElementById('destination').value, 
            travelMode: 'DRIVING'
            }, function(response, status) {
              if (status === 'OK') {
                directionsDisplay.setDirections(response);
              } else {
              }
            });
            break;
            break;
        case 1:
            directionsService.route({
            origin: document.getElementById('start').value,
            destination: document.getElementById('destination').value,
            waypoints: [{location: waypoint_array[0]}],
            travelMode: 'DRIVING'
            }, function(response, status) {
              if (status === 'OK') {
                directionsDisplay.setDirections(response);
              } else {
              }
            });
            break;
        case 2:
            directionsService.route({
            origin: document.getElementById('start').value,
            destination: document.getElementById('destination').value,
            waypoints: [{location: waypoint_array[0]},{location: waypoint_array[1]}],
            travelMode: 'DRIVING'
            }, function(response, status) {
              if (status === 'OK') {
                directionsDisplay.setDirections(response);
              } else {
              }
            });
            break;
        case 3:
            directionsService.route({
            origin: document.getElementById('start').value,
            destination: document.getElementById('destination').value,
            waypoints: [{location: waypoint_array[0]},{location: waypoint_array[1]}, {location: waypoint_array[2]}],
                travelMode: 'DRIVING'
            }, function(response, status) {
              if (status === 'OK') {
                directionsDisplay.setDirections(response);
              } else {
              }
            });
            break;
    }
}

1 Ответ

0 голосов
/ 30 мая 2018

Вы можете построить свой объект перед вызовом directionsService, ниже приведена небольшая модификация вашего кода, которой должно быть достаточно.

function calculateAndDisplayRoute(directionsService, directionsDisplay) {
    var waypoint_dict = parseStringToDictionary(document.getElementById('waypoints').value);

    var options = {
        origin: document.getElementById('start').value,
        destination: document.getElementById('destination').value, 
        travelMode: 'DRIVING',
        waypoints: []
    };

    for(var property in waypoint_dict) {
        options.waypoints.push({location: waypoint_dict[property]});
    }

    directionsService.route(options, function(response, status) {
      if (status === 'OK') {
        directionsDisplay.setDirections(response);
      } else {
      }
    });

    ...
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...