5 /*********************************************************
7 *********************************************************/
9 function get_config_value(str, default_val) {
10 return (typeof Nominatim_Config[str] !== 'undefined' ? Nominatim_Config[str] : default_val);
13 function parse_and_normalize_geojson_string(raw_string){
14 // normalize places the geometry into a featurecollection, similar to
15 // https://github.com/mapbox/geojson-normalize
16 var parsed_geojson = {
17 type: "FeatureCollection",
21 geometry: JSON.parse(raw_string),
26 return parsed_geojson;
29 function map_link_to_osm(){
30 return "https://openstreetmap.org/#map=" + map.getZoom() + "/" + map.getCenter().lat + "/" + map.getCenter().lng;
33 function map_viewbox_as_string() {
34 // since .toBBoxString() doesn't round numbers
36 map.getBounds().getSouthWest().lng.toFixed(5), // left
37 map.getBounds().getNorthEast().lat.toFixed(5), // top
38 map.getBounds().getNorthEast().lng.toFixed(5), // right
39 map.getBounds().getSouthWest().lat.toFixed(5) // bottom
44 /*********************************************************
46 *********************************************************/
48 function fetch_from_api(endpoint_name, params, callback) {
49 var api_url = get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?' + $.param(params);
50 $('#api-request-link').attr('href', api_url);
52 $.get(api_url, function(data){
57 function update_data_date() {
58 fetch_from_api('status', {format: 'json'}, function(data){
59 $('#data-date').text(data.data_last_updated.formatted);
63 function render_template(el, template_name, page_context) {
64 var template_source = $('#' + template_name).text();
65 var template = Handlebars.compile(template_source);
66 var html = template(page_context);
71 /*********************************************************
72 * FORWARD/REVERSE SEARCH PAGE
73 *********************************************************/
76 function display_map_position(mouse_lat_lng){
78 html_mouse = "mouse position " + (mouse_lat_lng ? [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',') : '-');
79 html_click = "last click: " + (last_click_latlng ? [last_click_latlng.lat.toFixed(5),last_click_latlng.lng.toFixed(5)].join(',') : '-');
83 map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5) +
84 " <a target='_blank' href='" + map_link_to_osm() + "'>view on osm.org</a>";
86 html_zoom = "map zoom: " + map.getZoom();
88 html_viewbox = "viewbox: " + map_viewbox_as_string();
90 $('#map-position-inner').html([html_center,html_zoom,html_viewbox,html_click,html_mouse].join('<br/>'));
92 var reverse_params = {
93 // lat: map.getCenter().lat.toFixed(5),
94 // lon: map.getCenter().lng.toFixed(5),
98 $('#switch-to-reverse').attr('href', 'reverse.html?' + $.param(reverse_params));
100 $('input#use_viewbox').trigger('change');
106 function init_map_on_search_page(is_reverse_search, nominatim_results) {
108 map = new L.map('map', {
109 // center: [nominatim_map_init.lat, nominatim_map_init.lon],
110 // zoom: nominatim_map_init.zoom,
111 attributionControl: (get_config_value('Map_Tile_Attribution') && get_config_value('Map_Tile_Attribution').length),
112 scrollWheelZoom: true, // !L.Browser.touch,
117 L.tileLayer(get_config_value('Map_Tile_URL'), {
118 noWrap: true, // otherwise we end up with click coordinates like latitude -728
120 attribution: (get_config_value('Map_Tile_Attribution') || null ) //'© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
123 // console.log(Nominatim_Config);
125 map.setView([get_config_value('Map_Default_Lat'), get_config_value('Map_Default_Lon')], get_config_value('Map_Default_Zoom'));
127 var osm2 = new L.TileLayer(get_config_value('Map_Tile_URL'), {minZoom: 0, maxZoom: 13, attribution: (get_config_value('Map_Tile_Attribution') || null )});
128 var miniMap = new L.Control.MiniMap(osm2, {toggleDisplay: true}).addTo(map);
130 if (is_reverse_search) {
131 // We don't need a marker, but an L.circle instance changes radius once you zoom in/out
132 var cm = L.circleMarker([get_config_value('Map_Default_Lat'), get_config_value('Map_Default_Lon')], { radius: 5, weight: 2, fillColor: '#ff7800', color: 'red', opacity: 0.75, clickable: false});
136 var MapPositionControl = L.Control.extend({
140 onAdd: function (map) {
141 var container = L.DomUtil.create('div', 'my-custom-control');
143 $(container).text('show map bounds').addClass('leaflet-bar btn btn-sm btn-default').on('click', function(e){
146 $('#map-position').show();
149 $('#map-position-close a').on('click', function(e){
152 $('#map-position').hide();
160 map.addControl(new MapPositionControl());
166 function update_viewbox_field(){
168 $('input[name=viewbox]').val( $('input#use_viewbox').prop('checked') ? map_viewbox_as_string() : '');
171 map.on('move', function(e) {
172 display_map_position();
173 update_viewbox_field();
176 map.on('mousemove', function(e) {
177 display_map_position(e.latlng);
180 map.on('click', function(e) {
181 last_click_latlng = e.latlng;
182 display_map_position();
185 map.on('load', function(e){
186 display_map_position();
190 $('input#use_viewbox').on('change', function(){
191 update_viewbox_field();
197 function get_result_element(position){
198 return $('.result').eq(position);
200 function marker_for_result(result){
201 return L.marker([result.lat,result.lon], {riseOnHover:true,title:result.name });
203 function circle_for_result(result){
204 return L.circleMarker([result.lat,result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75, clickable: !is_reverse_search});
207 var layerGroup = new L.layerGroup().addTo(map);
208 function highlight_result(position, bool_focus){
209 var result = nominatim_results[position];
210 if (!result){ return }
211 var result_el = get_result_element(position);
213 $('.result').removeClass('highlight');
214 result_el.addClass('highlight');
216 layerGroup.clearLayers();
219 var circle = circle_for_result(result);
220 circle.on('click', function(){
221 highlight_result(position);
223 layerGroup.addLayer(circle);
225 if (result.aBoundingBox){
227 var bounds = [[result.aBoundingBox[0]*1,result.aBoundingBox[2]*1], [result.aBoundingBox[1]*1,result.aBoundingBox[3]*1]];
228 map.fitBounds(bounds);
230 if (result.asgeojson && result.asgeojson.match(/(Polygon)|(Line)/) ){
232 var geojson_layer = L.geoJson(
233 parse_and_normalize_geojson_string(result.asgeojson),
235 // http://leafletjs.com/reference-1.0.3.html#path-option
236 style: function(feature) {
237 return { interactive: false, color: 'blue' };
241 layerGroup.addLayer(geojson_layer);
244 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
245 // layerGroup.addLayer(layer);
249 var result_coord = L.latLng(result.lat, result.lon);
251 if ( is_reverse_search ){
252 // make sure the search coordinates are in the map view as well
253 map.fitBounds([result_coord, [get_config_value('Map_Default_Lat'), get_config_value('Map_Default_Lon')]], {padding: [50,50], maxZoom: map.getZoom()});
255 // better, but causes a leaflet warning
256 // map.panInsideBounds([[result.lat,result.lon], [nominatim_map_init.lat,nominatim_map_init.lon]], {animate: false});
259 map.panTo(result_coord, result.zoom || get_config_value('Map_Default_Zoom'));
269 $('.result').on('click', function(e){
270 highlight_result($(this).data('position'), true);
273 if ( is_reverse_search ){
274 map.on('click', function(e){
275 $('form input[name=lat]').val( e.latlng.lat);
276 $('form input[name=lon]').val( e.latlng.lng);
280 $('#switch-coords').on('click', function(e){
283 var lat = $('form input[name=lat]').val();
284 var lon = $('form input[name=lon]').val();
285 $('form input[name=lat]').val(lon);
286 $('form input[name=lon]').val(lat);
291 highlight_result(0, false);
293 // common mistake is to copy&paste latitude and longitude into the 'lat' search box
294 $('form input[name=lat]').on('change', function(){
295 var coords = $(this).val().split(',');
296 if (coords.length == 2) {
297 $(this).val(L.Util.trim(coords[0]));
298 $(this).siblings('input[name=lon]').val(L.Util.trim(coords[1]));
312 jQuery(document).ready(function(){
314 if ( !$('#search-page,#reverse-page').length ){ return; }
316 var is_reverse_search = !!( $('#reverse-page').length );
317 var endpoint = is_reverse_search ? 'reverse' : 'search';
320 var search_params = new URLSearchParams(location.search);
323 // return view('search', [
324 // 'sQuery' => $sQuery,
327 // 'aSearchResults' => $aSearchResults,
328 // 'sMoreURL' => 'example.com',
329 // 'sDataDate' => $this->fetch_status_date(),
334 if (is_reverse_search) {
335 var api_request_params = {
336 lat: search_params.get('lat') || 0,
337 lon: search_params.get('lon') || 0,
338 zoom: search_params.get('zoom'),
342 fetch_from_api('reverse', api_request_params, function(aPlace){
350 fLat: api_request_params.lat,
351 fLon: api_request_params.lon,
352 iZoom: api_request_params.zoom
355 render_template($('main'), 'reversepage-template', context);
357 init_map_on_search_page(is_reverse_search, [aPlace]);
362 var api_request_params = {
363 q: search_params.get('q'),
364 polygon_geojson: search_params.get('polygon_geojson') ? 1 : 0,
365 polygon: search_params.get('polygon'),
369 fetch_from_api('search', api_request_params, function(aResults){
372 aSearchResults: aResults,
373 sQuery: api_request_params.q,
375 env: Nominatim_Config,
379 render_template($('main'), 'searchpage-template', context);
381 init_map_on_search_page(is_reverse_search, aResults);
391 /*********************************************************
393 *********************************************************/
397 function init_map_on_detail_page(lat, lon, geojson) {
398 map = new L.map('map', {
399 // center: [nominatim_map_init.lat, nominatim_map_init.lon],
400 // zoom: nominatim_map_init.zoom,
401 attributionControl: (get_config_value('Map_Tile_Attribution') && get_config_value('Map_Tile_Attribution').length),
402 scrollWheelZoom: true, // !L.Browser.touch,
406 L.tileLayer(get_config_value('Map_Tile_URL'), {
408 attribution: (get_config_value('Map_Tile_Attribution') || null ) //'© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
411 var layerGroup = new L.layerGroup().addTo(map);
413 var circle = L.circleMarker([lat,lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75});
414 map.addLayer(circle);
417 var geojson_layer = L.geoJson(
418 // http://leafletjs.com/reference-1.0.3.html#path-option
419 parse_and_normalize_geojson_string(geojson),
421 style: function(feature) {
422 return { interactive: false, color: 'blue' };
426 map.addLayer(geojson_layer);
427 map.fitBounds(geojson_layer.getBounds());
429 map.setView([lat,lon],10);
432 var osm2 = new L.TileLayer(get_config_value('Map_Tile_URL'), {minZoom: 0, maxZoom: 13, attribution: (get_config_value('Map_Tile_Attribution') || null )});
433 var miniMap = new L.Control.MiniMap(osm2, {toggleDisplay: true}).addTo(map);
436 jQuery(document).ready(function(){
438 if ( !$('#details-page').length ){ return; }
440 var search_params = new URLSearchParams(location.search);
441 // var place_id = search_params.get('place_id');
443 var api_request_params = {
444 place_id: search_params.get('place_id'),
449 fetch_from_api('details', api_request_params, function(aFeature){
451 var context = { aPlace: aFeature };
453 render_template($('main'), 'detailspage-template', context);
457 init_map_on_detail_page(aFeature.lat, aFeature.lon, aFeature.asgeojson);