7 // *********************************************************
9 // *********************************************************
11 function get_config_value(str, default_val) {
12 return (typeof Nominatim_Config[str] !== 'undefined' ? Nominatim_Config[str] : default_val);
15 function parse_and_normalize_geojson_string(part) {
16 // normalize places the geometry into a featurecollection, similar to
17 // https://github.com/mapbox/geojson-normalize
18 var parsed_geojson = {
19 type: 'FeatureCollection',
28 return parsed_geojson;
31 function map_link_to_osm() {
32 var zoom = map.getZoom();
33 var lat = map.getCenter().lat;
34 var lng = map.getCenter().lng;
35 return 'https://openstreetmap.org/#map=' + zoom + '/' + lat + '/' + lng;
38 function map_viewbox_as_string() {
39 var bounds = map.getBounds();
40 var west = bounds.getWest();
41 var east = bounds.getEast();
43 if ((east - west) >= 360) { // covers more than whole planet
44 west = map.getCenter().lng - 179.999;
45 east = map.getCenter().lng + 179.999;
47 east = L.latLng(77, east).wrap().lng;
48 west = L.latLng(77, west).wrap().lng;
51 west.toFixed(5), // left
52 bounds.getNorth().toFixed(5), // top
53 east.toFixed(5), // right
54 bounds.getSouth().toFixed(5) // bottom
59 // *********************************************************
61 // *********************************************************
63 function fetch_from_api(endpoint_name, params, callback) {
65 // `&a=&b=&c=1` => '&c=1'
66 var param_names = Object.keys(params);
67 for (var i = 0; i < param_names.length; i += 1) {
68 var val = params[param_names[i]];
69 if (typeof (val) === 'undefined' || val === '' || val === null) {
70 delete params[param_names[i]];
74 var api_url = get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?'
76 if (endpoint_name !== 'status') {
77 $('#api-request-link').attr('href', api_url);
79 $.get(api_url, function (data) {
84 function update_data_date() {
85 fetch_from_api('status', { format: 'json' }, function (data) {
86 $('#data-date').text(data.data_updated);
90 function render_template(el, template_name, page_context) {
91 var template_source = $('#' + template_name).text();
92 var template = Handlebars.compile(template_source);
93 var html = template(page_context);
97 function update_html_title(title) {
99 if (title && title.length > 1) {
100 prefix = title + ' | ';
102 $('head title').text(prefix + 'OpenStreetMap Nominatim');
105 function show_error(html) {
106 $('#error-overlay').html(html).show();
109 function hide_error() {
110 $('#error-overlay').empty().hide();
114 $(document).ajaxError(function (event, jqXHR, ajaxSettings/* , thrownError */) {
115 // console.log(thrownError);
116 // console.log(ajaxSettings);
117 var url = ajaxSettings.url;
118 show_error('Error fetching results from <a href="' + url + '">' + url + '</a>');
122 jQuery(document).ready(function () {
125 // *********************************************************
127 // *********************************************************
130 function init_map_on_detail_page(lat, lon, geojson) {
131 var attribution = get_config_value('Map_Tile_Attribution') || null;
132 map = new L.map('map', {
133 // center: [nominatim_map_init.lat, nominatim_map_init.lon],
134 // zoom: nominatim_map_init.zoom,
135 attributionControl: (attribution && attribution.length),
136 scrollWheelZoom: true, // !L.Browser.touch,
140 L.tileLayer(get_config_value('Map_Tile_URL'), {
142 // '© <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
143 attribution: attribution
146 // var layerGroup = new L.layerGroup().addTo(map);
148 var circle = L.circleMarker([lat, lon], {
149 radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75
151 map.addLayer(circle);
154 var geojson_layer = L.geoJson(
155 // https://leafletjs.com/reference-1.0.3.html#path-option
156 parse_and_normalize_geojson_string(geojson),
159 return { interactive: false, color: 'blue' };
163 map.addLayer(geojson_layer);
164 map.fitBounds(geojson_layer.getBounds());
166 map.setView([lat, lon], 10);
169 var osm2 = new L.TileLayer(
170 get_config_value('Map_Tile_URL'),
174 attribution: (get_config_value('Map_Tile_Attribution') || null)
177 (new L.Control.MiniMap(osm2, { toggleDisplay: true })).addTo(map);
181 jQuery(document).ready(function () {
182 if (!$('#details-page').length) { return; }
184 var search_params = new URLSearchParams(window.location.search);
185 // var place_id = search_params.get('place_id');
187 var api_request_params = {
188 place_id: search_params.get('place_id'),
189 osmtype: search_params.get('osmtype'),
190 osmid: search_params.get('osmid'),
191 keywords: search_params.get('keywords'),
193 hierarchy: (search_params.get('hierarchy') === '1' ? 1 : 0),
199 if (api_request_params.place_id || (api_request_params.osmtype && api_request_params.osmid)) {
200 fetch_from_api('details', api_request_params, function (aFeature) {
201 var context = { aPlace: aFeature, base_url: window.location.search };
203 render_template($('main'), 'detailspage-template', context);
204 if (api_request_params.place_id) {
205 update_html_title('Details for ' + api_request_params.place_id);
207 update_html_title('Details for ' + api_request_params.osmtype + api_request_params.osmid);
212 var lat = aFeature.centroid.coordinates[1];
213 var lon = aFeature.centroid.coordinates[0];
214 init_map_on_detail_page(lat, lon, aFeature.geometry);
217 render_template($('main'), 'detailspage-index-template');
220 $('#form-by-type-and-id,#form-by-osm-url').on('submit', function (e) {
223 var val = $(this).find('input[type=edit]').val();
224 var matches = val.match(/^\s*([NWR])(\d+)\s*$/i);
227 matches = val.match(/\/(relation|way|node)\/(\d+)\s*$/);
231 $(this).find('input[name=osmtype]').val(matches[1].charAt(0).toUpperCase());
232 $(this).find('input[name=osmid]').val(matches[2]);
233 $(this).get(0).submit();
235 alert('invalid input');
240 // *********************************************************
241 // FORWARD/REVERSE SEARCH PAGE
242 // *********************************************************
245 function display_map_position(mouse_lat_lng) {
248 mouse_lat_lng = map.wrapLatLng(mouse_lat_lng);
251 var html_mouse = 'mouse position: -';
253 html_mouse = 'mouse position: '
254 + [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',');
256 var html_click = 'last click: -';
257 if (last_click_latlng) {
258 html_click = 'last click: '
259 + [last_click_latlng.lat.toFixed(5), last_click_latlng.lng.toFixed(5)].join(',');
262 var html_center = 'map center: '
263 + map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5)
264 + ' <a target="_blank" href="' + map_link_to_osm() + '">view on osm.org</a>';
266 var html_zoom = 'map zoom: ' + map.getZoom();
267 var html_viewbox = 'viewbox: ' + map_viewbox_as_string();
269 $('#map-position-inner').html([
277 var center_lat_lng = map.wrapLatLng(map.getCenter());
278 var reverse_params = {
279 lat: center_lat_lng.lat.toFixed(5),
280 lon: center_lat_lng.lng.toFixed(5)
284 $('#switch-to-reverse').attr('href', 'reverse.html?' + $.param(reverse_params));
286 $('input#use_viewbox').trigger('change');
289 function init_map_on_search_page(is_reverse_search, nominatim_results, request_lat,
290 request_lon, init_zoom) {
292 var attribution = get_config_value('Map_Tile_Attribution') || null;
293 map = new L.map('map', {
294 // center: [nominatim_map_init.lat, nominatim_map_init.lon],
295 // zoom: nominatim_map_init.zoom,
296 attributionControl: (attribution && attribution.length),
297 scrollWheelZoom: true, // !L.Browser.touch,
302 L.tileLayer(get_config_value('Map_Tile_URL'), {
304 // '© <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
305 attribution: attribution
308 // console.log(Nominatim_Config);
310 map.setView([request_lat, request_lon], init_zoom);
312 var osm2 = new L.TileLayer(get_config_value('Map_Tile_URL'), {
315 attribution: attribution
317 new L.Control.MiniMap(osm2, { toggleDisplay: true }).addTo(map);
319 if (is_reverse_search) {
320 // We don't need a marker, but an L.circle instance changes radius once you zoom in/out
321 var cm = L.circleMarker(
322 [request_lat, request_lon],
326 fillColor: '#ff7800',
335 var search_params = new URLSearchParams(window.location.search);
336 var viewbox = search_params.get('viewbox');
338 var coords = viewbox.split(','); // <x1>,<y1>,<x2>,<y2>
339 var bounds = L.latLngBounds([coords[1], coords[0]], [coords[3], coords[2]]);
340 L.rectangle(bounds, {
350 var MapPositionControl = L.Control.extend({
354 onAdd: function (/* map */) {
355 var container = L.DomUtil.create('div', 'my-custom-control');
357 $(container).text('show map bounds')
358 .addClass('leaflet-bar btn btn-sm btn-outline-secondary')
359 .on('click', function (e) {
362 $('#map-position').show();
365 $('#map-position-close a').on('click', function (e) {
368 $('#map-position').hide();
376 map.addControl(new MapPositionControl());
382 function update_viewbox_field() {
384 $('input[name=viewbox]')
385 .val($('input#use_viewbox')
386 .prop('checked') ? map_viewbox_as_string() : '');
389 map.on('move', function () {
390 display_map_position();
391 update_viewbox_field();
394 map.on('mousemove', function (e) {
395 display_map_position(e.latlng);
398 map.on('click', function (e) {
399 last_click_latlng = e.latlng;
400 display_map_position();
403 map.on('load', function () {
404 display_map_position();
407 $('input#use_viewbox').on('change', function () {
408 update_viewbox_field();
411 $("input[name='query-selector']").click(function () {
412 var query_val = $("input[name='query-selector']:checked").val();
413 if (query_val === 'simple') {
414 $('div.form-group-simple').removeClass('hidden');
415 $('div.form-group-structured').addClass('hidden');
416 $('.form-group-structured').find('input:text').val('');
417 } else if (query_val === 'structured') {
419 $('div.form-group-simple').addClass('hidden');
420 $('div.form-group-structured').removeClass('hidden');
421 $('.form-group-simple').find('input:text').val('');
425 function get_result_element(position) {
426 return $('.result').eq(position);
428 // function marker_for_result(result) {
429 // return L.marker([result.lat, result.lon], { riseOnHover: true, title: result.name });
431 function circle_for_result(result) {
435 fillColor: '#ff7800',
438 clickable: !is_reverse_search
440 return L.circleMarker([result.lat, result.lon], cm_style);
443 var layerGroup = (new L.layerGroup()).addTo(map);
445 function highlight_result(position, bool_focus) {
446 var result = nominatim_results[position];
447 if (!result) { return; }
448 var result_el = get_result_element(position);
450 $('.result').removeClass('highlight');
451 result_el.addClass('highlight');
453 layerGroup.clearLayers();
456 var circle = circle_for_result(result);
457 circle.on('click', function () {
458 highlight_result(position);
460 layerGroup.addLayer(circle);
463 if (result.boundingbox) {
465 [result.boundingbox[0] * 1, result.boundingbox[2] * 1],
466 [result.boundingbox[1] * 1, result.boundingbox[3] * 1]
470 if (result.geojson && result.geojson.type.match(/(Polygon)|(Line)/)) {
472 var geojson_layer = L.geoJson(
473 parse_and_normalize_geojson_string(result.geojson),
475 // https://leafletjs.com/reference-1.0.3.html#path-option
476 style: function (/* feature */) {
477 return { interactive: false, color: 'blue' };
481 layerGroup.addLayer(geojson_layer);
484 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
485 // layerGroup.addLayer(layer);
488 var result_coord = L.latLng(result.lat, result.lon);
490 if (is_reverse_search) {
491 // console.dir([result_coord, [request_lat, request_lon]]);
492 // make sure the search coordinates are in the map view as well
494 [result_coord, [request_lat, request_lon]],
497 maxZoom: map.getZoom()
501 map.panTo(result_coord, result.zoom || get_config_value('Map_Default_Zoom'));
511 $('.result').on('click', function () {
512 highlight_result($(this).data('position'), true);
515 if (is_reverse_search) {
516 map.on('click', function (e) {
517 $('form input[name=lat]').val(e.latlng.lat);
518 $('form input[name=lon]').val(e.latlng.wrap().lng);
522 $('#switch-coords').on('click', function (e) {
525 var lat = $('form input[name=lat]').val();
526 var lon = $('form input[name=lon]').val();
527 $('form input[name=lat]').val(lon);
528 $('form input[name=lon]').val(lat);
533 highlight_result(0, false);
535 // common mistake is to copy&paste latitude and longitude into the 'lat' search box
536 $('form input[name=lat]').on('change', function () {
537 var coords_split = $(this).val().split(',');
538 if (coords_split.length === 2) {
539 $(this).val(L.Util.trim(coords_split[0]));
540 $(this).siblings('input[name=lon]').val(L.Util.trim(coords_split[1]));
548 jQuery(document).ready(function () {
550 if (!$('#search-page,#reverse-page').length) { return; }
552 var is_reverse_search = !!($('#reverse-page').length);
554 var search_params = new URLSearchParams(window.location.search);
556 // return view('search', [
557 // 'sQuery' => $sQuery,
560 // 'aSearchResults' => $aSearchResults,
561 // 'sMoreURL' => 'example.com',
562 // 'sDataDate' => $this->fetch_status_date(),
566 var api_request_params;
569 if (is_reverse_search) {
570 api_request_params = {
571 lat: search_params.get('lat'),
572 lon: search_params.get('lon'),
573 zoom: (search_params.get('zoom') > 1
574 ? search_params.get('zoom')
575 : get_config_value('Reverse_Default_Search_Zoom')),
581 fLat: api_request_params.lat,
582 fLon: api_request_params.lon,
583 iZoom: (search_params.get('zoom') > 1
584 ? api_request_params.zoom
585 : get_config_value('Reverse_Default_Search_Zoom'))
589 if (api_request_params.lat && api_request_params.lon) {
591 fetch_from_api('reverse', api_request_params, function (aPlace) {
597 context.aPlace = aPlace;
599 render_template($('main'), 'reversepage-template', context);
600 update_html_title('Reverse result for '
601 + api_request_params.lat
603 + api_request_params.lon);
605 init_map_on_search_page(
608 api_request_params.lat,
609 api_request_params.lon,
610 api_request_params.zoom
616 render_template($('main'), 'reversepage-template', context);
618 init_map_on_search_page(
621 get_config_value('Map_Default_Lat'),
622 get_config_value('Map_Default_Lon'),
623 get_config_value('Map_Default_Zoom')
628 api_request_params = {
629 q: search_params.get('q'),
630 street: search_params.get('street'),
631 city: search_params.get('city'),
632 county: search_params.get('county'),
633 state: search_params.get('state'),
634 country: search_params.get('country'),
635 postalcode: search_params.get('postalcode'),
636 polygon_geojson: search_params.get('polygon_geojson') ? 1 : 0,
637 viewbox: search_params.get('viewbox'),
638 exclude_place_ids: search_params.get('exclude_place_ids'),
643 sQuery: api_request_params.q,
644 sViewBox: search_params.get('viewbox'),
645 env: Nominatim_Config
648 if (api_request_params.street || api_request_params.city || api_request_params.county
649 || api_request_params.state || api_request_params.country || api_request_params.postalcode) {
650 context.hStructured = {
651 street: api_request_params.street,
652 city: api_request_params.city,
653 county: api_request_params.county,
654 state: api_request_params.state,
655 country: api_request_params.country,
656 postalcode: api_request_params.postalcode
660 if (api_request_params.q || context.hStructured) {
662 fetch_from_api('search', api_request_params, function (aResults) {
664 context.aSearchResults = aResults;
666 if (aResults.length >= 10) {
667 var aExcludePlaceIds = [];
668 if (search_params.has('exclude_place_ids')) {
669 aExcludePlaceIds.search_params.get('exclude_place_ids').split(',');
671 for (var i = 0; i < aResults.length; i += 1) {
672 aExcludePlaceIds.push(aResults[i].place_id);
675 var parsed_url = new URLSearchParams(window.location.search);
676 parsed_url.set('exclude_place_ids', aExcludePlaceIds.join(','));
677 context.sMoreURL = '?' + parsed_url.toString();
680 render_template($('main'), 'searchpage-template', context);
681 update_html_title('Result for ' + api_request_params.q);
683 init_map_on_search_page(
686 get_config_value('Map_Default_Lat'),
687 get_config_value('Map_Default_Lon'),
688 get_config_value('Map_Default_Zoom')
696 render_template($('main'), 'searchpage-template', context);
698 init_map_on_search_page(
701 get_config_value('Map_Default_Lat'),
702 get_config_value('Map_Default_Lon'),
703 get_config_value('Map_Default_Zoom')
708 // *********************************************************
710 // *********************************************************
712 jQuery(document).ready(function () {
713 if (!$('#deletable-page').length) { return; }
715 var api_request_params = {
719 fetch_from_api('deletable', api_request_params, function (aPolygons) {
720 var context = { aPolygons: aPolygons };
722 render_template($('main'), 'deletable-template', context);
723 update_html_title('Deletable objects');
728 // *********************************************************
729 // BROKEN POLYGON PAGE
730 // *********************************************************
732 jQuery(document).ready(function () {
733 if (!$('#polygons-page').length) { return; }
735 var api_request_params = {
739 fetch_from_api('polygons', api_request_params, function (aPolygons) {
740 var context = { aPolygons: aPolygons };
742 render_template($('main'), 'polygons-template', context);
743 update_html_title('Broken polygons');