2 // *********************************************************
3 // FORWARD/REVERSE SEARCH PAGE
4 // *********************************************************
7 function display_map_position(mouse_lat_lng) {
10 mouse_lat_lng = map.wrapLatLng(mouse_lat_lng);
13 var html_mouse = 'mouse position: -';
15 html_mouse = 'mouse position: '
16 + [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',');
18 var html_click = 'last click: -';
19 if (last_click_latlng) {
20 html_click = 'last click: '
21 + [last_click_latlng.lat.toFixed(5), last_click_latlng.lng.toFixed(5)].join(',');
24 var html_center = 'map center: '
25 + map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5)
26 + ' <a target="_blank" href="' + map_link_to_osm() + '">view on osm.org</a>';
28 var html_zoom = 'map zoom: ' + map.getZoom();
29 var html_viewbox = 'viewbox: ' + map_viewbox_as_string();
31 $('#map-position-inner').html([
39 var center_lat_lng = map.wrapLatLng(map.getCenter());
40 var reverse_params = {
41 lat: center_lat_lng.lat.toFixed(5),
42 lon: center_lat_lng.lng.toFixed(5)
46 $('#switch-to-reverse').attr('href', 'reverse.html?' + $.param(reverse_params));
48 $('input#use_viewbox').trigger('change');
51 function init_map_on_search_page(is_reverse_search, nominatim_results, request_lat,
52 request_lon, init_zoom) {
54 var attribution = get_config_value('Map_Tile_Attribution') || null;
55 map = new L.map('map', {
56 // center: [nominatim_map_init.lat, nominatim_map_init.lon],
57 // zoom: nominatim_map_init.zoom,
58 attributionControl: (attribution && attribution.length),
59 scrollWheelZoom: true, // !L.Browser.touch,
64 L.tileLayer(get_config_value('Map_Tile_URL'), {
66 // '© <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
67 attribution: attribution
70 // console.log(Nominatim_Config);
72 map.setView([request_lat, request_lon], init_zoom);
74 var osm2 = new L.TileLayer(get_config_value('Map_Tile_URL'), {
77 attribution: attribution
79 new L.Control.MiniMap(osm2, { toggleDisplay: true }).addTo(map);
81 if (is_reverse_search) {
82 // We don't need a marker, but an L.circle instance changes radius once you zoom in/out
83 var cm = L.circleMarker(
84 [request_lat, request_lon],
97 var search_params = new URLSearchParams(window.location.search);
98 var viewbox = search_params.get('viewbox');
100 var coords = viewbox.split(','); // <x1>,<y1>,<x2>,<y2>
101 var bounds = L.latLngBounds([coords[1], coords[0]], [coords[3], coords[2]]);
102 L.rectangle(bounds, {
112 var MapPositionControl = L.Control.extend({
116 onAdd: function (/* map */) {
117 var container = L.DomUtil.create('div', 'my-custom-control');
119 $(container).text('show map bounds')
120 .addClass('leaflet-bar btn btn-sm btn-outline-secondary')
121 .on('click', function (e) {
124 $('#map-position').show();
127 $('#map-position-close a').on('click', function (e) {
130 $('#map-position').hide();
138 map.addControl(new MapPositionControl());
144 function update_viewbox_field() {
146 $('input[name=viewbox]')
147 .val($('input#use_viewbox')
148 .prop('checked') ? map_viewbox_as_string() : '');
151 map.on('move', function () {
152 display_map_position();
153 update_viewbox_field();
156 map.on('mousemove', function (e) {
157 display_map_position(e.latlng);
160 map.on('click', function (e) {
161 last_click_latlng = e.latlng;
162 display_map_position();
165 map.on('load', function () {
166 display_map_position();
169 $('input#use_viewbox').on('change', function () {
170 update_viewbox_field();
173 function get_result_element(position) {
174 return $('.result').eq(position);
176 // function marker_for_result(result) {
177 // return L.marker([result.lat, result.lon], { riseOnHover: true, title: result.name });
179 function circle_for_result(result) {
183 fillColor: '#ff7800',
186 clickable: !is_reverse_search
188 return L.circleMarker([result.lat, result.lon], cm_style);
191 var layerGroup = (new L.layerGroup()).addTo(map);
193 function highlight_result(position, bool_focus) {
194 var result = nominatim_results[position];
195 if (!result) { return; }
196 var result_el = get_result_element(position);
198 $('.result').removeClass('highlight');
199 result_el.addClass('highlight');
201 layerGroup.clearLayers();
204 var circle = circle_for_result(result);
205 circle.on('click', function () {
206 highlight_result(position);
208 layerGroup.addLayer(circle);
211 if (result.boundingbox) {
213 [result.boundingbox[0] * 1, result.boundingbox[2] * 1],
214 [result.boundingbox[1] * 1, result.boundingbox[3] * 1]
218 if (result.geojson && result.geojson.type.match(/(Polygon)|(Line)/)) {
220 var geojson_layer = L.geoJson(
221 parse_and_normalize_geojson_string(result.geojson),
223 // https://leafletjs.com/reference-1.0.3.html#path-option
224 style: function (/* feature */) {
225 return { interactive: false, color: 'blue' };
229 layerGroup.addLayer(geojson_layer);
232 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
233 // layerGroup.addLayer(layer);
236 var result_coord = L.latLng(result.lat, result.lon);
238 if (is_reverse_search) {
239 // console.dir([result_coord, [request_lat, request_lon]]);
240 // make sure the search coordinates are in the map view as well
242 [result_coord, [request_lat, request_lon]],
245 maxZoom: map.getZoom()
249 map.panTo(result_coord, result.zoom || get_config_value('Map_Default_Zoom'));
259 $('.result').on('click', function () {
260 highlight_result($(this).data('position'), true);
263 if (is_reverse_search) {
264 map.on('click', function (e) {
265 $('form input[name=lat]').val(e.latlng.lat);
266 $('form input[name=lon]').val(e.latlng.wrap().lng);
270 $('#switch-coords').on('click', function (e) {
273 var lat = $('form input[name=lat]').val();
274 var lon = $('form input[name=lon]').val();
275 $('form input[name=lat]').val(lon);
276 $('form input[name=lon]').val(lat);
281 highlight_result(0, false);
283 // common mistake is to copy&paste latitude and longitude into the 'lat' search box
284 $('form input[name=lat]').on('change', function () {
285 var coords_split = $(this).val().split(',');
286 if (coords_split.length === 2) {
287 $(this).val(L.Util.trim(coords_split[0]));
288 $(this).siblings('input[name=lon]').val(L.Util.trim(coords_split[1]));
296 jQuery(document).ready(function () {
298 if (!$('#search-page,#reverse-page').length) { return; }
300 var is_reverse_search = !!($('#reverse-page').length);
302 var search_params = new URLSearchParams(window.location.search);
304 // return view('search', [
305 // 'sQuery' => $sQuery,
308 // 'aSearchResults' => $aSearchResults,
309 // 'sMoreURL' => 'example.com',
310 // 'sDataDate' => $this->fetch_status_date(),
314 var api_request_params;
317 if (is_reverse_search) {
318 api_request_params = {
319 lat: search_params.get('lat'),
320 lon: search_params.get('lon'),
321 zoom: (search_params.get('zoom') > 1
322 ? search_params.get('zoom')
323 : get_config_value('Reverse_Default_Search_Zoom')),
329 fLat: api_request_params.lat,
330 fLon: api_request_params.lon,
331 iZoom: (search_params.get('zoom') > 1
332 ? api_request_params.zoom
333 : get_config_value('Reverse_Default_Search_Zoom'))
337 if (api_request_params.lat && api_request_params.lon) {
339 fetch_from_api('reverse', api_request_params, function (aPlace) {
345 context.aPlace = aPlace;
347 render_template($('main'), 'reversepage-template', context);
348 update_html_title('Reverse result for '
349 + api_request_params.lat
351 + api_request_params.lon);
353 init_map_on_search_page(
356 api_request_params.lat,
357 api_request_params.lon,
358 api_request_params.zoom
364 render_template($('main'), 'reversepage-template', context);
366 init_map_on_search_page(
369 get_config_value('Map_Default_Lat'),
370 get_config_value('Map_Default_Lon'),
371 get_config_value('Map_Default_Zoom')
376 api_request_params = {
377 q: search_params.get('q'),
378 street: search_params.get('street'),
379 city: search_params.get('city'),
380 county: search_params.get('county'),
381 state: search_params.get('state'),
382 country: search_params.get('country'),
383 postalcode: search_params.get('postalcode'),
384 polygon_geojson: get_config_value('Search_AreaPolygons', false) ? 1 : 0,
385 viewbox: search_params.get('viewbox'),
386 exclude_place_ids: search_params.get('exclude_place_ids'),
391 sQuery: api_request_params.q,
392 sViewBox: search_params.get('viewbox'),
396 if (api_request_params.street || api_request_params.city || api_request_params.county
397 || api_request_params.state || api_request_params.country || api_request_params.postalcode) {
398 context.hStructured = {
399 street: api_request_params.street,
400 city: api_request_params.city,
401 county: api_request_params.county,
402 state: api_request_params.state,
403 country: api_request_params.country,
404 postalcode: api_request_params.postalcode
408 if (api_request_params.q || context.hStructured) {
410 fetch_from_api('search', api_request_params, function (aResults) {
412 context.aSearchResults = aResults;
414 if (aResults.length >= 10) {
415 var aExcludePlaceIds = [];
416 if (search_params.has('exclude_place_ids')) {
417 aExcludePlaceIds.search_params.get('exclude_place_ids').split(',');
419 for (var i = 0; i < aResults.length; i += 1) {
420 aExcludePlaceIds.push(aResults[i].place_id);
423 var parsed_url = new URLSearchParams(window.location.search);
424 parsed_url.set('exclude_place_ids', aExcludePlaceIds.join(','));
425 context.sMoreURL = '?' + parsed_url.toString();
428 render_template($('main'), 'searchpage-template', context);
429 update_html_title('Result for ' + api_request_params.q);
431 init_map_on_search_page(
434 get_config_value('Map_Default_Lat'),
435 get_config_value('Map_Default_Lon'),
436 get_config_value('Map_Default_Zoom')
444 render_template($('main'), 'searchpage-template', context);
446 init_map_on_search_page(
449 get_config_value('Map_Default_Lat'),
450 get_config_value('Map_Default_Lon'),
451 get_config_value('Map_Default_Zoom')