Leaflet/src/layer/ImageOverlay.js

142 lines
3.3 KiB
JavaScript
Raw Normal View History

/*
* L.ImageOverlay is used to overlay images over the map (to specific geographical bounds).
*/
2010-09-27 22:02:00 +08:00
L.ImageOverlay = L.Class.extend({
includes: L.Mixin.Events,
options: {
opacity: 1
},
2012-07-05 19:45:06 +08:00
initialize: function (url, bounds, options) { // (String, LatLngBounds, Object)
2010-09-27 22:02:00 +08:00
this._url = url;
2012-07-03 22:06:44 +08:00
this._bounds = L.latLngBounds(bounds);
L.setOptions(this, options);
2010-09-27 22:02:00 +08:00
},
2011-12-09 22:51:31 +08:00
onAdd: function (map) {
2010-09-27 22:02:00 +08:00
this._map = map;
if (!this._image) {
this._initImage();
}
2012-02-15 19:17:25 +08:00
map._panes.overlayPane.appendChild(this._image);
map.on('viewreset', this._reset, this);
if (map.options.zoomAnimation && L.Browser.any3d) {
map.on('zoomanim', this._animateZoom, this);
}
this._reset();
},
2011-12-09 22:51:31 +08:00
onRemove: function (map) {
2013-12-05 20:35:39 +08:00
L.DomUtil.remove(this._image);
2012-07-03 22:06:44 +08:00
map.off('viewreset', this._reset, this);
if (map.options.zoomAnimation) {
map.off('zoomanim', this._animateZoom, this);
}
},
addTo: function (map) {
map.addLayer(this);
return this;
},
setOpacity: function (opacity) {
this.options.opacity = opacity;
this._updateOpacity();
2012-07-24 06:42:57 +08:00
return this;
},
// TODO remove bringToFront/bringToBack duplication from TileLayer/Path
bringToFront: function () {
if (this._image) {
this._map._panes.overlayPane.appendChild(this._image);
}
return this;
},
bringToBack: function () {
var pane = this._map._panes.overlayPane;
if (this._image) {
pane.insertBefore(this._image, pane.firstChild);
}
return this;
},
setUrl: function (url) {
2013-06-29 04:24:31 +08:00
this._url = url;
this._image.src = this._url;
},
getAttribution: function () {
return this.options.attribution;
},
2011-12-09 22:51:31 +08:00
_initImage: function () {
this._image = L.DomUtil.create('img', 'leaflet-image-layer');
if (this._map.options.zoomAnimation && L.Browser.any3d) {
L.DomUtil.addClass(this._image, 'leaflet-zoom-animated');
} else {
L.DomUtil.addClass(this._image, 'leaflet-zoom-hide');
}
this._updateOpacity();
2011-03-22 23:58:35 +08:00
//TODO createImage util method to remove duplication
L.extend(this._image, {
2010-09-27 22:02:00 +08:00
galleryimg: 'no',
onselectstart: L.Util.falseFn,
onmousemove: L.Util.falseFn,
onload: L.bind(this._onImageLoad, this),
2010-09-27 22:02:00 +08:00
src: this._url
});
},
_animateZoom: function (e) {
var map = this._map,
2012-11-08 00:46:59 +08:00
image = this._image,
scale = map.getZoomScale(e.zoom),
nw = this._bounds.getNorthWest(),
se = this._bounds.getSouthEast(),
topLeft = map._latLngToNewLayerPoint(nw, e.zoom, e.center),
size = map._latLngToNewLayerPoint(se, e.zoom, e.center)._subtract(topLeft),
2012-12-12 20:20:08 +08:00
origin = topLeft._add(size._multiplyBy((1 / 2) * (1 - 1 / scale)));
2012-11-08 00:46:59 +08:00
image.style[L.DomUtil.TRANSFORM] =
L.DomUtil.getTranslateString(origin) + ' scale(' + scale + ') ';
},
2011-12-09 22:51:31 +08:00
_reset: function () {
2012-02-15 19:17:25 +08:00
var image = this._image,
topLeft = this._map.latLngToLayerPoint(this._bounds.getNorthWest()),
size = this._map.latLngToLayerPoint(this._bounds.getSouthEast())._subtract(topLeft);
2012-02-15 19:17:25 +08:00
L.DomUtil.setPosition(image, topLeft);
2012-02-15 19:17:25 +08:00
image.style.width = size.x + 'px';
image.style.height = size.y + 'px';
2010-09-27 22:02:00 +08:00
},
2011-12-09 22:51:31 +08:00
_onImageLoad: function () {
this.fire('load');
},
_updateOpacity: function () {
L.DomUtil.setOpacity(this._image, this.options.opacity);
2010-09-27 22:02:00 +08:00
}
});
2012-07-05 19:45:06 +08:00
L.imageOverlay = function (url, bounds, options) {
return new L.ImageOverlay(url, bounds, options);
};