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: ' + [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',');
17 var html_click = 'last click: -';
18 if (last_click_latlng) {
19 html_click = 'last click: ' + [last_click_latlng.lat.toFixed(5), last_click_latlng.lng.toFixed(5)].join(',');
22 var html_center = 'map center: '
23 + map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5)
24 + ' <a target="_blank" href="' + map_link_to_osm() + '">view on osm.org</a>';
26 var html_zoom = 'map zoom: ' + map.getZoom();
27 var html_viewbox = 'viewbox: ' + map_viewbox_as_string();
29 $('#map-position-inner').html([html_center, html_zoom, html_viewbox, html_click, html_mouse].join('<br/>'));
31 var center_lat_lng = map.wrapLatLng(map.getCenter());
32 var reverse_params = {
33 lat: center_lat_lng.lat.toFixed(5),
34 lon: center_lat_lng.lng.toFixed(5)
38 $('#switch-to-reverse').attr('href', 'reverse.html?' + $.param(reverse_params));
40 $('input#use_viewbox').trigger('change');
43 function init_map_on_search_page(is_reverse_search, nominatim_results, request_lat, request_lon, init_zoom) {
45 map = new L.map('map', {
46 // center: [nominatim_map_init.lat, nominatim_map_init.lon],
47 // zoom: nominatim_map_init.zoom,
48 attributionControl: (get_config_value('Map_Tile_Attribution') && get_config_value('Map_Tile_Attribution').length),
49 scrollWheelZoom: true, // !L.Browser.touch,
54 L.tileLayer(get_config_value('Map_Tile_URL'), {
56 attribution: (get_config_value('Map_Tile_Attribution') || null) // '© <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
59 // console.log(Nominatim_Config);
61 map.setView([request_lat, request_lon], init_zoom);
63 var osm2 = new L.TileLayer(get_config_value('Map_Tile_URL'), {
66 attribution: (get_config_value('Map_Tile_Attribution') || null)
68 new L.Control.MiniMap(osm2, { toggleDisplay: true }).addTo(map);
70 if (is_reverse_search) {
71 // We don't need a marker, but an L.circle instance changes radius once you zoom in/out
72 var cm = L.circleMarker(
73 [request_lat, request_lon],
86 var search_params = new URLSearchParams(window.location.search);
87 var viewbox = search_params.get('viewbox');
89 var coords = viewbox.split(','); // <x1>,<y1>,<x2>,<y2>
90 var bounds = L.latLngBounds([coords[1], coords[0]], [coords[3], coords[2]]);
101 var MapPositionControl = L.Control.extend({
105 onAdd: function (/* map */) {
106 var container = L.DomUtil.create('div', 'my-custom-control');
108 $(container).text('show map bounds').addClass('leaflet-bar btn btn-sm btn-default').on('click', function (e) {
111 $('#map-position').show();
114 $('#map-position-close a').on('click', function (e) {
117 $('#map-position').hide();
125 map.addControl(new MapPositionControl());
131 function update_viewbox_field() {
133 $('input[name=viewbox]').val($('input#use_viewbox').prop('checked') ? map_viewbox_as_string() : '');
136 map.on('move', function () {
137 display_map_position();
138 update_viewbox_field();
141 map.on('mousemove', function (e) {
142 display_map_position(e.latlng);
145 map.on('click', function (e) {
146 last_click_latlng = e.latlng;
147 display_map_position();
150 map.on('load', function () {
151 display_map_position();
154 $('input#use_viewbox').on('change', function () {
155 update_viewbox_field();
161 function get_result_element(position) {
162 return $('.result').eq(position);
164 // function marker_for_result(result) {
165 // return L.marker([result.lat, result.lon], { riseOnHover: true, title: result.name });
167 function circle_for_result(result) {
171 fillColor: '#ff7800',
174 clickable: !is_reverse_search
176 return L.circleMarker([result.lat, result.lon], cm_style);
179 var layerGroup = (new L.layerGroup()).addTo(map);
181 function highlight_result(position, bool_focus) {
182 var result = nominatim_results[position];
183 if (!result) { return; }
184 var result_el = get_result_element(position);
186 $('.result').removeClass('highlight');
187 result_el.addClass('highlight');
189 layerGroup.clearLayers();
192 var circle = circle_for_result(result);
193 circle.on('click', function () {
194 highlight_result(position);
196 layerGroup.addLayer(circle);
199 if (result.boundingbox) {
201 [result.boundingbox[0] * 1, result.boundingbox[2] * 1],
202 [result.boundingbox[1] * 1, result.boundingbox[3] * 1]
206 if (result.geojson && result.geojson.type.match(/(Polygon)|(Line)/)) {
208 var geojson_layer = L.geoJson(
209 parse_and_normalize_geojson_string(result.geojson),
211 // https://leafletjs.com/reference-1.0.3.html#path-option
212 style: function (/* feature */) {
213 return { interactive: false, color: 'blue' };
217 layerGroup.addLayer(geojson_layer);
220 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
221 // layerGroup.addLayer(layer);
224 var result_coord = L.latLng(result.lat, result.lon);
226 if (is_reverse_search) {
227 // console.dir([result_coord, [request_lat, request_lon]]);
228 // make sure the search coordinates are in the map view as well
230 [result_coord, [request_lat, request_lon]],
233 maxZoom: map.getZoom()
237 map.panTo(result_coord, result.zoom || get_config_value('Map_Default_Zoom'));
247 $('.result').on('click', function () {
248 highlight_result($(this).data('position'), true);
251 if (is_reverse_search) {
252 map.on('click', function (e) {
253 $('form input[name=lat]').val(e.latlng.lat);
254 $('form input[name=lon]').val(e.latlng.wrap().lng);
258 $('#switch-coords').on('click', function (e) {
261 var lat = $('form input[name=lat]').val();
262 var lon = $('form input[name=lon]').val();
263 $('form input[name=lat]').val(lon);
264 $('form input[name=lon]').val(lat);
269 highlight_result(0, false);
271 // common mistake is to copy&paste latitude and longitude into the 'lat' search box
272 $('form input[name=lat]').on('change', function () {
273 var coords_split = $(this).val().split(',');
274 if (coords_split.length === 2) {
275 $(this).val(L.Util.trim(coords_split[0]));
276 $(this).siblings('input[name=lon]').val(L.Util.trim(coords_split[1]));
287 jQuery(document).ready(function () {
289 if (!$('#search-page,#reverse-page').length) { return; }
291 var is_reverse_search = !!($('#reverse-page').length);
293 var search_params = new URLSearchParams(window.location.search);
295 // return view('search', [
296 // 'sQuery' => $sQuery,
299 // 'aSearchResults' => $aSearchResults,
300 // 'sMoreURL' => 'example.com',
301 // 'sDataDate' => $this->fetch_status_date(),
305 var api_request_params;
308 if (is_reverse_search) {
309 api_request_params = {
310 lat: search_params.get('lat'),
311 lon: search_params.get('lon'),
312 zoom: (search_params.get('zoom') > 1 ? search_params.get('zoom') : get_config_value('Reverse_Default_Search_Zoom')),
318 fLat: api_request_params.lat,
319 fLon: api_request_params.lon,
320 iZoom: (search_params.get('zoom') > 1 ? api_request_params.zoom : get_config_value('Reverse_Default_Search_Zoom'))
324 if (api_request_params.lat && api_request_params.lon) {
326 fetch_from_api('reverse', api_request_params, function (aPlace) {
332 context.aPlace = aPlace;
334 render_template($('main'), 'reversepage-template', context);
335 update_html_title('Reverse result for ' + api_request_params.lat + ',' + api_request_params.lon);
337 init_map_on_search_page(
340 api_request_params.lat,
341 api_request_params.lon,
342 api_request_params.zoom
348 render_template($('main'), 'reversepage-template', context);
350 init_map_on_search_page(
353 get_config_value('Map_Default_Lat'),
354 get_config_value('Map_Default_Lon'),
355 get_config_value('Map_Default_Zoom')
360 api_request_params = {
361 q: search_params.get('q'),
362 polygon_geojson: search_params.get('polygon_geojson') ? 1 : 0,
363 viewbox: search_params.get('viewbox'),
368 // aSearchResults: aResults,
369 sQuery: api_request_params.q,
370 sViewBox: search_params.get('viewbox'),
371 env: Nominatim_Config,
375 if (api_request_params.q) {
377 fetch_from_api('search', api_request_params, function (aResults) {
379 context.aSearchResults = aResults;
381 render_template($('main'), 'searchpage-template', context);
382 update_html_title('Result for ' + api_request_params.q);
384 init_map_on_search_page(
387 get_config_value('Map_Default_Lat'),
388 get_config_value('Map_Default_Lon'),
389 get_config_value('Map_Default_Zoom')
397 render_template($('main'), 'searchpage-template', context);
399 init_map_on_search_page(
402 get_config_value('Map_Default_Lat'),
403 get_config_value('Map_Default_Lon'),
404 get_config_value('Map_Default_Zoom')