代码之家  ›  专栏  ›  技术社区  ›  David

如何用几个路径点简化google路由代码

  •  -1
  • David  · 技术社区  · 8 年前

    我有一个包含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 回复  |  直到 8 年前
        1
  •  1
  •   Napoli    8 年前

    你可以在调用之前构建你的对象 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 {
          }
        });
    
        ...
    }
    
    推荐文章