fork download
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  5. <title>Maps TSP</title>
  6. <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false" type="text/javascript"></script>
  7. <script type="text/javascript">
  8. function calculate() {
  9. if (GBrowserIsCompatible()) {
  10. var map = new GMap2(document.getElementById("map"));
  11. map.setMapType(G_NORMAL_MAP);
  12. map.addControl(new GLargeMapControl());
  13. map.addControl(new GMapTypeControl());
  14. var directions = new GDirections(map, null);
  15. GEvent.addListener(directions, "error", function() {
  16. var message = document.getElementById("message");
  17. message.appendChild(document.createTextNode(directions.getStatus().code));
  18. message.appendChild(document.createElement("br"));
  19. });
  20. directions.loadFromWaypoints(
  21. toGLatLng(document.getElementById("input").value.split("\n")),
  22. { locale: "ja_JP" }
  23. );
  24. var message = document.getElementById("message");
  25. message.innerHTML = "";
  26. var minSeconds = Number.POSITIVE_INFINITY;
  27. var minRoute = null;
  28. var results = new Array();
  29. enumerate(document.getElementById("input").value.split("\n"), new Array(), results);
  30. setInterval(function(){
  31. if (results.length == 0 ) {
  32. return;
  33. }
  34. var array = results.shift();
  35. var directions = new GDirections(null, null);
  36. var route = toPlaceNames(array);
  37. GEvent.addListener(directions, "load", function() {
  38. var message = document.getElementById("message");
  39. message.appendChild(document.createTextNode(directions.getDuration().seconds + "秒(" + directions.getDuration().html + ")" + directions.getDistance().html.replace("&nbsp;", "") + ": " + route.join(" ")));
  40. message.appendChild(document.createElement("br"));
  41. if (directions.getDuration().seconds < minSeconds) {
  42. minSeconds = directions.getDuration().seconds;
  43. document.getElementById("output").innerHTML = route.join(" ");
  44. }
  45. });
  46. GEvent.addListener(directions, "error", function() {
  47. var message = document.getElementById("message");
  48. message.appendChild(document.createTextNode(directions.getStatus().code));
  49. message.appendChild(document.createElement("br"));
  50. });
  51. directions.loadFromWaypoints(
  52. toGLatLng(array),
  53. { locale: "ja_JP" }
  54. );
  55. }, 1000);
  56. }
  57. }
  58.  
  59. if (GBrowserIsCompatible()) {
  60. GEvent.addDomListener(window, "unload", function() { GUnload(); });
  61. }
  62.  
  63. function enumerate(inArray, outArray, results) {
  64. if (inArray.length == 0) {
  65. outArray.push(outArray[0]);
  66. results.push(outArray.slice(0));
  67. outArray.pop();
  68. } else {
  69. for (var i = 0; i < inArray.length; i++) {
  70. var value = inArray[i];
  71. outArray.push(value);
  72. inArray.splice(i, 1);
  73. enumerate(inArray, outArray, results);
  74. inArray.splice(i, 0, value);
  75. outArray.pop();
  76. }
  77. }
  78. }
  79.  
  80. function toPlaceNames(array) {
  81. var ret = new Array();
  82. for (var index in array) {
  83. var items = array[index].split(" ");
  84. ret.push(items[0]);
  85. }
  86. return ret;
  87. }
  88.  
  89. function toGLatLng(array) {
  90. var ret = new Array();
  91. for (var index in array) {
  92. var items = array[index].split(" ");
  93. ret.push(new GLatLng(items[1], items[2]));
  94. }
  95. return ret;
  96. }
  97. </script>
  98. </head>
  99. <body>
  100. <h1>Maps TSP</h1>
  101. <h2>入力</h2>
  102. <textarea id="input" style="width: 20em; height: 5em;">札幌時計台 43.062603 141.353642
  103. 通天閣 34.652554 135.506333
  104. 襟裳岬 41.926490 143.246642
  105. 京都大学 35.025280 135.780910
  106. 旭山動物園 43.768994 142.482248
  107. 幕張メッセ 35.646701 140.036654</textarea>
  108. <input type="button" value="計算する" onClick="calculate()"/>
  109. <h2>出力</h2>
  110. <div id="output" style="margin-bottom: 1em;">ここに結果が表示されます。</div>
  111. <div id="map" style="width: 400px; height: 300px; border: 1px solid gray; float: left;">ここに地図が表示されます。</div>
  112. <div id="message" style="margin-left: 400px; padding-left: 1em;">ここにメッセージが表示されます。</div>
  113. </body>
  114. </html>
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty