]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/leaflet.map.js
Refactor embed.js.erb with layers.yml
[rails.git] / app / assets / javascripts / leaflet.map.js
index 0f4272a65a31c034d551e5c9a9a07d6c5a24e801..10ce9471580b038c776c232c3a953c6d499c5559 100644 (file)
@@ -115,19 +115,18 @@ L.OSM.Map = L.Map.extend({
   },
 
   updateLayers: function (layerParam) {
-    var layers = layerParam || "M";
+    const oldBaseLayer = this.getMapBaseLayer();
+    let newBaseLayer;
 
-    for (let i = this.baseLayers.length - 1; i >= 0; i--) {
-      if (layers.indexOf(this.baseLayers[i].options.code) === -1) {
-        this.removeLayer(this.baseLayers[i]);
+    for (const layer of this.baseLayers) {
+      if (!newBaseLayer || layerParam.includes(layer.options.code)) {
+        newBaseLayer = layer;
       }
     }
 
-    for (let i = this.baseLayers.length - 1; i >= 0; i--) {
-      if (layers.indexOf(this.baseLayers[i].options.code) >= 0 || i === 0) {
-        this.addLayer(this.baseLayers[i]);
-        return;
-      }
+    if (newBaseLayer !== oldBaseLayer) {
+      if (oldBaseLayer) this.removeLayer(oldBaseLayer);
+      if (newBaseLayer) this.addLayer(newBaseLayer);
     }
   },
 
@@ -312,8 +311,8 @@ L.OSM.Map = L.Map.extend({
       var map = this;
       this._objectLoader = $.ajax({
         url: OSM.apiUrl(object),
-        dataType: "xml",
-        success: function (xml) {
+        dataType: "json",
+        success: function (data) {
           map._object = object;
 
           map._objectLayer = new L.OSM.DataLayer(null, {
@@ -335,7 +334,7 @@ L.OSM.Map = L.Map.extend({
             }
           };
 
-          map._objectLayer.addData(xml);
+          map._objectLayer.addData(data);
           map._objectLayer.addTo(map);
 
           if (callback) callback(map._objectLayer.getBounds());