searx_mapresult.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * searx is free software: you can redistribute it and/or modify
  3. * it under the terms of the GNU Affero General Public License as published by
  4. * the Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * searx is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU Affero General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU Affero General Public License
  13. * along with searx. If not, see < http://www.gnu.org/licenses/ >.
  14. *
  15. * (C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  16. * (C) 2017 by Alexandre Flament, <alex@al-f.net>
  17. */
  18. (function (w, d, searx) {
  19. 'use strict';
  20. searx.ready(function () {
  21. searx.on('.searx_init_map', 'click', function(event) {
  22. // no more request
  23. this.classList.remove("searx_init_map");
  24. //
  25. var leaflet_target = this.dataset.leafletTarget;
  26. var map_lon = parseFloat(this.dataset.mapLon);
  27. var map_lat = parseFloat(this.dataset.mapLat);
  28. var map_zoom = parseFloat(this.dataset.mapZoom);
  29. var map_boundingbox = JSON.parse(this.dataset.mapBoundingbox);
  30. var map_geojson = JSON.parse(this.dataset.mapGeojson);
  31. searx.loadStyle('leaflet/leaflet.css');
  32. searx.loadScript('leaflet/leaflet.js', function() {
  33. var map_bounds = null;
  34. if(map_boundingbox) {
  35. var southWest = L.latLng(map_boundingbox[0], map_boundingbox[2]);
  36. var northEast = L.latLng(map_boundingbox[1], map_boundingbox[3]);
  37. map_bounds = L.latLngBounds(southWest, northEast);
  38. }
  39. // init map
  40. var map = L.map(leaflet_target);
  41. // create the tile layer with correct attribution
  42. var osmMapnikUrl='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  43. var osmMapnikAttrib='Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  44. var osmMapnik = new L.TileLayer(osmMapnikUrl, {minZoom: 1, maxZoom: 19, attribution: osmMapnikAttrib});
  45. var osmWikimediaUrl='https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png';
  46. var osmWikimediaAttrib = 'Wikimedia maps beta | Maps data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  47. var osmWikimedia = new L.TileLayer(osmWikimediaUrl, {minZoom: 1, maxZoom: 19, attribution: osmWikimediaAttrib});
  48. // init map view
  49. if(map_bounds) {
  50. // TODO hack: https://github.com/Leaflet/Leaflet/issues/2021
  51. // Still useful ?
  52. setTimeout(function () {
  53. map.fitBounds(map_bounds, {
  54. maxZoom:17
  55. });
  56. }, 0);
  57. } else if (map_lon && map_lat) {
  58. if(map_zoom) {
  59. map.setView(new L.latLng(map_lat, map_lon),map_zoom);
  60. } else {
  61. map.setView(new L.latLng(map_lat, map_lon),8);
  62. }
  63. }
  64. map.addLayer(osmMapnik);
  65. var baseLayers = {
  66. "OSM Mapnik": osmMapnik/*,
  67. "OSM Wikimedia": osmWikimedia*/
  68. };
  69. L.control.layers(baseLayers).addTo(map);
  70. if(map_geojson) {
  71. L.geoJson(map_geojson).addTo(map);
  72. } /*else if(map_bounds) {
  73. L.rectangle(map_bounds, {color: "#ff7800", weight: 3, fill:false}).addTo(map);
  74. }*/
  75. });
  76. // this event occour only once per element
  77. event.preventDefault();
  78. });
  79. });
  80. })(window, document, window.searx);