mapresult.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* SPDX-License-Identifier: AGPL-3.0-or-later */
  2. /* global L */
  3. (function (w, d, searxng) {
  4. 'use strict';
  5. searxng.ready(function () {
  6. searxng.on('.searxng_init_map', 'click', function (event) {
  7. // no more request
  8. this.classList.remove("searxng_init_map");
  9. //
  10. var leaflet_target = this.dataset.leafletTarget;
  11. var map_lon = parseFloat(this.dataset.mapLon);
  12. var map_lat = parseFloat(this.dataset.mapLat);
  13. var map_zoom = parseFloat(this.dataset.mapZoom);
  14. var map_boundingbox = JSON.parse(this.dataset.mapBoundingbox);
  15. var map_geojson = JSON.parse(this.dataset.mapGeojson);
  16. searxng.loadStyle('css/leaflet.css');
  17. searxng.loadScript('js/leaflet.js', function () {
  18. var map_bounds = null;
  19. if (map_boundingbox) {
  20. var southWest = L.latLng(map_boundingbox[0], map_boundingbox[2]);
  21. var northEast = L.latLng(map_boundingbox[1], map_boundingbox[3]);
  22. map_bounds = L.latLngBounds(southWest, northEast);
  23. }
  24. // init map
  25. var map = L.map(leaflet_target);
  26. // create the tile layer with correct attribution
  27. var osmMapnikUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  28. var osmMapnikAttrib = 'Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  29. var osmMapnik = new L.TileLayer(osmMapnikUrl, {minZoom: 1, maxZoom: 19, attribution: osmMapnikAttrib});
  30. var osmWikimediaUrl = 'https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png';
  31. var osmWikimediaAttrib = 'Wikimedia maps | Maps data © <a href="https://openstreetmap.org">OpenStreetMap contributors</a>';
  32. var osmWikimedia = new L.TileLayer(osmWikimediaUrl, {minZoom: 1, maxZoom: 19, attribution: osmWikimediaAttrib});
  33. // init map view
  34. if (map_bounds) {
  35. // TODO hack: https://github.com/Leaflet/Leaflet/issues/2021
  36. // Still useful ?
  37. setTimeout(function () {
  38. map.fitBounds(map_bounds, {
  39. maxZoom: 17
  40. });
  41. }, 0);
  42. } else if (map_lon && map_lat) {
  43. if (map_zoom) {
  44. map.setView(new L.latLng(map_lat, map_lon), map_zoom);
  45. } else {
  46. map.setView(new L.latLng(map_lat, map_lon), 8);
  47. }
  48. }
  49. map.addLayer(osmMapnik);
  50. var baseLayers = {
  51. "OSM Mapnik": osmMapnik,
  52. "OSM Wikimedia": osmWikimedia,
  53. };
  54. L.control.layers(baseLayers).addTo(map);
  55. if (map_geojson) {
  56. L.geoJson(map_geojson).addTo(map);
  57. } /* else if(map_bounds) {
  58. L.rectangle(map_bounds, {color: "#ff7800", weight: 3, fill:false}).addTo(map);
  59. } */
  60. });
  61. // this event occour only once per element
  62. event.preventDefault();
  63. });
  64. });
  65. })(window, document, window.searxng);