leaflet_map.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/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 osmMapquestUrl='http://otile{s}.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.jpg';
  119. var osmMapquestAttrib='Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors | Tiles Courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png">';
  120. var osmMapquest = new L.TileLayer(osmMapquestUrl, {minZoom: 1, maxZoom: 18, subdomains: '1234', attribution: osmMapquestAttrib});
  121. var osmMapquestOpenAerialUrl='http://otile{s}.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg';
  122. var osmMapquestOpenAerialAttrib='Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors | Tiles Courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="https://developer.mapquest.com/content/osm/mq_logo.png"> | Portions Courtesy NASA/JPL-Caltech and U.S. Depart. of Agriculture, Farm Service Agency';
  123. var osmMapquestOpenAerial = new L.TileLayer(osmMapquestOpenAerialUrl, {minZoom: 1, maxZoom: 11, subdomains: '1234', attribution: osmMapquestOpenAerialAttrib});
  124. // init map view
  125. if(map_bounds) {
  126. // TODO hack: https://github.com/Leaflet/Leaflet/issues/2021
  127. setTimeout(function () {
  128. map.fitBounds(map_bounds, {
  129. maxZoom:17
  130. });
  131. }, 0);
  132. } else if (map_lon && map_lat) {
  133. if(map_zoom)
  134. map.setView(new L.LatLng(map_lat, map_lon),map_zoom);
  135. else
  136. map.setView(new L.LatLng(map_lat, map_lon),8);
  137. }
  138. map.addLayer(osmMapquest);
  139. var baseLayers = {
  140. "OSM Mapnik": osmMapnik,
  141. "MapQuest": osmMapquest/*,
  142. "MapQuest Open Aerial": osmMapquestOpenAerial*/
  143. };
  144. L.control.layers(baseLayers).addTo(map);
  145. if(map_geojson)
  146. L.geoJson(map_geojson).addTo(map);
  147. /*else if(map_bounds)
  148. L.rectangle(map_bounds, {color: "#ff7800", weight: 3, fill:false}).addTo(map);*/
  149. });
  150. // this event occour only once per element
  151. $( this ).off( event );
  152. });
  153. });