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).on('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:    !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     map.setView([nominatim_map_init.lat, nominatim_map_init.lon], nominatim_map_init.zoom);
 
  42     if ( is_reverse_search ){
 
  43         // We don't need a marker, but an L.circle instance changes radius once you zoom in/out
 
  44         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});
 
  48     var MapPositionControl = L.Control.extend({
 
  53             onAdd: function (map) {
 
  54                     var container = L.DomUtil.create('div', 'my-custom-control');
 
  56                     $(container).text('show map bounds').addClass('leaflet-bar btn btn-sm btn-default').on('click', function(e){
 
  59                         $('#map-position').show();
 
  62                     $('#map-position-close a').on('click', function(e){
 
  65                         $('#map-position').hide();
 
  73     map.addControl(new MapPositionControl());
 
  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.php?' + $.param(reverse_params));
 
 100         $('input#use_viewbox').trigger('change');
 
 103     function update_viewbox_field(){
 
 105         $('input[name=viewbox]').val( $('input#use_viewbox').prop('checked') ? map_viewbox_as_string() : '');
 
 108     map.on('move', function(e) {
 
 109         display_map_position();
 
 110         update_viewbox_field();
 
 113     map.on('mousemove', function(e) {
 
 114         display_map_position(e.latlng);
 
 117     map.on('click', function(e) {
 
 118         last_click_latlng = e.latlng;
 
 119         display_map_position();
 
 122     map.on('load', function(e){
 
 123         display_map_position();
 
 127     $('input#use_viewbox').on('change', function(){
 
 128         update_viewbox_field();
 
 133     function map_viewbox_as_string() {
 
 134         // since .toBBoxString() doesn't round numbers
 
 136             map.getBounds().getSouthWest().lng.toFixed(5), // left
 
 137             map.getBounds().getNorthEast().lat.toFixed(5), // top
 
 138             map.getBounds().getNorthEast().lng.toFixed(5), // right
 
 139             map.getBounds().getSouthWest().lat.toFixed(5)  // bottom
 
 142     function map_link_to_osm(){
 
 143         return "http://openstreetmap.org/#map=" + map.getZoom() + "/" + map.getCenter().lat + "/" + map.getCenter().lng;
 
 146     function get_result_element(position){
 
 147         return $('.result').eq(position);
 
 149     function marker_for_result(result){
 
 150         return L.marker([result.lat,result.lon], {riseOnHover:true,title:result.name });
 
 152     function circle_for_result(result){
 
 153         return L.circleMarker([result.lat,result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75, clickable: !is_reverse_search});
 
 156     var layerGroup = new L.layerGroup().addTo(map);
 
 157     function highlight_result(position, bool_focus){
 
 158         var result = nominatim_results[position];
 
 159         if (!result){ return }
 
 160         var result_el = get_result_element(position);
 
 162         $('.result').removeClass('highlight');
 
 163         result_el.addClass('highlight');
 
 165         layerGroup.clearLayers();
 
 168             var circle = circle_for_result(result);
 
 169             circle.on('click', function(){
 
 170                 highlight_result(position);
 
 172             layerGroup.addLayer(circle);            
 
 174         if (result.aBoundingBox){
 
 176             var bounds = [[result.aBoundingBox[0]*1,result.aBoundingBox[2]*1], [result.aBoundingBox[1]*1,result.aBoundingBox[3]*1]];
 
 177             map.fitBounds(bounds);
 
 179             if (result.asgeojson && result.asgeojson.match(/(Polygon)|(Line)/) ){
 
 181                 var geojson_layer = L.geoJson(
 
 182                     parse_and_normalize_geojson_string(result.asgeojson),
 
 184                         // http://leafletjs.com/reference-1.0.3.html#path-option
 
 185                         style: function(feature) {
 
 186                             return { interactive: false, color: 'blue' }; 
 
 190                 layerGroup.addLayer(geojson_layer);
 
 193                 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
 
 194                 // layerGroup.addLayer(layer);
 
 198             if ( is_reverse_search ){
 
 199                 // make sure the search coordinates are in the map view as well
 
 200                 map.fitBounds([[result.lat,result.lon], [nominatim_map_init.lat,nominatim_map_init.lon]], {padding: [50,50], maxZoom: map.getZoom()});
 
 202                 // better, but causes a leaflet warning
 
 203                 // map.panInsideBounds([[result.lat,result.lon], [nominatim_map_init.lat,nominatim_map_init.lon]], {animate: false});
 
 206                 map.panTo([result.lat,result.lon], result.zoom || nominatim_map_init.zoom);
 
 210         // var crosshairIcon = L.icon({
 
 211         //  iconUrl:     'images/crosshair.png',
 
 212         //  iconSize:    [12, 12],
 
 213         //  iconAnchor:  [6, 6],
 
 215         // var crossMarker = new L.Marker([result.lat,result.lon], { icon: crosshairIcon, clickable: false});
 
 216         // layerGroup.addLayer(crossMarker);
 
 226     $('.result').on('click', function(e){
 
 227         highlight_result($(this).data('position'), true);
 
 230     if ( is_reverse_search ){
 
 231         map.on('click', function(e){
 
 232             $('form input[name=lat]').val( e.latlng.lat);
 
 233             $('form input[name=lon]').val( e.latlng.lng);
 
 237         $('#switch-coords').on('click', function(e){
 
 238             var lat = $('form input[name=lat]').val();
 
 239             var lon = $('form input[name=lon]').val();
 
 240             $('form input[name=lat]').val(lon);
 
 241             $('form input[name=lon]').val(lat);
 
 246     highlight_result(0, false);
 
 252 jQuery(document).on('ready', function(){
 
 254     if ( !$('#details-page').length ){ return; }
 
 257         map = new L.map('map', {
 
 258                     // center: [nominatim_map_init.lat, nominatim_map_init.lon],
 
 259                     // zoom:   nominatim_map_init.zoom,
 
 260                     attributionControl: (nominatim_map_init.tile_attribution && nominatim_map_init.tile_attribution.length),
 
 261                     scrollWheelZoom:    false,
 
 266         L.tileLayer(nominatim_map_init.tile_url, {
 
 268             attribution: (nominatim_map_init.tile_attribution || null ) //'© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
 
 272         var layerGroup = new L.layerGroup().addTo(map);
 
 274         var circle = L.circleMarker([nominatim_result.lat,nominatim_result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75});
 
 275         map.addLayer(circle);
 
 277         if ( nominatim_result.asgeojson ){
 
 279             var geojson_layer = L.geoJson(
 
 280                 parse_and_normalize_geojson_string(nominatim_result.asgeojson),
 
 282                     // http://leafletjs.com/reference-1.0.3.html#path-option
 
 283                     style: function(feature) {
 
 284                         return { interactive: false, color: 'blue' }; 
 
 288             map.addLayer(geojson_layer);
 
 289             map.fitBounds(geojson_layer.getBounds());
 
 291             map.setView([nominatim_result.lat,nominatim_result.lon],10);