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 html_mouse = 'mouse position ' + (mouse_lat_lng ? [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',') : '-');
14 html_click = 'last click: ' + (last_click_latlng ? [last_click_latlng.lat.toFixed(5), last_click_latlng.lng.toFixed(5)].join(',') : '-');
16 html_center = 'map center: '
17 + map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5)
18 + ' <a target="_blank" href="' + map_link_to_osm() + '">view on osm.org</a>';
20 html_zoom = 'map zoom: ' + map.getZoom();
22 html_viewbox = 'viewbox: ' + map_viewbox_as_string();
24 $('#map-position-inner').html([html_center, html_zoom, html_viewbox, html_click, html_mouse].join('<br/>'));
26 var center_lat_lng = map.wrapLatLng(map.getCenter());
27 var reverse_params = {
28 lat: center_lat_lng.lat.toFixed(5),
29 lon: center_lat_lng.lng.toFixed(5)
33 $('#switch-to-reverse').attr('href', 'reverse.html?' + $.param(reverse_params));
35 $('input#use_viewbox').trigger('change');
38 function init_map_on_search_page(is_reverse_search, nominatim_results, request_lat, request_lon, init_zoom) {
40 map = new L.map('map', {
41 // center: [nominatim_map_init.lat, nominatim_map_init.lon],
42 // zoom: nominatim_map_init.zoom,
43 attributionControl: (get_config_value('Map_Tile_Attribution') && get_config_value('Map_Tile_Attribution').length),
44 scrollWheelZoom: true, // !L.Browser.touch,
49 L.tileLayer(get_config_value('Map_Tile_URL'), {
51 attribution: (get_config_value('Map_Tile_Attribution') || null ) // '© <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
54 // console.log(Nominatim_Config);
56 map.setView([request_lat, request_lon], init_zoom);
58 var osm2 = new L.TileLayer(get_config_value('Map_Tile_URL'), { minZoom: 0, maxZoom: 13, attribution: (get_config_value('Map_Tile_Attribution') || null ) });
59 new L.Control.MiniMap(osm2, { toggleDisplay: true }).addTo(map);
61 if (is_reverse_search) {
62 // We don't need a marker, but an L.circle instance changes radius once you zoom in/out
63 var cm = L.circleMarker(
64 [request_lat, request_lon],
78 var MapPositionControl = L.Control.extend({
82 onAdd: function (/* map */) {
83 var container = L.DomUtil.create('div', 'my-custom-control');
85 $(container).text('show map bounds').addClass('leaflet-bar btn btn-sm btn-default').on('click', function (e) {
88 $('#map-position').show();
91 $('#map-position-close a').on('click', function (e) {
94 $('#map-position').hide();
102 map.addControl(new MapPositionControl());
108 function update_viewbox_field() {
110 $('input[name=viewbox]').val($('input#use_viewbox').prop('checked') ? map_viewbox_as_string() : '');
113 map.on('move', function () {
114 display_map_position();
115 update_viewbox_field();
118 map.on('mousemove', function (e) {
119 display_map_position(e.latlng);
122 map.on('click', function (e) {
123 last_click_latlng = e.latlng;
124 display_map_position();
127 map.on('load', function () {
128 display_map_position();
131 $('input#use_viewbox').on('change', function () {
132 update_viewbox_field();
138 function get_result_element(position) {
139 return $('.result').eq(position);
141 function marker_for_result(result) {
142 return L.marker([result.lat, result.lon], { riseOnHover: true, title: result.name });
144 function circle_for_result(result) {
148 fillColor: '#ff7800',
151 clickable: !is_reverse_search
153 return L.circleMarker([result.lat, result.lon], cm_style);
156 var layerGroup = new L.layerGroup().addTo(map);
158 function highlight_result(position, bool_focus) {
159 var result = nominatim_results[position];
160 if (!result) { return; }
161 var result_el = get_result_element(position);
163 $('.result').removeClass('highlight');
164 result_el.addClass('highlight');
166 layerGroup.clearLayers();
169 var circle = circle_for_result(result);
170 circle.on('click', function () {
171 highlight_result(position);
173 layerGroup.addLayer(circle);
176 if (result.boundingbox) {
178 [result.boundingbox[0] * 1, result.boundingbox[2] * 1],
179 [result.boundingbox[1] * 1, result.boundingbox[3] * 1]
181 map.fitBounds(bounds);
183 if (result.geojson && result.geojson.type.match(/(Polygon)|(Line)/)) {
185 var geojson_layer = L.geoJson(
186 parse_and_normalize_geojson_string(result.geojson),
188 // https://leafletjs.com/reference-1.0.3.html#path-option
189 style: function (feature) {
190 return { interactive: false, color: 'blue' };
194 layerGroup.addLayer(geojson_layer);
197 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
198 // layerGroup.addLayer(layer);
201 var result_coord = L.latLng(result.lat, result.lon);
203 if (is_reverse_search) {
204 // console.dir([result_coord, [request_lat, request_lon]]);
205 // make sure the search coordinates are in the map view as well
207 [result_coord, [request_lat, request_lon]],
210 maxZoom: map.getZoom()
214 map.panTo(result_coord, result.zoom || get_config_value('Map_Default_Zoom'));
224 $('.result').on('click', function () {
225 highlight_result($(this).data('position'), true);
228 if (is_reverse_search) {
229 map.on('click', function (e) {
230 $('form input[name=lat]').val(e.latlng.lat);
231 $('form input[name=lon]').val(e.latlng.wrap().lng);
235 $('#switch-coords').on('click', function (e) {
238 var lat = $('form input[name=lat]').val();
239 var lon = $('form input[name=lon]').val();
240 $('form input[name=lat]').val(lon);
241 $('form input[name=lon]').val(lat);
246 highlight_result(0, false);
248 // common mistake is to copy&paste latitude and longitude into the 'lat' search box
249 $('form input[name=lat]').on('change', function () {
250 var coords = $(this).val().split(',');
251 if (coords.length === 2) {
252 $(this).val(L.Util.trim(coords[0]));
253 $(this).siblings('input[name=lon]').val(L.Util.trim(coords[1]));
264 jQuery(document).ready(function () {
266 if (!$('#search-page,#reverse-page').length) { return; }
268 var is_reverse_search = !!($('#reverse-page').length);
269 var endpoint = is_reverse_search ? 'reverse' : 'search';
272 var search_params = new URLSearchParams(location.search);
274 // return view('search', [
275 // 'sQuery' => $sQuery,
278 // 'aSearchResults' => $aSearchResults,
279 // 'sMoreURL' => 'example.com',
280 // 'sDataDate' => $this->fetch_status_date(),
284 if (is_reverse_search) {
285 var api_request_params = {
286 lat: search_params.get('lat'),
287 lon: search_params.get('lon'),
288 zoom: (search_params.get('zoom') > 1 ? search_params.get('zoom') : get_config_value('Reverse_Default_Search_Zoom')),
294 fLat: api_request_params.lat,
295 fLon: api_request_params.lon,
296 iZoom: (search_params.get('zoom') > 1 ? api_request_params.zoom : get_config_value('Reverse_Default_Search_Zoom'))
300 if (api_request_params.lat && api_request_params.lon) {
302 fetch_from_api('reverse', api_request_params, function (aPlace) {
308 context.aPlace = aPlace;
310 render_template($('main'), 'reversepage-template', context);
312 init_map_on_search_page(
315 api_request_params.lat,
316 api_request_params.lon,
317 api_request_params.zoom
323 render_template($('main'), 'reversepage-template', context);
325 init_map_on_search_page(
328 get_config_value('Map_Default_Lat'),
329 get_config_value('Map_Default_Lon'),
330 get_config_value('Map_Default_Zoom')
335 var api_request_params = {
336 q: search_params.get('q'),
337 polygon_geojson: search_params.get('polygon_geojson') ? 1 : 0,
338 viewbox: search_params.get('viewbox'),
343 // aSearchResults: aResults,
344 sQuery: api_request_params.q,
346 env: Nominatim_Config,
350 if (api_request_params.q) {
352 fetch_from_api('search', api_request_params, function (aResults) {
354 context.aSearchResults = aResults;
356 render_template($('main'), 'searchpage-template', context);
358 init_map_on_search_page(is_reverse_search, aResults, get_config_value('Map_Default_Lat'), get_config_value('Map_Default_Lon'), get_config_value('Map_Default_Zoom'));
365 render_template($('main'), 'searchpage-template', context);
367 init_map_on_search_page(is_reverse_search, [], get_config_value('Map_Default_Lat'), get_config_value('Map_Default_Lon'), get_config_value('Map_Default_Zoom'));