leaflet_map.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * @license
  3. * (C) Copyright Contributors to the SearXNG project.
  4. * (C) Copyright Contributors to the searx project (2014 - 2021).
  5. * (C) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
  6. * SPDX-License-Identifier: AGPL-3.0-or-later
  7. */
  8. $(document).ready(function(){
  9. $(".searxng_init_map").on( "click", function( event ) {
  10. var leaflet_target = $(this).data('leaflet-target');
  11. var map_lon = $(this).data('map-lon');
  12. var map_lat = $(this).data('map-lat');
  13. var map_zoom = $(this).data('map-zoom');
  14. var map_boundingbox = $(this).data('map-boundingbox');
  15. var map_geojson = $(this).data('map-geojson');
  16. if(map_boundingbox) {
  17. southWest = L.latLng(map_boundingbox[0], map_boundingbox[2]);
  18. northEast = L.latLng(map_boundingbox[1], map_boundingbox[3]);
  19. map_bounds = L.latLngBounds(southWest, northEast);
  20. }
  21. // change default imagePath
  22. L.Icon.Default.imagePath = "./static/themes/oscar/css/images/";
  23. // init map
  24. var map = L.map(leaflet_target);
  25. // create the tile layer with correct attribution
  26. var osmMapnikUrl='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  27. var osmMapnikAttrib='Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  28. var osmMapnik = new L.TileLayer(osmMapnikUrl, {minZoom: 1, maxZoom: 19, attribution: osmMapnikAttrib});
  29. var osmWikimediaUrl='https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png';
  30. var osmWikimediaAttrib = 'Wikimedia maps beta | Maps data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  31. var osmWikimedia = new L.TileLayer(osmWikimediaUrl, {minZoom: 1, maxZoom: 19, attribution: osmWikimediaAttrib});
  32. // init map view
  33. setTimeout(function() {
  34. if(map_bounds) {
  35. map.fitBounds(map_bounds, {
  36. maxZoom:17
  37. });
  38. } else if (map_lon && map_lat) {
  39. if(map_zoom)
  40. map.setView(new L.LatLng(map_lat, map_lon),map_zoom);
  41. else
  42. map.setView(new L.LatLng(map_lat, map_lon),8);
  43. }
  44. }, 0);
  45. map.addLayer(osmMapnik);
  46. var baseLayers = {
  47. "OSM Mapnik": osmMapnik/*,
  48. "OSM Wikimedia": osmWikimedia*/
  49. };
  50. L.control.layers(baseLayers).addTo(map);
  51. if(map_geojson)
  52. L.geoJson(map_geojson).addTo(map);
  53. /*else if(map_bounds)
  54. L.rectangle(map_bounds, {color: "#ff7800", weight: 3, fill:false}).addTo(map);*/
  55. // this event occour only once per element
  56. $( this ).off( event );
  57. });
  58. });