2014-12-28 5 views
3

Google은 출발지와 목적지 간의 경로를 계산하는 API로 사용되며 웨이 포인트 (중간 경로 중지)라는 추가 매개 변수가 있으며 경로를 최적화하여 최종 길 찾기를 지정할 수 있습니다. 모든 경유지를 통과하는 최적화 경로.Google 길 찾기 최적화

API : https://developers.google.com/maps/documentation/directions/#Waypoints

는 웨이 포인트를 통과뿐만 아니라 출발지와 목적지뿐만 아니라 최적화 있도록 경로를 최적화 할 수있는 방법이 있나요? Google은 경유지 만 최적화하고 출발지와 목적지는 고정되어 있지만 "여기에 경로를 시작하고 순서대로 이러한 경로를 따라 가면 최적화 된 경로가됩니다."

+1

게시를 제공하는 최적의 경로와 같은 의미입니까? 어디서 시작하든 관계없이? 당신은 무엇을 최적이라고 부릅니까? 최소 거리 또는 다른 것에 대해 이야기하고 있습니까? –

+0

@EmmanuelDelay, 정확하게 시작/종료하지 않으며 최단 경로를 반환합니다 (최적). – andrepcg

답변

0

이것은 실제로는 아닙니다. 질문에 대한 답변이지만 도움이 될 것입니다. 위치를 정렬 가능한 상자에 넣습니다 (jQuery-UI). 그러면 순서 (시작과 끝)를 쉽게 변경하고 다시 확인할 수 있습니다.

최적화 라우팅은 쉽지 않습니다. 나는 명백한 해결책을 보지 못했다. Dijkstra와 같은 알고리즘을 사용해야 할 수도 있습니다.

누구든지 의견이 있으면; 그들은 환영합니다.

<!DOCTYPE html> 
<html> 
    <head> 
    <title></title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" /> 
    <style> 
     #map-canvas { 
     height: 500px; 
     width: 48%; 
     float: left; 
     margin: 0px; 
     padding: 0px 
     } 
     ul.sortable { 
     height: 500px; 
     width: 48%; 
     float: left; 
     overflow-y: auto; 
     } 
     ul.sortable li { 
     border: 1px solid #eee; 
     margin: 2px; 
     cursor: pointer; 
     } 
    </style> 
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script> 
    <script> 

    // list of some locations in Brussels 
    var locations = [ 
     {lat: 50.8949444353626, lng: 4.34153383970260, title: 'Atomium'}, 
     {lat: 50.8224796094648, lng: 4.39153021574020, title: 'VUB'}, 
     {lat: 50.8348811096048, lng: 4.29784601926803, title: 'RSCA stadion'}, 
     {lat: 50.8471595652635, lng: 4.34852904081344, title: 'AB'}, 
     {lat: 50.8390463848631, lng: 4.72816848, title: 'European parliament'}, 
    ]; 
    var locationsOrdered = []; 

    // generates the <li> items 
    function generateListItems(items) { 
     var content=''; 
     for(var i=0; i<items.length; i++) { 
     content += '<li data-id="' + i + '">' + items[i].title + '</li>'; 
     } 
     return content; 
    } 

    var map; 
    function initialize() { 
     directionsDisplay = new google.maps.DirectionsRenderer(); 
     // generates the <li> items, add them to ul.sortable 
     var ul = generateListItems(locations); 
     $('ul.sortable').html(ul); 
     // make the <ul> sortable with jQuery-UI 
     locationsOrdered = locations; 
     $('ul.sortable').sortable({ 
     stop: function(event, ui) { 
      // update the order of the items 
      locationsOrdered = []; 
      $('ul.sortable li').each(function(key, value) { 
      var index = $(value).data('id'); // reads the data-id="X"; this value is the original order 
      locationsOrdered.push(locations[index]); 
      }); 
     } 
     }); 


     var my_position = new google.maps.LatLng(50.84715956, 4.3485290); 
     map = new google.maps.Map(document.getElementById('map-canvas'), { 
     center: my_position, 
     zoom: 12 
     }); 
     directionsDisplay.setMap(map); 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 

     var directionsDisplay; 
     var directionsService = new google.maps.DirectionsService(); 

     // based on https://developers.google.com/maps/documentation/javascript/examples/directions-waypoints 
     function calcRoute(locationsOrdered) { 
     // optimize? read the checkbox 
     var optimize = $('#optimize').prop('checked'); 
     // clear all previous overlays, and distance display 
     directionsDisplay.setMap(null); 
     $('#log').empty(); 

     // 
     var length = locationsOrdered.length; 
     var start = new google.maps.LatLng(locationsOrdered[0].lat, locationsOrdered[0].lng); 
     var end = new google.maps.LatLng(locationsOrdered[length - 1].lat, locationsOrdered[length - 1].lng); 
     var waypts = []; 
     var checkboxArray = document.getElementById('waypoints'); 
     for (var i=1; i < locationsOrdered.length - 1; i++) { 
      waypts.push({ 
       location: new google.maps.LatLng(locationsOrdered[i].lat, locationsOrdered[i].lng), 
       stopover: true 
      }); 
     } 

     var request = { 
      origin: start, 
      destination: end, 
      waypoints: waypts, 
      optimizeWaypoints: optimize, // depends on the checkbox 
      travelMode: google.maps.TravelMode.DRIVING 
     }; 
     directionsService.route(request, function(response, status) { 
      if (status == google.maps.DirectionsStatus.OK) { 
      // as Google changes the order of the waypoints, we will update the order of the <li> items 
      var waypoint_order = response.routes[0].waypoint_order; 
      for (var i=0; i<waypoint_order.length; i++) { 
       swapLiItem(i + 1, waypoint_order[i] + 1); // the + 1 is because the start point is not a waypoint, but it is an <li> 
      } 
      // route display 
      directionsDisplay.setDirections(response); 
      var route = response.routes[0]; 
      // display the distances 
      var totalDistance = 0; 
      log('Waypoint distances: <br>'); 
      for (var i = 0; i < route.legs.length; i++) { 
       totalDistance += route.legs[i].distance.value; 
       log(i +': ' + route.legs[i].distance.text + '<br>'); 
      } 
      log('Total distance: ' + (totalDistance/1000) + 'km'); 
      directionsDisplay.setMap(map); 
      } 
     }); 
     } 
     // How to set sortable items manually. 
     // @see http://stackoverflow.com/questions/9981563/jquery-ui-sortable-set-manually-a-position 
     function swapLiItem(from, to) { 
     $('ul.sortable li:eq(' + from + ')').insertAfter($('ul.sortable li:eq(' + to + ')')); 
     } 

     // writes a message in <div id=log"></div> 
     function log(message) { 
     $('#log').append(message); 
     } 
    </script> 
    </head> 
    <body> 
    <div id="map-canvas"></div> 
    <ul class="sortable"></ul> 
    <input type="button" id="go" value="Go" onclick="calcRoute(locationsOrdered)"><br> 
    <input type="checkbox" id="optimize" checked="checked"> auto optimize waypoints 
    <hr> 
    <div id="log"></div> 
    </body> 
</html>