2010-09-27 22:02:00 +08:00
|
|
|
L.ImageOverlay = L.Class.extend({
|
|
|
|
includes: L.Mixin.Events,
|
2011-12-09 22:35:15 +08:00
|
|
|
|
2012-06-26 04:45:37 +08:00
|
|
|
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);
|
2012-06-26 04:45:37 +08:00
|
|
|
|
|
|
|
L.Util.setOptions(this, options);
|
2010-09-27 22:02:00 +08:00
|
|
|
},
|
2011-12-09 22:35:15 +08:00
|
|
|
|
2011-12-09 22:51:31 +08:00
|
|
|
onAdd: function (map) {
|
2010-09-27 22:02:00 +08:00
|
|
|
this._map = map;
|
2011-12-09 22:35:15 +08:00
|
|
|
|
2011-05-20 16:26:40 +08:00
|
|
|
if (!this._image) {
|
|
|
|
this._initImage();
|
|
|
|
}
|
2011-12-09 22:35:15 +08:00
|
|
|
|
2012-02-15 19:17:25 +08:00
|
|
|
map._panes.overlayPane.appendChild(this._image);
|
2011-12-09 22:35:15 +08:00
|
|
|
|
2011-05-20 16:26:40 +08:00
|
|
|
map.on('viewreset', this._reset, this);
|
2012-06-30 19:51:09 +08:00
|
|
|
|
2012-07-03 05:12:14 +08:00
|
|
|
if (map.options.zoomAnimation && L.Browser.any3d) {
|
2012-06-30 19:51:09 +08:00
|
|
|
map.on('zoomanim', this._animateZoom, this);
|
|
|
|
}
|
|
|
|
|
2011-05-20 16:26:40 +08:00
|
|
|
this._reset();
|
|
|
|
},
|
2011-12-09 22:35:15 +08:00
|
|
|
|
2011-12-09 22:51:31 +08:00
|
|
|
onRemove: function (map) {
|
2011-05-20 16:26:40 +08:00
|
|
|
map.getPanes().overlayPane.removeChild(this._image);
|
2012-07-03 22:06:44 +08:00
|
|
|
|
2011-05-20 16:26:40 +08:00
|
|
|
map.off('viewreset', this._reset, this);
|
2012-06-30 19:51:09 +08:00
|
|
|
|
|
|
|
if (map.options.zoomAnimation) {
|
|
|
|
map.off('zoomanim', this._animateZoom, this);
|
|
|
|
}
|
2011-05-20 16:26:40 +08:00
|
|
|
},
|
2011-12-09 22:35:15 +08:00
|
|
|
|
2012-07-05 21:53:32 +08:00
|
|
|
addTo: function (map) {
|
|
|
|
map.addLayer(this);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2012-06-26 04:45:37 +08:00
|
|
|
setOpacity: function (opacity) {
|
|
|
|
this.options.opacity = opacity;
|
|
|
|
this._updateOpacity();
|
2012-07-24 06:42:57 +08:00
|
|
|
return this;
|
2012-06-26 04:45:37 +08:00
|
|
|
},
|
|
|
|
|
2012-07-25 22:36:14 +08:00
|
|
|
// 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;
|
|
|
|
},
|
|
|
|
|
2011-12-09 22:51:31 +08:00
|
|
|
_initImage: function () {
|
2012-06-30 19:51:09 +08:00
|
|
|
this._image = L.DomUtil.create('img', 'leaflet-image-layer');
|
|
|
|
|
2012-07-03 05:12:14 +08:00
|
|
|
if (this._map.options.zoomAnimation && L.Browser.any3d) {
|
2012-07-05 22:59:00 +08:00
|
|
|
L.DomUtil.addClass(this._image, 'leaflet-zoom-animated');
|
2012-07-03 05:12:14 +08:00
|
|
|
} else {
|
2012-07-05 22:59:00 +08:00
|
|
|
L.DomUtil.addClass(this._image, 'leaflet-zoom-hide');
|
2012-06-30 19:51:09 +08:00
|
|
|
}
|
2011-12-09 22:35:15 +08:00
|
|
|
|
2012-06-26 04:45:37 +08:00
|
|
|
this._updateOpacity();
|
2011-12-09 22:35:15 +08:00
|
|
|
|
2011-03-22 23:58:35 +08:00
|
|
|
//TODO createImage util method to remove duplication
|
2010-09-27 22:02:00 +08:00
|
|
|
L.Util.extend(this._image, {
|
|
|
|
galleryimg: 'no',
|
|
|
|
onselectstart: L.Util.falseFn,
|
|
|
|
onmousemove: L.Util.falseFn,
|
2011-12-14 19:38:42 +08:00
|
|
|
onload: L.Util.bind(this._onImageLoad, this),
|
2010-09-27 22:02:00 +08:00
|
|
|
src: this._url
|
|
|
|
});
|
|
|
|
},
|
2011-12-09 22:35:15 +08:00
|
|
|
|
2012-06-30 19:51:09 +08:00
|
|
|
_animateZoom: function (e) {
|
2012-06-29 07:59:49 +08:00
|
|
|
var map = this._map,
|
2012-11-08 00:46:59 +08:00
|
|
|
image = this._image,
|
2012-06-30 19:51:09 +08:00
|
|
|
scale = map.getZoomScale(e.zoom),
|
2012-06-29 07:59:49 +08:00
|
|
|
nw = this._bounds.getNorthWest(),
|
|
|
|
se = this._bounds.getSouthEast(),
|
2012-08-11 03:40:55 +08:00
|
|
|
|
2012-06-30 19:51:09 +08:00
|
|
|
topLeft = map._latLngToNewLayerPoint(nw, e.zoom, e.center),
|
2012-08-11 03:40:55 +08:00
|
|
|
size = map._latLngToNewLayerPoint(se, e.zoom, e.center)._subtract(topLeft),
|
|
|
|
currentSize = map.latLngToLayerPoint(se)._subtract(map.latLngToLayerPoint(nw)),
|
|
|
|
origin = topLeft._add(size._subtract(currentSize)._divideBy(2));
|
2012-06-29 07:59:49 +08:00
|
|
|
|
2012-11-08 00:46:59 +08:00
|
|
|
image.style[L.DomUtil.TRANSFORM] =
|
|
|
|
L.DomUtil.getTranslateString(origin) + ' scale(' + scale + ') ';
|
2012-06-26 06:13:39 +08:00
|
|
|
},
|
|
|
|
|
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()),
|
2012-08-11 03:40:55 +08:00
|
|
|
size = this._map.latLngToLayerPoint(this._bounds.getSouthEast())._subtract(topLeft);
|
2011-12-09 22:35:15 +08:00
|
|
|
|
2012-02-15 19:17:25 +08:00
|
|
|
L.DomUtil.setPosition(image, topLeft);
|
2011-12-09 22:35:15 +08:00
|
|
|
|
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:35:15 +08:00
|
|
|
|
2011-12-09 22:51:31 +08:00
|
|
|
_onImageLoad: function () {
|
2011-12-14 19:38:42 +08:00
|
|
|
this.fire('load');
|
2012-06-26 04:45:37 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_updateOpacity: function () {
|
|
|
|
L.DomUtil.setOpacity(this._image, this.options.opacity);
|
2010-09-27 22:02:00 +08:00
|
|
|
}
|
2011-12-09 22:35:15 +08:00
|
|
|
});
|
2012-07-05 19:45:06 +08:00
|
|
|
|
|
|
|
L.imageOverlay = function (url, bounds, options) {
|
|
|
|
return new L.ImageOverlay(url, bounds, options);
|
2012-07-05 21:53:32 +08:00
|
|
|
};
|