map.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * Closes the floating map.
  3. */
  4. function hideMap(){
  5. document.getElementById('map').innerHTML = "";
  6. document.getElementById('map_container').style.opacity = 0;
  7. document.getElementById('map_container').style.display = 'none';
  8. }
  9. /**
  10. * Opens the floating map.
  11. * Called from {@link #showMapRoute(route, title, format, lat, lon, zoom)} and {@link #showMapPoint(title, lat, lon)}.
  12. * @param title The title of the map window.
  13. */
  14. function showMap(title){
  15. document.getElementById('map_container').style.display = 'block';
  16. document.getElementById('map_container').style.opacity = 1;
  17. document.getElementById('map_title').innerHTML = title;
  18. }
  19. /**
  20. * Draws a route on the map.
  21. * Calls {@link #showMap(title)}.
  22. * @param route Route identifier.
  23. * @param title The title of the map window.
  24. * @param lat Map center latitude coordinate (default: Downtown Vitoria).
  25. * @param lon Mat center longitude coordinate (default: Downtown Vitoria).
  26. * @param zoom Map initial zoom level (default: 15);
  27. */
  28. function showMapRoute(route, title, lat = 42.846484, lon = -2.673625, zoom = 15){
  29. // Show the map window.
  30. showMap(title);
  31. // Styles for the route.
  32. var style = {
  33. 'Point': new ol.style.Style({
  34. image: new ol.style.Circle({
  35. fill: new ol.style.Fill({
  36. color: 'rgba(255,255,0,0.4)'
  37. }),
  38. radius: 25,
  39. stroke: new ol.style.Stroke({
  40. color: '#ff0',
  41. width: 1
  42. })
  43. })
  44. }),
  45. 'LineString': new ol.style.Style({
  46. stroke: new ol.style.Stroke({
  47. color: '#f00',
  48. width: 3
  49. })
  50. }),
  51. 'MultiLineString': new ol.style.Style({
  52. stroke: new ol.style.Stroke({
  53. color: '#00008f',
  54. width: 8
  55. })
  56. })
  57. };
  58. // Generate vector from gpx file.
  59. var vectorSrc = new ol.source.Vector({
  60. url: "/gpx/" + route + ".gpx",
  61. format: new ol.format.GPX()
  62. })
  63. var vector = new ol.layer.Vector({
  64. source: vectorSrc,
  65. style: function(feature) {
  66. return style[feature.getGeometry().getType()];
  67. }
  68. });
  69. // Initialize map, with background and route.
  70. var map = new ol.Map({
  71. layers: [
  72. new ol.layer.Tile({
  73. source: new ol.source.OSM()
  74. }),
  75. vector
  76. ],
  77. target: 'map',
  78. controls: ol.control.defaults({
  79. attributionOptions: {
  80. collapsible: false
  81. }
  82. }),
  83. view: new ol.View({
  84. center: ol.proj.fromLonLat([lon, lat]),
  85. zoom: zoom
  86. })
  87. });
  88. // Manually read file to get first and last point.
  89. var first = [lat, lon];
  90. var last = [lat, lon];
  91. var href = "/gpx/" + route + ".gpx";
  92. xmlhttp = new XMLHttpRequest();
  93. xmlhttp.onreadystatechange = function(){
  94. if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
  95. // Get first and last point.
  96. out = xmlhttp.responseText;
  97. out = out.substring(out.indexOf("<trkpt"));
  98. firstL = out.substring(out.indexOf("<trkpt") + 7, out.indexOf("/>") - 1);
  99. lastL = out.substring(out.lastIndexOf("<trkpt") + 7, out.lastIndexOf("/>") - 1);
  100. first = firstL.split(" ");
  101. first[0] = parseFloat(first[0].substring(5, first[0].length - 1));
  102. first[1] = parseFloat(first[1].substring(5, first[1].length - 1));
  103. last = lastL.split(" ");
  104. last[0] = parseFloat(last[0].substring(5, last[0].length - 1));
  105. last[1] = parseFloat(last[1].substring(5, last[1].length - 1));
  106. // Add start and end marker
  107. var startIcon = new ol.Feature({
  108. geometry: new ol.geom.Point(ol.proj.fromLonLat([first[1], first[0]])),
  109. name: title,
  110. });
  111. startIcon.setStyle(new ol.style.Style({
  112. image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
  113. src: '/img/misc/pinpoint-start.png',
  114. scale: 0.25
  115. }))
  116. }));
  117. var endIcon = new ol.Feature({
  118. geometry: new ol.geom.Point(ol.proj.fromLonLat([last[1], last[0]])),
  119. name: title,
  120. });
  121. endIcon.setStyle(new ol.style.Style({
  122. image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
  123. src: '/img/misc/pinpoint-end.png',
  124. scale: 0.25
  125. }))
  126. }));
  127. iconLayer = new ol.layer.Vector({
  128. source: new ol.source.Vector({
  129. features: [startIcon, endIcon]
  130. })
  131. });
  132. map.addLayer(iconLayer);
  133. }
  134. }
  135. xmlhttp.open("GET", href, true);
  136. xmlhttp.send();
  137. }
  138. /**
  139. * Puts a marker on the map.
  140. * Calls {@link #showMap(title)}.
  141. * @param title The title of the map window.
  142. * @param lat Marker latitude coordinate.
  143. * @param lon Marker longitude coordinate.
  144. */
  145. function showMapPoint(title, lat, lon){
  146. // Show the map window.
  147. showMap(title);
  148. // Set marker icon.
  149. var icon = new ol.Feature({
  150. geometry: new ol.geom.Point(ol.proj.fromLonLat([lon, lat])),
  151. name: title,
  152. });
  153. icon.setStyle(new ol.style.Style({
  154. image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
  155. src: '/img/misc/pinpoint.png',
  156. scale: 0.28
  157. }))
  158. }));
  159. iconLayer = new ol.layer.Vector({
  160. source: new ol.source.Vector({
  161. features: [icon]
  162. })
  163. });
  164. // Configure the map.
  165. var map = new ol.Map({
  166. layers: [
  167. new ol.layer.Tile({
  168. source: new ol.source.OSM()
  169. }),
  170. iconLayer
  171. ],
  172. target: 'map',
  173. controls: ol.control.defaults({
  174. attributionOptions: {
  175. collapsible: false
  176. }
  177. }),
  178. view: new ol.View({
  179. center: ol.proj.fromLonLat([lon, lat]),
  180. zoom: 15
  181. })
  182. });
  183. }