Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1.  
  2. // GENERATE MAP
  3.  
  4. var map_element = L.map('map-reference').setView([52.368, 5.5], 9);
  5.  
  6. // Mapbox maps tile
  7. L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
  8. attribution: '',
  9. maxZoom: 18,
  10. id: 'mapbox.streets',
  11. accessToken: 'pk.eyJ1Ijoia3dhbnN1cHAiLCJhIjoiY2p0NjZwZXplMDNoczQ0cWwzZ3IzNHJsdiJ9.zqrnapW2W_gpZrtLu5dSMQ'
  12. }).addTo(map_element);
  13. L.Control.geocoder().addTo(map_element);
  14.  
  15. map_element.invalidateSize();
  16.  
  17. // // OSM original tiles
  18. // L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
  19. // attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
  20. // }).addTo(map);
  21.  
  22. // Locate
  23. // initiate list variable to contain current location latitude and longitude
  24. var currentLocation = [0, 0]
  25. // on locate, zoom and keep watch
  26. map_element.locate({setView: true,
  27. maxZoom: 16,
  28. watch: true
  29. });
  30.  
  31. map_element.on('locationfound', function(e) {
  32. var radius = e.accuracy / 10;
  33. currentLocation = [e.latlng.lat, e.latlng.lng]
  34. console.log("currentLocation: ", currentLocation)
  35. control.spliceWaypoints(0, 1, e.latlng);
  36. L.marker(e.latlng).addTo(map_element)
  37. .bindPopup("You are within " + radius + " meters from this point").openPopup();
  38. L.circle(e.latlng, radius).addTo(map_element);
  39. });
  40.  
  41.  
  42. // // GEOLOCATION ALTERNATIVE
  43. //navigator.geolocation.getCurrentPosition(function(location) {
  44. // var latlng = new L.LatLng(location.coords.latitude, location.coords.longitude);
  45. //
  46. // var mymap = L.map('mapid').setView(latlng, 13)
  47. // L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
  48. // attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://mapbox.com">Mapbox</a>',
  49. // maxZoom: 18,
  50. // id: 'mapbox.streets',
  51. // accessToken: 'pk.eyJ1IjoiYmJyb29rMTU0IiwiYSI6ImNpcXN3dnJrdDAwMGNmd250bjhvZXpnbWsifQ.Nf9Zkfchos577IanoKMoYQ'
  52. // }).addTo(mymap);
  53. //
  54. // var marker = L.marker(latlng).addTo(mymap);
  55. //});
  56.  
  57.  
  58. // INTERACTION WITH MAP
  59.  
  60.  
  61. control = L.Routing.control({
  62. waypoints: [
  63. L.latLng(),
  64. L.latLng()
  65. ],
  66. router: L.Routing.mapbox('pk.eyJ1Ijoia3dhbnN1cHAiLCJhIjoiY2p0NjZwZXplMDNoczQ0cWwzZ3IzNHJsdiJ9.zqrnapW2W_gpZrtLu5dSMQ',{
  67. //possible profiles: mapbox/driving-traffic, mapbox/driving, mapbox/walking, mapbox/cycling
  68. profile: 'mapbox/driving'
  69. }),
  70. reverseWaypoints: true,
  71. showAlternatives: true,
  72. geocoder: L.Control.Geocoder.nominatim(),
  73. //router: L.Routing.osrmv1("profile"),
  74. waypointNameFallback: function(latLng) {
  75. function zeroPad(n) {
  76. n = Math.round(n);
  77. return n < 10 ? '0' + n : n;
  78. }
  79. function sexagesimal(p, pos, neg) {
  80. var n = Math.abs(p),
  81. degs = Math.floor(n),
  82. mins = (n - degs) * 60,
  83. secs = (mins - Math.floor(mins)) * 60,
  84. frac = Math.round((secs - Math.floor(secs)) * 100);
  85. return (n >= 0 ? pos : neg) + degs + '°' +
  86. zeroPad(mins) + '\'' +
  87. zeroPad(secs) + '.' + zeroPad(frac) + '"';
  88. }
  89.  
  90. return sexagesimal(latLng.lat, 'N', 'S') + ' ' + sexagesimal(latLng.lng, 'E', 'W');
  91. },
  92. altLineOptions: {
  93. styles: [
  94. {color: 'blue', opacity: 1, weight: 2}
  95. ]
  96. },
  97. })
  98. //.on('routeselected', function(e) {
  99. //var route = e.route;
  100. //alert('Showing route between waypoints:\n' + JSON.stringify(route.inputWaypoints, null, 2));
  101. //})
  102. .addTo(map_element);
  103.  
  104. L.Routing.errorControl(control).addTo(map_element);
  105.  
  106. // initiate list variable to store clicked location latitude and longitude
  107. //var clickedLocation = [0, 0]
  108. var marker = {};
  109. function createButton(label, container) {
  110. var btn = L.DomUtil.create('button', '', container);
  111. btn.setAttribute('type', 'button');
  112. btn.innerHTML = label;
  113. return btn;
  114. }
  115.  
  116.  
  117.  
  118.  
  119. map_element.on('click', function(e) {
  120. var container = L.DomUtil.create('div'),
  121. startBtn = createButton('Start from this location', container),
  122. destBtn = createButton('Go to this location', container);
  123.  
  124. L.popup()
  125. .setContent(container)
  126. .setLatLng(e.latlng)
  127. .openOn(map_element);
  128.  
  129. L.DomEvent.on(startBtn, 'click', function() {
  130. control.spliceWaypoints(0, 1, e.latlng);
  131. map_element.closePopup();
  132. });
  133. L.DomEvent.on(destBtn, 'click', function() {
  134. control.spliceWaypoints(control.getWaypoints().length - 1, 1, e.latlng);
  135. map_element.closePopup();
  136. });
  137. });
  138.  
  139. //var helloPopup = L.popup().setContent('Hello World!');
  140. /*
  141. var walking_btn;
  142. var cycling_btn;
  143. var driving_btn;
  144. var traffic_btn;
  145.  
  146. L.easyButton('<img src="http://cdn.onlinewebfonts.com/svg/img_427005.png" style="width:15px">', function(btn, map){
  147. //helloPopup.setLatLng(map.getCenter()).openOn(map);
  148. //this.disable();
  149. walking_btn = btn;
  150. reset_other_buttons();
  151. btn.button.style.backgroundColor = 'grey';
  152. control.getRouter().options.profile = "mapbox/walking";
  153. control.route();
  154. }).addTo( map_element );
  155.  
  156.  
  157.  
  158.  
  159. L.easyButton('<img src="https://image.flaticon.com/icons/png/512/60/60693.png" style="width:23px">', function(btn, map){
  160. //helloPopup.setLatLng(map.getCenter()).openOn(map);
  161. cycling_btn = btn;
  162. reset_other_buttons();
  163. btn.button.style.backgroundColor = 'grey';
  164. control.getRouter().options.profile = "mapbox/cycling";
  165. control.route();
  166. }).addTo( map_element );
  167.  
  168. L.easyButton('<img src="http://static.thenounproject.com/png/72-200.png" style="width:23px">', function(btn, map){
  169. //helloPopup.setLatLng(map.getCenter()).openOn(map);
  170. driving_btn = btn;
  171. reset_other_buttons();
  172. btn.button.style.backgroundColor = 'grey';
  173. control.getRouter().options.profile = "mapbox/driving";
  174. control.route();
  175. }).addTo( map_element );
  176.  
  177. L.easyButton('<img src="https://cdn4.iconfinder.com/data/icons/transport-56/30/Traffic_Jam-512.png" style="width:23px">', function(btn, map){
  178. //helloPopup.setLatLng(map.getCenter()).openOn(map);
  179. traffic_btn = btn;
  180. reset_other_buttons();
  181. btn.button.style.backgroundColor = 'grey';
  182. control.getRouter().options.profile = "mapbox/driving-traffic";
  183. control.route();
  184. }).addTo( map_element );
  185. */
  186.  
  187. var zoomBar = L.easyBar([
  188. L.easyButton('<img src="http://cdn.onlinewebfonts.com/svg/img_427005.png" style="width:15px">', function(btn, map){
  189. //helloPopup.setLatLng(map.getCenter()).openOn(map);
  190. //this.disable();
  191. walking_btn = btn;
  192. //reset_other_buttons();
  193. //btn.button.style.backgroundColor = 'grey';
  194. control.getRouter().options.profile = "mapbox/walking";
  195. control.route();
  196. }),
  197. L.easyButton('<img src="https://image.flaticon.com/icons/png/512/60/60693.png" style="width:23px">', function(btn, map){
  198. //helloPopup.setLatLng(map.getCenter()).openOn(map);
  199. cycling_btn = btn;
  200. //reset_other_buttons();
  201. //btn.button.style.backgroundColor = 'grey';
  202. control.getRouter().options.profile = "mapbox/cycling";
  203. control.route();
  204. }),
  205. L.easyButton('<img src="http://static.thenounproject.com/png/72-200.png" style="width:23px">', function(btn, map){
  206. //helloPopup.setLatLng(map.getCenter()).openOn(map);
  207. driving_btn = btn;
  208. //reset_other_buttons();
  209. //btn.button.style.backgroundColor = 'grey';
  210. control.getRouter().options.profile = "mapbox/driving";
  211. control.route();
  212. }),
  213. L.easyButton('<img src="https://cdn4.iconfinder.com/data/icons/transport-56/30/Traffic_Jam-512.png" style="width:23px">', function(btn, map){
  214. //helloPopup.setLatLng(map.getCenter()).openOn(map);
  215. traffic_btn = btn;
  216. //reset_other_buttons();
  217. //btn.button.style.backgroundColor = 'grey';
  218. control.getRouter().options.profile = "mapbox/driving-traffic";
  219. control.route();
  220. })
  221. ]);
  222.  
  223. // add it to the map
  224. zoomBar.addTo(map_element);
  225. /*
  226. var clickedLocation = [0, 0]
  227. var marker = {};
  228. // map_element.on('click', onMapClick);
  229. map_element.on('click', function(e) {
  230. clickedLocation = [e.latlng.lat, e.latlng.lng]
  231. console.log("clickedLocation: ", clickedLocation)
  232.  
  233. if (marker != undefined) {
  234. map_element.removeLayer(marker);
  235. }
  236.  
  237. // add marker to clicked location
  238. marker = L.marker(e.latlng).addTo(map_element);
  239. });
  240. */
  241.  
  242. function reset_other_buttons()
  243. {
  244. walking_btn.button.style.backgroundColor = 'white';
  245. driving_btn.button.style.backgroundColor = 'white';
  246. cycling_btn.button.style.backgroundColor = 'white';
  247. traffic_btn.button.style.backgroundColor = 'white';
  248.  
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement