2 /*********************************************************
3 * FORWARD/REVERSE SEARCH PAGE
4 *********************************************************/
7 function display_map_position(mouse_lat_lng){
9 html_mouse = "mouse position " + (mouse_lat_lng ? [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',') : '-');
10 html_click = "last click: " + (last_click_latlng ? [last_click_latlng.lat.toFixed(5),last_click_latlng.lng.toFixed(5)].join(',') : '-');
14 map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5) +
15 " <a target='_blank' href='" + map_link_to_osm() + "'>view on osm.org</a>";
17 html_zoom = "map zoom: " + map.getZoom();
19 html_viewbox = "viewbox: " + map_viewbox_as_string();
21 $('#map-position-inner').html([html_center,html_zoom,html_viewbox,html_click,html_mouse].join('<br/>'));
23 var reverse_params = {
24 // lat: map.getCenter().lat.toFixed(5),
25 // lon: map.getCenter().lng.toFixed(5),
29 $('#switch-to-reverse').attr('href', 'reverse.html?' + $.param(reverse_params));
31 $('input#use_viewbox').trigger('change');
37 function init_map_on_search_page(is_reverse_search, nominatim_results, request_lat, request_lon, init_zoom) {
39 map = new L.map('map', {
40 // center: [nominatim_map_init.lat, nominatim_map_init.lon],
41 // zoom: nominatim_map_init.zoom,
42 attributionControl: (get_config_value('Map_Tile_Attribution') && get_config_value('Map_Tile_Attribution').length),
43 scrollWheelZoom: true, // !L.Browser.touch,
48 L.tileLayer(get_config_value('Map_Tile_URL'), {
49 noWrap: true, // otherwise we end up with click coordinates like latitude -728
51 attribution: (get_config_value('Map_Tile_Attribution') || null ) //'© <a href="http://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 var miniMap = 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([request_lat, request_lon], { radius: 5, weight: 2, fillColor: '#ff7800', color: 'red', opacity: 0.75, clickable: false});
67 var MapPositionControl = L.Control.extend({
71 onAdd: function (map) {
72 var container = L.DomUtil.create('div', 'my-custom-control');
74 $(container).text('show map bounds').addClass('leaflet-bar btn btn-sm btn-default').on('click', function(e){
77 $('#map-position').show();
80 $('#map-position-close a').on('click', function(e){
83 $('#map-position').hide();
91 map.addControl(new MapPositionControl());
97 function update_viewbox_field(){
99 $('input[name=viewbox]').val( $('input#use_viewbox').prop('checked') ? map_viewbox_as_string() : '');
102 map.on('move', function(e) {
103 display_map_position();
104 update_viewbox_field();
107 map.on('mousemove', function(e) {
108 display_map_position(e.latlng);
111 map.on('click', function(e) {
112 last_click_latlng = e.latlng;
113 display_map_position();
116 map.on('load', function(e){
117 display_map_position();
121 $('input#use_viewbox').on('change', function(){
122 update_viewbox_field();
128 function get_result_element(position){
129 return $('.result').eq(position);
131 function marker_for_result(result){
132 return L.marker([result.lat,result.lon], {riseOnHover:true,title:result.name });
134 function circle_for_result(result){
135 return L.circleMarker([result.lat,result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75, clickable: !is_reverse_search});
138 var layerGroup = new L.layerGroup().addTo(map);
139 function highlight_result(position, bool_focus){
140 var result = nominatim_results[position];
141 if (!result){ return }
142 var result_el = get_result_element(position);
144 $('.result').removeClass('highlight');
145 result_el.addClass('highlight');
147 layerGroup.clearLayers();
150 var circle = circle_for_result(result);
151 circle.on('click', function(){
152 highlight_result(position);
154 layerGroup.addLayer(circle);
156 if (result.boundingbox){
158 var bounds = [[result.boundingbox[0]*1,result.boundingbox[2]*1], [result.boundingbox[1]*1,result.boundingbox[3]*1]];
159 map.fitBounds(bounds);
161 if (result.geojson && result.geojson.type.match(/(Polygon)|(Line)/) ){
163 var geojson_layer = L.geoJson(
164 parse_and_normalize_geojson_string(result.geojson),
166 // http://leafletjs.com/reference-1.0.3.html#path-option
167 style: function(feature) {
168 return { interactive: false, color: 'blue' };
172 layerGroup.addLayer(geojson_layer);
175 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
176 // layerGroup.addLayer(layer);
180 var result_coord = L.latLng(result.lat, result.lon);
182 if ( is_reverse_search ){
183 // console.dir([result_coord, [request_lat, request_lon]]);
184 // make sure the search coordinates are in the map view as well
185 map.fitBounds([result_coord, [request_lat, request_lon]], {padding: [50,50], maxZoom: map.getZoom()});
187 // better, but causes a leaflet warning
188 // map.panInsideBounds([[result.lat,result.lon], [nominatim_map_init.lat,nominatim_map_init.lon]], {animate: false});
191 map.panTo(result_coord, result.zoom || get_config_value('Map_Default_Zoom'));
201 $('.result').on('click', function(e){
202 highlight_result($(this).data('position'), true);
205 if ( is_reverse_search ){
206 map.on('click', function(e){
207 $('form input[name=lat]').val( e.latlng.lat);
208 $('form input[name=lon]').val( e.latlng.lng);
212 $('#switch-coords').on('click', function(e){
215 var lat = $('form input[name=lat]').val();
216 var lon = $('form input[name=lon]').val();
217 $('form input[name=lat]').val(lon);
218 $('form input[name=lon]').val(lat);
223 highlight_result(0, false);
225 // common mistake is to copy&paste latitude and longitude into the 'lat' search box
226 $('form input[name=lat]').on('change', function(){
227 var coords = $(this).val().split(',');
228 if (coords.length == 2) {
229 $(this).val(L.Util.trim(coords[0]));
230 $(this).siblings('input[name=lon]').val(L.Util.trim(coords[1]));
241 jQuery(document).ready(function(){
243 if ( !$('#search-page,#reverse-page').length ){ return; }
245 var is_reverse_search = !!( $('#reverse-page').length );
246 var endpoint = is_reverse_search ? 'reverse' : 'search';
249 var search_params = new URLSearchParams(location.search);
251 // return view('search', [
252 // 'sQuery' => $sQuery,
255 // 'aSearchResults' => $aSearchResults,
256 // 'sMoreURL' => 'example.com',
257 // 'sDataDate' => $this->fetch_status_date(),
262 if (is_reverse_search) {
263 var api_request_params = {
264 // lat: typeof(search_params.get('lat') !== 'undefined') ? search_params.get('lat') : get_config_value('Map_Default_Lat'),
265 // lon: typeof(search_params.get('lon') !== 'undefined') ? search_params.get('lon') : get_config_value('Map_Default_Lon'),
266 lat: search_params.get('lat'),
267 lon: search_params.get('lon'),
268 zoom: (search_params.get('zoom') !== '' ? search_params.get('zoom') : get_config_value('Map_Default_Zoom')),
274 fLat: api_request_params.lat,
275 fLon: api_request_params.lon,
276 iZoom: (search_params.get('zoom') !== '' ? api_request_params.zoom : undefined)
280 if (api_request_params.lat && api_request_params.lon) {
282 fetch_from_api('reverse', api_request_params, function(aPlace){
288 context.aPlace = aPlace;
290 render_template($('main'), 'reversepage-template', context);
292 init_map_on_search_page(is_reverse_search, [aPlace], api_request_params.lat, api_request_params.lon, api_request_params.zoom);
297 render_template($('main'), 'reversepage-template', context);
299 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'));
303 var api_request_params = {
304 q: search_params.get('q'),
305 polygon_geojson: search_params.get('polygon_geojson') ? 1 : 0,
306 viewbox: search_params.get('viewbox'),
311 // aSearchResults: aResults,
312 sQuery: api_request_params.q,
314 env: Nominatim_Config,
318 if (api_request_params.q) {
320 fetch_from_api('search', api_request_params, function(aResults){
322 context.aSearchResults = aResults;
324 render_template($('main'), 'searchpage-template', context);
326 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'));
333 render_template($('main'), 'searchpage-template', context);
335 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'));