logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

leaflet_map.js (8040B)


  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. */
  17. $(document).ready(function(){
  18. $(".searx_overpass_request").on( "click", function( event ) {
  19. var overpass_url = "https://overpass-api.de/api/interpreter?data=";
  20. var query_start = overpass_url + "[out:json][timeout:25];(";
  21. var query_end = ");out meta;";
  22. var osm_id = $(this).data('osm-id');
  23. var osm_type = $(this).data('osm-type');
  24. var result_table = $(this).data('result-table');
  25. var result_table_loadicon = "#" + $(this).data('result-table-loadicon');
  26. // tags which can be ignored
  27. var osm_ignore_tags = [ "addr:city", "addr:country", "addr:housenumber", "addr:postcode", "addr:street" ];
  28. if(osm_id && osm_type && result_table) {
  29. result_table = "#" + result_table;
  30. var query = null;
  31. switch(osm_type) {
  32. case 'node':
  33. query = query_start + "node(" + osm_id + ");" + query_end;
  34. break;
  35. case 'way':
  36. query = query_start + "way(" + osm_id + ");" + query_end;
  37. break;
  38. case 'relation':
  39. query = query_start + "relation(" + osm_id + ");" + query_end;
  40. break;
  41. default:
  42. break;
  43. }
  44. if(query) {
  45. //alert(query);
  46. var ajaxRequest = $.ajax( query )
  47. .done(function( html) {
  48. if(html && html.elements && html.elements[0]) {
  49. var element = html.elements[0];
  50. var newHtml = $(result_table).html();
  51. for (var row in element.tags) {
  52. if(element.tags.name === null || osm_ignore_tags.indexOf(row) == -1) {
  53. newHtml += "<tr><td>" + row + "</td><td>";
  54. switch(row) {
  55. case "phone":
  56. case "fax":
  57. newHtml += "<a href=\"tel:" + element.tags[row].replace(/ /g,'') + "\">" + element.tags[row] + "</a>";
  58. break;
  59. case "email":
  60. newHtml += "<a href=\"mailto:" + element.tags[row] + "\">" + element.tags[row] + "</a>";
  61. break;
  62. case "website":
  63. case "url":
  64. newHtml += "<a href=\"" + element.tags[row] + "\">" + element.tags[row] + "</a>";
  65. break;
  66. case "wikidata":
  67. newHtml += "<a href=\"https://www.wikidata.org/wiki/" + element.tags[row] + "\">" + element.tags[row] + "</a>";
  68. break;
  69. case "wikipedia":
  70. if(element.tags[row].indexOf(":") != -1) {
  71. newHtml += "<a href=\"https://" + element.tags[row].substring(0,element.tags[row].indexOf(":")) + ".wikipedia.org/wiki/" + element.tags[row].substring(element.tags[row].indexOf(":")+1) + "\">" + element.tags[row] + "</a>";
  72. break;
  73. }
  74. /* jshint ignore:start */
  75. default:
  76. /* jshint ignore:end */
  77. newHtml += element.tags[row];
  78. break;
  79. }
  80. newHtml += "</td></tr>";
  81. }
  82. }
  83. $(result_table).html(newHtml);
  84. $(result_table).removeClass('hidden');
  85. $(result_table_loadicon).addClass('hidden');
  86. }
  87. })
  88. .fail(function() {
  89. $(result_table_loadicon).html($(result_table_loadicon).html() + "<p class=\"text-muted\">could not load data!</p>");
  90. });
  91. }
  92. }
  93. // this event occour only once per element
  94. $( this ).off( event );
  95. });
  96. $(".searx_init_map").on( "click", function( event ) {
  97. var leaflet_target = $(this).data('leaflet-target');
  98. var map_lon = $(this).data('map-lon');
  99. var map_lat = $(this).data('map-lat');
  100. var map_zoom = $(this).data('map-zoom');
  101. var map_boundingbox = $(this).data('map-boundingbox');
  102. var map_geojson = $(this).data('map-geojson');
  103. require(['leaflet-0.7.3.min'], function(leaflet) {
  104. if(map_boundingbox) {
  105. southWest = L.latLng(map_boundingbox[0], map_boundingbox[2]);
  106. northEast = L.latLng(map_boundingbox[1], map_boundingbox[3]);
  107. map_bounds = L.latLngBounds(southWest, northEast);
  108. }
  109. // TODO hack
  110. // change default imagePath
  111. L.Icon.Default.imagePath = "./static/themes/oscar/img/map";
  112. // init map
  113. var map = L.map(leaflet_target);
  114. // create the tile layer with correct attribution
  115. var osmMapnikUrl='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  116. var osmMapnikAttrib='Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  117. var osmMapnik = new L.TileLayer(osmMapnikUrl, {minZoom: 1, maxZoom: 19, attribution: osmMapnikAttrib});
  118. var osmWikimediaUrl='https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png';
  119. var osmWikimediaAttrib = 'Wikimedia maps beta | Maps data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  120. var osmWikimedia = new L.TileLayer(osmWikimediaUrl, {minZoom: 1, maxZoom: 19, attribution: osmWikimediaAttrib});
  121. // init map view
  122. if(map_bounds) {
  123. // TODO hack: https://github.com/Leaflet/Leaflet/issues/2021
  124. setTimeout(function () {
  125. map.fitBounds(map_bounds, {
  126. maxZoom:17
  127. });
  128. }, 0);
  129. } else if (map_lon && map_lat) {
  130. if(map_zoom)
  131. map.setView(new L.LatLng(map_lat, map_lon),map_zoom);
  132. else
  133. map.setView(new L.LatLng(map_lat, map_lon),8);
  134. }
  135. map.addLayer(osmMapnik);
  136. var baseLayers = {
  137. "OSM Mapnik": osmMapnik/*,
  138. "OSM Wikimedia": osmWikimedia*/
  139. };
  140. L.control.layers(baseLayers).addTo(map);
  141. if(map_geojson)
  142. L.geoJson(map_geojson).addTo(map);
  143. /*else if(map_bounds)
  144. L.rectangle(map_bounds, {color: "#ff7800", weight: 3, fill:false}).addTo(map);*/
  145. });
  146. // this event occour only once per element
  147. $( this ).off( event );
  148. });
  149. });