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 map = new L.map('map', {
29 attributionControl: (nominatim_map_init.tile_attribution && nominatim_map_init.tile_attribution.length),
30 scrollWheelZoom: true, // !L.Browser.touch,
34 L.tileLayer(nominatim_map_init.tile_url, {
35 noWrap: true, // otherwise we end up with click coordinates like latitude -728
37 attribution: (nominatim_map_init.tile_attribution || null ) //'© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
40 var osm2 = new L.TileLayer(nominatim_map_init.tile_url, {minZoom: 0, maxZoom: 13, attribution: (nominatim_map_init.tile_attribution || null )});
41 var miniMap = new L.Control.MiniMap(osm2, {toggleDisplay: true}).addTo(map);
43 if ( is_reverse_search ){
44 // We don't need a marker, but an L.circle instance changes radius once you zoom in/out
45 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});
49 var MapPositionControl = L.Control.extend({
54 onAdd: function (map) {
55 var container = L.DomUtil.create('div', 'my-custom-control');
57 $(container).text('show map bounds').addClass('leaflet-bar btn btn-sm btn-default').on('click', function(e){
60 $('#map-position').show();
63 $('#map-position-close a').on('click', function(e){
66 $('#map-position').hide();
74 map.addControl(new MapPositionControl());
77 function display_map_position(mouse_lat_lng){
79 html_mouse = "mouse position " + (mouse_lat_lng ? [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',') : '-');
80 html_click = "last click: " + (last_click_latlng ? [last_click_latlng.lat.toFixed(5),last_click_latlng.lng.toFixed(5)].join(',') : '-');
84 map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5) +
85 " <a target='_blank' href='" + map_link_to_osm() + "'>view on osm.org</a>";
87 html_zoom = "map zoom: " + map.getZoom();
89 html_viewbox = "viewbox: " + map_viewbox_as_string();
91 $('#map-position-inner').html([html_center,html_zoom,html_viewbox,html_click,html_mouse].join('<br/>'));
93 var reverse_params = {
94 // lat: map.getCenter().lat.toFixed(5),
95 // lon: map.getCenter().lng.toFixed(5),
99 $('#switch-to-reverse').attr('href', 'reverse.php?' + $.param(reverse_params));
101 $('input#use_viewbox').trigger('change');
104 function update_viewbox_field(){
106 $('input[name=viewbox]').val( $('input#use_viewbox').prop('checked') ? map_viewbox_as_string() : '');
109 map.on('move', function(e) {
110 display_map_position();
111 update_viewbox_field();
114 map.on('mousemove', function(e) {
115 display_map_position(e.latlng);
118 map.on('click', function(e) {
119 last_click_latlng = e.latlng;
120 display_map_position();
123 map.on('load', function(e){
124 display_map_position();
128 $('input#use_viewbox').on('change', function(){
129 update_viewbox_field();
134 function map_viewbox_as_string() {
135 // since .toBBoxString() doesn't round numbers
137 map.getBounds().getSouthWest().lng.toFixed(5), // left
138 map.getBounds().getNorthEast().lat.toFixed(5), // top
139 map.getBounds().getNorthEast().lng.toFixed(5), // right
140 map.getBounds().getSouthWest().lat.toFixed(5) // bottom
143 function map_link_to_osm(){
144 return "http://openstreetmap.org/#map=" + map.getZoom() + "/" + map.getCenter().lat + "/" + map.getCenter().lng;
147 function get_result_element(position){
148 return $('.result').eq(position);
150 function marker_for_result(result){
151 return L.marker([result.lat,result.lon], {riseOnHover:true,title:result.name });
153 function circle_for_result(result){
154 return L.circleMarker([result.lat,result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75, clickable: !is_reverse_search});
157 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);
175 if (result.aBoundingBox){
177 var bounds = [[result.aBoundingBox[0]*1,result.aBoundingBox[2]*1], [result.aBoundingBox[1]*1,result.aBoundingBox[3]*1]];
178 map.fitBounds(bounds);
180 if (result.asgeojson && result.asgeojson.match(/(Polygon)|(Line)/) ){
182 var geojson_layer = L.geoJson(
183 parse_and_normalize_geojson_string(result.asgeojson),
185 // http://leafletjs.com/reference-1.0.3.html#path-option
186 style: function(feature) {
187 return { interactive: false, color: 'blue' };
191 layerGroup.addLayer(geojson_layer);
194 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
195 // layerGroup.addLayer(layer);
199 var result_coord = L.latLng(result.lat, result.lon);
201 if ( is_reverse_search ){
202 // make sure the search coordinates are in the map view as well
203 map.fitBounds([result_coord, [nominatim_map_init.lat,nominatim_map_init.lon]], {padding: [50,50], maxZoom: map.getZoom()});
205 // better, but causes a leaflet warning
206 // map.panInsideBounds([[result.lat,result.lon], [nominatim_map_init.lat,nominatim_map_init.lon]], {animate: false});
209 map.panTo(result_coord, result.zoom || nominatim_map_init.zoom);
214 // var crosshairIcon = L.icon({
215 // iconUrl: 'images/crosshair.png',
216 // iconSize: [12, 12],
217 // iconAnchor: [6, 6],
219 // var crossMarker = new L.Marker([result.lat,result.lon], { icon: crosshairIcon, clickable: false});
220 // layerGroup.addLayer(crossMarker);
230 $('.result').on('click', function(e){
231 highlight_result($(this).data('position'), true);
234 if ( is_reverse_search ){
235 map.on('click', function(e){
236 $('form input[name=lat]').val( e.latlng.lat);
237 $('form input[name=lon]').val( e.latlng.lng);
241 $('#switch-coords').on('click', function(e){
244 var lat = $('form input[name=lat]').val();
245 var lon = $('form input[name=lon]').val();
246 $('form input[name=lat]').val(lon);
247 $('form input[name=lon]').val(lat);
252 highlight_result(0, false);
254 // common mistake is to copy&paste latitude and longitude into the 'lat' search box
255 $('form input[name=lat]').on('change', function(){
256 var coords = $(this).val().split(',');
257 if (coords.length == 2) {
258 $(this).val(L.Util.trim(coords[0]));
259 $(this).siblings('input[name=lon]').val(L.Util.trim(coords[1]));
266 jQuery(document).ready(function(){
268 if ( !$('#details-page').length ){ return; }
271 map = new L.map('map', {
272 // center: [nominatim_map_init.lat, nominatim_map_init.lon],
273 // zoom: nominatim_map_init.zoom,
274 attributionControl: (nominatim_map_init.tile_attribution && nominatim_map_init.tile_attribution.length),
275 scrollWheelZoom: true, // !L.Browser.touch,
280 L.tileLayer(nominatim_map_init.tile_url, {
282 attribution: (nominatim_map_init.tile_attribution || null ) //'© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
285 var layerGroup = new L.layerGroup().addTo(map);
287 var circle = L.circleMarker([nominatim_result.lat,nominatim_result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75});
288 map.addLayer(circle);
290 if ( nominatim_result.asgeojson ){
292 var geojson_layer = L.geoJson(
293 parse_and_normalize_geojson_string(nominatim_result.asgeojson),
295 // http://leafletjs.com/reference-1.0.3.html#path-option
296 style: function(feature) {
297 return { interactive: false, color: 'blue' };
301 map.addLayer(geojson_layer);
302 map.fitBounds(geojson_layer.getBounds());
304 map.setView([nominatim_result.lat,nominatim_result.lon],10);
307 var osm2 = new L.TileLayer(nominatim_map_init.tile_url, {minZoom: 0, maxZoom: 13, attribution: (nominatim_map_init.tile_attribution || null )});
308 var miniMap = new L.Control.MiniMap(osm2, {toggleDisplay: true}).addTo(map);