]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/leaflet.extend.js.erb
Fix some Leaflet asset loading issues
[rails.git] / app / assets / javascripts / leaflet.extend.js.erb
index f1abfb4bab61de9144a1bf91255d5d8ff07d88d7..966d0276b33c6827553dbcaaa9b1366bf4a77584 100644 (file)
@@ -29,28 +29,34 @@ L.extend(L.Map.prototype, {
   },
 
   getUrl: function(marker) {
-    var center = this.getCenter(),
-        zoom = this.getZoom(),
-        toZoom = zoomPrecision(zoom),
-        params = {
-          lat: toZoom(center.lat),
-          lon: toZoom(center.lng),
-          zoom: zoom,
-          layers: this.getLayersCode()
-        };
+    var precision = zoomPrecision(this.getZoom()),
+        params = {};
 
     if (marker && this.hasLayer(marker)) {
-      params.mlat = toZoom(marker.getLatLng().lat);
-      params.mlon = toZoom(marker.getLatLng().lng);
+      var latLng = marker.getLatLng().wrap();
+      params.mlat = latLng.lat.toFixed(precision);
+      params.mlon = latLng.lng.toFixed(precision);
+    }
+
+    if (this._object) {
+      params[this._object.type] = this._object.id;
     }
 
-    return 'http://' + OSM.SERVER_URL + '/?' + querystring.stringify(params);
+    var url = 'http://' + OSM.SERVER_URL + '/',
+      query = querystring.stringify(params),
+      hash = OSM.formatHash(this);
+
+    if (query) url += '?' + query;
+    if (hash) url += hash;
+
+    return url;
   },
 
   getShortUrl: function(marker) {
     var zoom = this.getZoom(),
-      latLng = marker && this.hasLayer(marker) ? marker.getLatLng() : this.getCenter(),
-      str = '',
+      latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
+      str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
+        'http://osm.org/go/' : 'http://' + window.location.hostname + '/go/',
       char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
       x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
       y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
@@ -83,13 +89,89 @@ L.extend(L.Map.prototype, {
       return (x << 1) | y;
     }
 
+    var params = {};
+
     if (marker && this.hasLayer(marker)) {
-      str += '?m'
+      params.m = '';
+    }
+
+    if (this._object) {
+      params[this._object.type] = this._object.id;
+    }
+
+    var query = querystring.stringify(params);
+    if (query) {
+      str += '?' + query;
     }
 
-    return (window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
-            'http://osm.org/go/' : 'http://' + window.location.hostname + '/go/') + str;
+    return str;
+  },
+
+  addObject: function(object, options) {
+    this._object = object;
+
+    if (this._objectLoader) this._objectLoader.abort();
+    if (this._objectLayer) this.removeLayer(this._objectLayer);
+
+    var map = this;
+    this._objectLoader = $.ajax({
+      url: OSM.apiUrl(object),
+      dataType: "xml",
+      success: function (xml) {
+        map._objectLayer = new L.OSM.DataLayer(null, {
+          styles: {
+            node: options.style,
+            way: options.style,
+            area: options.style
+          }
+        });
+
+        map._objectLayer.interestingNode = function (node, ways, relations) {
+          if (object.type === "node") {
+            return true;
+          } else if (object.type === "relation") {
+            for (var i = 0; i < relations.length; i++)
+              if (relations[i].members.indexOf(node) != -1)
+                return true;
+          } else {
+            return false;
+          }
+        };
+
+        map._objectLayer.addData(xml);
+
+        if (options.zoom) map.fitBounds(map._objectLayer.getBounds());
+        if (options.callback) options.callback(map._objectLayer.getBounds());
+
+        map._objectLayer.addTo(map);
+      }
+    });
+  },
+
+  removeObject: function() {
+    this._object = null;
+    if (this._objectLoader) this._objectLoader.abort();
+    if (this._objectLayer) this.removeLayer(this._objectLayer);
+  }
+});
+
+L.Icon.Default.imagePath = "/images";
+
+L.Icon.Default.imageUrls = {
+  "/images/marker-icon.png": "<%= asset_path("images/marker-icon.png") %>",
+  "/images/marker-icon-2x.png": "<%= asset_path("images/marker-icon-2x.png") %>",
+  "/images/marker-shadow.png": "<%= asset_path("images/marker-shadow.png") %>",
+  "/images/marker-shadow-2x.png": "<%= asset_path("images/marker-shadow-2x.png") %>"
+};
+
+L.extend(L.Icon.Default.prototype, {
+  _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
+
+  _getIconUrl:  function (name) {
+    var url = this._oldGetIconUrl(name);
+    return L.Icon.Default.imageUrls[url];
   }
 });
 
-L.Icon.Default.imagePath = <%= "#{asset_prefix}/images".to_json %>;
+L.Hash.prototype.parseHash = OSM.parseHash;
+L.Hash.prototype.formatHash = OSM.formatHash;