]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/map.js.erb
Show OSM objects for Nominatim search results as map overlays
[rails.git] / app / assets / javascripts / map.js.erb
index 6701cf74011009fa320ef5616ceeed08a32510fc..9900935701f6511155ad78c322538e087a357255 100644 (file)
@@ -1,5 +1,5 @@
 // Leaflet extensions
-L.LatLngBounds.include({
+L.extend(L.LatLngBounds.prototype, {
   getSouthLat: function () {
     return this._southWest.lat;
   },
@@ -29,10 +29,14 @@ L.LatLngBounds.include({
   getSize: function () {
     return (this._northEast.lat - this._southWest.lat) *
            (this._northEast.lng - this._southWest.lng);
+  },
+
+  wrap: function () {
+    return new L.LatLngBounds(this._southWest.wrap(), this._northEast.wrap());
   }
 });
 
-L.Bounds.include({
+L.extend(L.Bounds.prototype, {
   getWidth: function () {
    return this.max.x - this.min.x;
   },
@@ -45,6 +49,7 @@ L.Bounds.include({
 L.Icon.Default.imagePath = <%= "#{asset_prefix}/images".to_json %>;
 
 var map;
+var objectLayer;
 
 var layers = [
   {
@@ -78,12 +83,12 @@ var layers = [
 ];
 
 function createMap(divName, options) {
-  options = $.extend({zoomControl: false, panZoomControl: true, layerControl: true}, options);
+  options = $.extend({zoomControl: true, panZoomControl: true, layerControl: true}, options);
 
   map = L.map(divName, $.extend({}, options, {panControl: false, zoomsliderControl: false, maxZoom: 18}));
 
   if (map.attributionControl) {
-    map.attributionControl.setPrefix(''); // For tmcw
+    map.attributionControl.setPrefix('');
   }
 
   if (options.panZoomControl) {
@@ -95,16 +100,16 @@ function createMap(divName, options) {
 
   if (options.layerControl) {
     layersControl.addTo(map);
+    map.layersControl = layersControl;
   }
 
   for (var i = 0; i < layers.length; i++) {
     layers[i].layer = new (layers[i].klass)(layers[i]);
-    if (i == 0) {
-      layers[i].layer.addTo(map);
-    }
     layersControl.addBaseLayer(layers[i].layer, layers[i].name);
   }
 
+  layers[0].layer.addTo(map);
+
   $("#" + divName).on("resized", function () {
     map.invalidateSize();
   });
@@ -124,11 +129,15 @@ function getUserIcon(url) {
 }
 
 function addObjectToMap(object, zoom, callback) {
+  if (objectLayer) {
+    map.removeLayer(objectLayer);
+  }
+
   $.ajax({
     url: OSM.apiUrl(object),
     dataType: "xml",
     success: function (xml) {
-      var layer = new L.OSM.DataLayer(xml, {
+      objectLayer = new L.OSM.DataLayer(xml, {
         style: {
           strokeColor: "blue",
           strokeWidth: 3,
@@ -139,7 +148,7 @@ function addObjectToMap(object, zoom, callback) {
         }
       });
 
-      var bounds = layer.getBounds();
+      var bounds = objectLayer.getBounds();
 
       if (zoom) {
         map.fitBounds(bounds);
@@ -149,7 +158,7 @@ function addObjectToMap(object, zoom, callback) {
         callback(bounds);
       }
 
-      layer.addTo(map);
+      objectLayer.addTo(map);
     }
   });
 }
@@ -185,11 +194,16 @@ function getMapLayers() {
 }
 
 function setMapLayers(layerConfig) {
+  var foundLayer = false;
   for (var i = 0; i < layers.length; i++) {
-    if (~layerConfig.indexOf(layers[i].layerCode)) {
+    if (layerConfig.indexOf(layers[i].layerCode) >= 0) {
       map.addLayer(layers[i].layer);
+      foundLayer = true;
     } else {
       map.removeLayer(layers[i].layer);
     }
   }
+  if (!foundLayer) {
+    map.addLayer(layers[0].layer);
+  }
 }