4 function parse_and_normalize_geojson_string(raw_string){
5 // normalize places the geometry into a featurecollection, similar to
6 // https://github.com/mapbox/geojson-normalize
8 type: "FeatureCollection",
12 geometry: JSON.parse(raw_string),
17 return parsed_geojson;
20 jQuery(document).ready(function(){
22 if ( !$('#search-page,#reverse-page').length ){ return; }
24 var is_reverse_search = !!( $('#reverse-page').length );
28 $(document).ready(function() {
29 $("input[name='query-selector']").click(function(){
30 var query_val = $("input[name='query-selector']:checked").val() ;
31 if (query_val == "simple") {
32 $("div.form-group-structured").hide();
33 $("div.form-group-simple").show();
34 $("div.form-group-structured .form-control").prop('disabled', true);
35 $("div.form-group-simple .form-control").prop('disabled', false);
36 $('.form-group-structured').find('input:text').val('');
38 else if (query_val == "structured") {
39 $("div.form-group-simple").hide();
40 $("div.form-group-structured").show();
41 $("div.form-group-structured .form-control").prop('disabled', false);
42 $("div.form-group-simple .form-control").prop('disabled', true);
43 $('.form-group-simple').find('input:text').val('');
47 if (nominatim_structured_query) {
48 $('input#structured').prop('checked', true).trigger('click');
50 $('input#simple').prop('checked', true).trigger('click');
54 map = new L.map('map', {
55 attributionControl: (nominatim_map_init.tile_attribution && nominatim_map_init.tile_attribution.length),
56 scrollWheelZoom: true, // !L.Browser.touch,
60 L.tileLayer(nominatim_map_init.tile_url, {
62 attribution: (nominatim_map_init.tile_attribution || null ) //'© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
65 map.setView([nominatim_map_init.lat, nominatim_map_init.lon], nominatim_map_init.zoom);
67 var osm2 = new L.TileLayer(nominatim_map_init.tile_url, {minZoom: 0, maxZoom: 13, attribution: (nominatim_map_init.tile_attribution || null )});
68 var miniMap = 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([nominatim_map_init.lat,nominatim_map_init.lon], { radius: 5, weight: 2, fillColor: '#ff7800', color: 'red', opacity: 0.75, clickable: false});
76 var MapPositionControl = L.Control.extend({
81 onAdd: function (map) {
82 var container = L.DomUtil.create('div', 'my-custom-control');
84 $(container).text('show map bounds').addClass('leaflet-bar btn btn-sm btn-default').on('click', function(e){
87 $('#map-position').show();
90 $('#map-position-close a').on('click', function(e){
93 $('#map-position').hide();
101 map.addControl(new MapPositionControl());
104 function display_map_position(mouse_lat_lng){
107 mouse_lat_lng = map.wrapLatLng(mouse_lat_lng);
109 html_mouse = "mouse position " + (mouse_lat_lng ? [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',') : '-');
110 html_click = "last click: " + (last_click_latlng ? [last_click_latlng.lat.toFixed(5),last_click_latlng.lng.toFixed(5)].join(',') : '-');
114 map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5) +
115 " <a target='_blank' href='" + map_link_to_osm() + "'>view on osm.org</a>";
117 html_zoom = "map zoom: " + map.getZoom();
119 html_viewbox = "viewbox: " + map_viewbox_as_string();
121 $('#map-position-inner').html([html_center,html_zoom,html_viewbox,html_click,html_mouse].join('<br/>'));
123 var center_lat_lng = map.wrapLatLng(map.getCenter());
124 var reverse_params = {
125 lat: center_lat_lng.lat.toFixed(5),
126 lon: center_lat_lng.lng.toFixed(5),
130 $('#switch-to-reverse').attr('href', 'reverse.php?' + $.param(reverse_params));
132 $('input#use_viewbox').trigger('change');
135 function update_viewbox_field(){
137 $('input[name=viewbox]').val( $('input#use_viewbox').prop('checked') ? map_viewbox_as_string() : '');
140 map.on('move', function(e) {
141 display_map_position();
142 update_viewbox_field();
145 map.on('mousemove', function(e) {
146 display_map_position(e.latlng);
149 map.on('click', function(e) {
150 last_click_latlng = e.latlng;
151 display_map_position();
154 map.on('load', function(e){
155 display_map_position();
159 $('input#use_viewbox').on('change', function(){
160 update_viewbox_field();
165 function map_viewbox_as_string() {
166 var bounds = map.getBounds();
167 var west = bounds.getWest();
168 var east = bounds.getEast();
170 if ((east - west) >= 360) { // covers more than whole planet
171 west = map.getCenter().lng-179.999;
172 east = map.getCenter().lng+179.999;
174 east = L.latLng(77, east).wrap().lng;
175 west = L.latLng(77, west).wrap().lng;
178 west.toFixed(5), // left
179 bounds.getNorth().toFixed(5), // top
180 east.toFixed(5), // right
181 bounds.getSouth().toFixed(5) // bottom
184 function map_link_to_osm(){
185 return "https://openstreetmap.org/#map=" + map.getZoom() + "/" + map.getCenter().lat + "/" + map.getCenter().lng;
188 function get_result_element(position){
189 return $('.result').eq(position);
191 function marker_for_result(result){
192 return L.marker([result.lat,result.lon], {riseOnHover:true,title:result.name });
194 function circle_for_result(result){
195 return L.circleMarker([result.lat,result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75, clickable: !is_reverse_search});
198 var layerGroup = new L.layerGroup().addTo(map);
199 function highlight_result(position, bool_focus){
200 var result = nominatim_results[position];
201 if (!result){ return }
202 var result_el = get_result_element(position);
204 $('.result').removeClass('highlight');
205 result_el.addClass('highlight');
207 layerGroup.clearLayers();
210 var circle = circle_for_result(result);
211 circle.on('click', function(){
212 highlight_result(position);
214 layerGroup.addLayer(circle);
216 if (result.aBoundingBox){
218 var bounds = [[result.aBoundingBox[0]*1,result.aBoundingBox[2]*1], [result.aBoundingBox[1]*1,result.aBoundingBox[3]*1]];
219 map.fitBounds(bounds);
221 if (result.asgeojson && result.asgeojson.match(/(Polygon)|(Line)/) ){
223 var geojson_layer = L.geoJson(
224 parse_and_normalize_geojson_string(result.asgeojson),
226 // http://leafletjs.com/reference-1.0.3.html#path-option
227 style: function(feature) {
228 return { interactive: false, color: 'blue' };
232 layerGroup.addLayer(geojson_layer);
235 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
236 // layerGroup.addLayer(layer);
240 var result_coord = L.latLng(result.lat, result.lon);
242 if ( is_reverse_search ){
243 // make sure the search coordinates are in the map view as well
244 map.fitBounds([result_coord, [nominatim_map_init.lat,nominatim_map_init.lon]], {padding: [50,50], maxZoom: map.getZoom()});
246 // better, but causes a leaflet warning
247 // map.panInsideBounds([[result.lat,result.lon], [nominatim_map_init.lat,nominatim_map_init.lon]], {animate: false});
250 map.panTo(result_coord, result.zoom || nominatim_map_init.zoom);
255 // var crosshairIcon = L.icon({
256 // iconUrl: 'images/crosshair.png',
257 // iconSize: [12, 12],
258 // iconAnchor: [6, 6],
260 // var crossMarker = new L.Marker([result.lat,result.lon], { icon: crosshairIcon, clickable: false});
261 // layerGroup.addLayer(crossMarker);
271 $('.result').on('click', function(e){
272 highlight_result($(this).data('position'), true);
275 if ( is_reverse_search ){
276 map.on('click', function(e){
277 $('form input[name=lat]').val( e.latlng.lat);
278 $('form input[name=lon]').val( e.latlng.wrap().lng);
282 $('#switch-coords').on('click', function(e){
285 var lat = $('form input[name=lat]').val();
286 var lon = $('form input[name=lon]').val();
287 $('form input[name=lat]').val(lon);
288 $('form input[name=lon]').val(lat);
292 var search_params = new URLSearchParams(location.search);
293 var viewbox = search_params.get('viewbox');
295 var coords = viewbox.split(','); // <x1>,<y1>,<x2>,<y2>
296 var bounds = L.latLngBounds([coords[1], coords[0]], [coords[3], coords[2]]);
297 L.rectangle(bounds, {color: "#69d53e", weight: 3, dashArray: '5 5', opacity: 0.8, fill: false}).addTo(map);
301 highlight_result(0, false);
303 // common mistake is to copy&paste latitude and longitude into the 'lat' search box
304 $('form input[name=lat]').on('change', function(){
305 var coords = $(this).val().split(',');
306 if (coords.length == 2) {
307 $(this).val(L.Util.trim(coords[0]));
308 $(this).siblings('input[name=lon]').val(L.Util.trim(coords[1]));
315 jQuery(document).ready(function(){
317 if ( !$('#details-index-page').length ){ return; }
319 $('#form-by-type-and-id,#form-by-osm-url').on('submit', function(e){
322 var val = $(this).find('input[type=edit]').val();
323 var matches = val.match(/^\s*([NWR])(\d+)\s*$/i);
326 matches = val.match(/\/(relation|way|node)\/(\d+)\s*$/);
330 $(this).find('input[name=osmtype]').val(matches[1].charAt(0).toUpperCase());
331 $(this).find('input[name=osmid]').val(matches[2]);
332 $(this).get(0).submit();
334 alert('invalid input');
339 jQuery(document).ready(function(){
341 if ( !$('#details-page').length ){ return; }
344 map = new L.map('map', {
345 // center: [nominatim_map_init.lat, nominatim_map_init.lon],
346 // zoom: nominatim_map_init.zoom,
347 attributionControl: (nominatim_map_init.tile_attribution && nominatim_map_init.tile_attribution.length),
348 scrollWheelZoom: true, // !L.Browser.touch,
353 L.tileLayer(nominatim_map_init.tile_url, {
355 attribution: (nominatim_map_init.tile_attribution || null ) //'© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
358 var layerGroup = new L.layerGroup().addTo(map);
360 var circle = L.circleMarker([nominatim_result.lat,nominatim_result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75});
361 map.addLayer(circle);
363 if ( nominatim_result.asgeojson ){
365 var geojson_layer = L.geoJson(
366 parse_and_normalize_geojson_string(nominatim_result.asgeojson),
368 // http://leafletjs.com/reference-1.0.3.html#path-option
369 style: function(feature) {
370 return { interactive: false, color: 'blue' };
374 map.addLayer(geojson_layer);
375 map.fitBounds(geojson_layer.getBounds());
377 map.setView([nominatim_result.lat,nominatim_result.lon],10);
380 var osm2 = new L.TileLayer(nominatim_map_init.tile_url, {minZoom: 0, maxZoom: 13, attribution: (nominatim_map_init.tile_attribution || null )});
381 var miniMap = new L.Control.MiniMap(osm2, {toggleDisplay: true}).addTo(map);