2012-07-16 11:28:13 +08:00
|
|
|
/*
|
|
|
|
* L.Map is the central class of the API - it is used to create a map.
|
|
|
|
*/
|
|
|
|
|
|
|
|
L.Map = L.Class.extend({
|
|
|
|
|
|
|
|
includes: L.Mixin.Events,
|
|
|
|
|
|
|
|
options: {
|
|
|
|
crs: L.CRS.EPSG3857,
|
|
|
|
|
|
|
|
/*
|
|
|
|
center: LatLng,
|
|
|
|
zoom: Number,
|
|
|
|
layers: Array,
|
|
|
|
*/
|
|
|
|
|
|
|
|
fadeAnimation: L.DomUtil.TRANSITION && !L.Browser.android23,
|
|
|
|
trackResize: true,
|
|
|
|
markerZoomAnimation: L.DomUtil.TRANSITION && L.Browser.any3d
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function (id, options) { // (HTMLElement or String, Object)
|
2012-11-08 02:30:56 +08:00
|
|
|
options = L.setOptions(this, options);
|
2012-07-16 11:28:13 +08:00
|
|
|
|
|
|
|
this._initContainer(id);
|
|
|
|
this._initLayout();
|
2012-12-12 23:48:30 +08:00
|
|
|
this.callInitHooks();
|
2012-07-16 11:28:13 +08:00
|
|
|
this._initEvents();
|
|
|
|
|
|
|
|
if (options.maxBounds) {
|
|
|
|
this.setMaxBounds(options.maxBounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.center && options.zoom !== undefined) {
|
|
|
|
this.setView(L.latLng(options.center), options.zoom, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._initLayers(options.layers);
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// public methods that modify map state
|
|
|
|
|
|
|
|
// replaced by animation-powered implementation in Map.PanAnimation.js
|
|
|
|
setView: function (center, zoom) {
|
|
|
|
this._resetView(L.latLng(center), this._limitZoom(zoom));
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
setZoom: function (zoom) { // (Number)
|
|
|
|
return this.setView(this.getCenter(), zoom);
|
|
|
|
},
|
|
|
|
|
2012-08-11 04:11:28 +08:00
|
|
|
zoomIn: function (delta) {
|
|
|
|
return this.setZoom(this._zoom + (delta || 1));
|
2012-07-16 11:28:13 +08:00
|
|
|
},
|
|
|
|
|
2012-08-11 04:11:28 +08:00
|
|
|
zoomOut: function (delta) {
|
|
|
|
return this.setZoom(this._zoom - (delta || 1));
|
2012-07-16 11:28:13 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
fitBounds: function (bounds) { // (LatLngBounds)
|
|
|
|
var zoom = this.getBoundsZoom(bounds);
|
|
|
|
return this.setView(L.latLngBounds(bounds).getCenter(), zoom);
|
|
|
|
},
|
|
|
|
|
|
|
|
fitWorld: function () {
|
|
|
|
var sw = new L.LatLng(-60, -170),
|
|
|
|
ne = new L.LatLng(85, 179);
|
|
|
|
|
|
|
|
return this.fitBounds(new L.LatLngBounds(sw, ne));
|
|
|
|
},
|
|
|
|
|
|
|
|
panTo: function (center) { // (LatLng)
|
|
|
|
return this.setView(center, this._zoom);
|
|
|
|
},
|
|
|
|
|
|
|
|
panBy: function (offset) { // (Point)
|
|
|
|
// replaced with animated panBy in Map.Animation.js
|
|
|
|
this.fire('movestart');
|
|
|
|
|
|
|
|
this._rawPanBy(L.point(offset));
|
|
|
|
|
|
|
|
this.fire('move');
|
|
|
|
return this.fire('moveend');
|
|
|
|
},
|
|
|
|
|
|
|
|
setMaxBounds: function (bounds) {
|
|
|
|
bounds = L.latLngBounds(bounds);
|
|
|
|
|
|
|
|
this.options.maxBounds = bounds;
|
|
|
|
|
|
|
|
if (!bounds) {
|
|
|
|
this._boundsMinZoom = null;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
var minZoom = this.getBoundsZoom(bounds, true);
|
|
|
|
|
|
|
|
this._boundsMinZoom = minZoom;
|
|
|
|
|
|
|
|
if (this._loaded) {
|
|
|
|
if (this._zoom < minZoom) {
|
|
|
|
this.setView(bounds.getCenter(), minZoom);
|
|
|
|
} else {
|
|
|
|
this.panInsideBounds(bounds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
panInsideBounds: function (bounds) {
|
|
|
|
bounds = L.latLngBounds(bounds);
|
|
|
|
|
|
|
|
var viewBounds = this.getBounds(),
|
|
|
|
viewSw = this.project(viewBounds.getSouthWest()),
|
|
|
|
viewNe = this.project(viewBounds.getNorthEast()),
|
|
|
|
sw = this.project(bounds.getSouthWest()),
|
|
|
|
ne = this.project(bounds.getNorthEast()),
|
|
|
|
dx = 0,
|
|
|
|
dy = 0;
|
|
|
|
|
|
|
|
if (viewNe.y < ne.y) { // north
|
|
|
|
dy = ne.y - viewNe.y;
|
|
|
|
}
|
|
|
|
if (viewNe.x > ne.x) { // east
|
|
|
|
dx = ne.x - viewNe.x;
|
|
|
|
}
|
|
|
|
if (viewSw.y > sw.y) { // south
|
|
|
|
dy = sw.y - viewSw.y;
|
|
|
|
}
|
|
|
|
if (viewSw.x < sw.x) { // west
|
|
|
|
dx = sw.x - viewSw.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.panBy(new L.Point(dx, dy, true));
|
|
|
|
},
|
|
|
|
|
2012-07-27 17:32:24 +08:00
|
|
|
addLayer: function (layer) {
|
2012-07-16 11:28:13 +08:00
|
|
|
// TODO method is too big, refactor
|
|
|
|
|
2012-11-08 02:30:56 +08:00
|
|
|
var id = L.stamp(layer);
|
2012-07-16 11:28:13 +08:00
|
|
|
|
|
|
|
if (this._layers[id]) { return this; }
|
|
|
|
|
|
|
|
this._layers[id] = layer;
|
|
|
|
|
|
|
|
// TODO getMaxZoom, getMinZoom in ILayer (instead of options)
|
2012-11-30 04:43:40 +08:00
|
|
|
if (layer.options && (!isNaN(layer.options.maxZoom) || !isNaN(layer.options.minZoom))) {
|
2012-11-19 05:07:27 +08:00
|
|
|
this._zoomBoundLayers[id] = layer;
|
|
|
|
this._updateZoomLevels();
|
2012-07-16 11:28:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO looks ugly, refactor!!!
|
|
|
|
if (this.options.zoomAnimation && L.TileLayer && (layer instanceof L.TileLayer)) {
|
|
|
|
this._tileLayersNum++;
|
|
|
|
this._tileLayersToLoad++;
|
|
|
|
layer.on('load', this._onTileLayerLoad, this);
|
|
|
|
}
|
|
|
|
|
2012-10-13 03:49:12 +08:00
|
|
|
this.whenReady(function () {
|
2012-07-27 17:32:24 +08:00
|
|
|
layer.onAdd(this);
|
2012-07-16 11:28:13 +08:00
|
|
|
this.fire('layeradd', {layer: layer});
|
2012-10-13 03:49:12 +08:00
|
|
|
}, this);
|
2012-07-16 11:28:13 +08:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
removeLayer: function (layer) {
|
2012-11-08 02:30:56 +08:00
|
|
|
var id = L.stamp(layer);
|
2012-07-16 11:28:13 +08:00
|
|
|
|
|
|
|
if (!this._layers[id]) { return; }
|
|
|
|
|
|
|
|
layer.onRemove(this);
|
|
|
|
|
|
|
|
delete this._layers[id];
|
2012-11-30 04:43:40 +08:00
|
|
|
if (this._zoomBoundLayers[id]) {
|
2012-11-19 05:07:27 +08:00
|
|
|
delete this._zoomBoundLayers[id];
|
|
|
|
this._updateZoomLevels();
|
|
|
|
}
|
2012-07-16 11:28:13 +08:00
|
|
|
|
|
|
|
// TODO looks ugly, refactor
|
|
|
|
if (this.options.zoomAnimation && L.TileLayer && (layer instanceof L.TileLayer)) {
|
|
|
|
this._tileLayersNum--;
|
|
|
|
this._tileLayersToLoad--;
|
|
|
|
layer.off('load', this._onTileLayerLoad, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.fire('layerremove', {layer: layer});
|
|
|
|
},
|
|
|
|
|
|
|
|
hasLayer: function (layer) {
|
2013-01-22 06:22:01 +08:00
|
|
|
if (!layer) { return false; }
|
|
|
|
|
2012-11-08 02:30:56 +08:00
|
|
|
var id = L.stamp(layer);
|
2012-07-16 11:28:13 +08:00
|
|
|
return this._layers.hasOwnProperty(id);
|
|
|
|
},
|
|
|
|
|
2012-07-31 07:59:06 +08:00
|
|
|
invalidateSize: function (animate) {
|
2012-07-16 11:28:13 +08:00
|
|
|
var oldSize = this.getSize();
|
|
|
|
|
|
|
|
this._sizeChanged = true;
|
|
|
|
|
|
|
|
if (this.options.maxBounds) {
|
|
|
|
this.setMaxBounds(this.options.maxBounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._loaded) { return this; }
|
|
|
|
|
2012-08-11 03:40:55 +08:00
|
|
|
var offset = oldSize._subtract(this.getSize())._divideBy(2)._round();
|
2012-08-06 18:36:52 +08:00
|
|
|
|
2013-02-14 04:38:33 +08:00
|
|
|
if ((offset.x !== 0) || (offset.y !== 0)) {
|
|
|
|
if (animate === true) {
|
|
|
|
this.panBy(offset);
|
|
|
|
} else {
|
|
|
|
this._rawPanBy(offset);
|
2012-07-16 11:28:13 +08:00
|
|
|
|
2013-02-14 04:38:33 +08:00
|
|
|
this.fire('move');
|
2012-07-16 11:28:13 +08:00
|
|
|
|
2013-02-14 04:38:33 +08:00
|
|
|
clearTimeout(this._sizeTimer);
|
|
|
|
this._sizeTimer = setTimeout(L.bind(this.fire, this, 'moveend'), 200);
|
|
|
|
}
|
2012-07-31 07:59:06 +08:00
|
|
|
}
|
2012-07-16 11:28:13 +08:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
// TODO handler.addTo
|
|
|
|
addHandler: function (name, HandlerClass) {
|
|
|
|
if (!HandlerClass) { return; }
|
|
|
|
|
|
|
|
this[name] = new HandlerClass(this);
|
|
|
|
|
|
|
|
if (this.options[name]) {
|
|
|
|
this[name].enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2013-02-20 09:35:19 +08:00
|
|
|
remove: function () {
|
|
|
|
this._initEvents('off');
|
|
|
|
delete this._container._leaflet;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2012-07-16 11:28:13 +08:00
|
|
|
|
|
|
|
// public methods for getting map state
|
|
|
|
|
|
|
|
getCenter: function () { // (Boolean) -> LatLng
|
2013-02-21 02:11:47 +08:00
|
|
|
this._checkIfLoaded();
|
|
|
|
|
|
|
|
if (!this._moved()) {
|
2013-02-21 02:54:12 +08:00
|
|
|
return this._initialCenter;
|
2013-02-21 02:11:47 +08:00
|
|
|
}
|
2012-07-16 11:28:13 +08:00
|
|
|
return this.layerPointToLatLng(this._getCenterLayerPoint());
|
|
|
|
},
|
|
|
|
|
|
|
|
getZoom: function () {
|
|
|
|
return this._zoom;
|
|
|
|
},
|
|
|
|
|
|
|
|
getBounds: function () {
|
|
|
|
var bounds = this.getPixelBounds(),
|
|
|
|
sw = this.unproject(bounds.getBottomLeft()),
|
|
|
|
ne = this.unproject(bounds.getTopRight());
|
|
|
|
|
|
|
|
return new L.LatLngBounds(sw, ne);
|
|
|
|
},
|
|
|
|
|
|
|
|
getMinZoom: function () {
|
|
|
|
var z1 = this.options.minZoom || 0,
|
|
|
|
z2 = this._layersMinZoom || 0,
|
|
|
|
z3 = this._boundsMinZoom || 0;
|
|
|
|
|
|
|
|
return Math.max(z1, z2, z3);
|
|
|
|
},
|
|
|
|
|
|
|
|
getMaxZoom: function () {
|
|
|
|
var z1 = this.options.maxZoom === undefined ? Infinity : this.options.maxZoom,
|
|
|
|
z2 = this._layersMaxZoom === undefined ? Infinity : this._layersMaxZoom;
|
|
|
|
|
|
|
|
return Math.min(z1, z2);
|
|
|
|
},
|
|
|
|
|
|
|
|
getBoundsZoom: function (bounds, inside) { // (LatLngBounds, Boolean) -> Number
|
|
|
|
bounds = L.latLngBounds(bounds);
|
|
|
|
|
|
|
|
var size = this.getSize(),
|
|
|
|
zoom = this.options.minZoom || 0,
|
|
|
|
maxZoom = this.getMaxZoom(),
|
|
|
|
ne = bounds.getNorthEast(),
|
|
|
|
sw = bounds.getSouthWest(),
|
|
|
|
boundsSize,
|
|
|
|
nePoint,
|
|
|
|
swPoint,
|
|
|
|
zoomNotFound = true;
|
|
|
|
|
|
|
|
if (inside) {
|
|
|
|
zoom--;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
zoom++;
|
|
|
|
nePoint = this.project(ne, zoom);
|
|
|
|
swPoint = this.project(sw, zoom);
|
2012-11-07 20:13:06 +08:00
|
|
|
|
|
|
|
boundsSize = new L.Point(
|
|
|
|
Math.abs(nePoint.x - swPoint.x),
|
|
|
|
Math.abs(swPoint.y - nePoint.y));
|
2012-07-16 11:28:13 +08:00
|
|
|
|
|
|
|
if (!inside) {
|
|
|
|
zoomNotFound = boundsSize.x <= size.x && boundsSize.y <= size.y;
|
|
|
|
} else {
|
|
|
|
zoomNotFound = boundsSize.x < size.x || boundsSize.y < size.y;
|
|
|
|
}
|
|
|
|
} while (zoomNotFound && zoom <= maxZoom);
|
|
|
|
|
|
|
|
if (zoomNotFound && inside) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return inside ? zoom : zoom - 1;
|
|
|
|
},
|
|
|
|
|
|
|
|
getSize: function () {
|
|
|
|
if (!this._size || this._sizeChanged) {
|
|
|
|
this._size = new L.Point(
|
|
|
|
this._container.clientWidth,
|
|
|
|
this._container.clientHeight);
|
|
|
|
|
|
|
|
this._sizeChanged = false;
|
|
|
|
}
|
2012-08-11 03:40:55 +08:00
|
|
|
return this._size.clone();
|
2012-07-16 11:28:13 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
getPixelBounds: function () {
|
|
|
|
var topLeftPoint = this._getTopLeftPoint();
|
|
|
|
return new L.Bounds(topLeftPoint, topLeftPoint.add(this.getSize()));
|
|
|
|
},
|
|
|
|
|
|
|
|
getPixelOrigin: function () {
|
2013-02-21 02:11:47 +08:00
|
|
|
this._checkIfLoaded();
|
2012-07-16 11:28:13 +08:00
|
|
|
return this._initialTopLeftPoint;
|
|
|
|
},
|
|
|
|
|
|
|
|
getPanes: function () {
|
|
|
|
return this._panes;
|
|
|
|
},
|
|
|
|
|
|
|
|
getContainer: function () {
|
|
|
|
return this._container;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// TODO replace with universal implementation after refactoring projections
|
|
|
|
|
|
|
|
getZoomScale: function (toZoom) {
|
|
|
|
var crs = this.options.crs;
|
|
|
|
return crs.scale(toZoom) / crs.scale(this._zoom);
|
|
|
|
},
|
|
|
|
|
|
|
|
getScaleZoom: function (scale) {
|
|
|
|
return this._zoom + (Math.log(scale) / Math.LN2);
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// conversion methods
|
|
|
|
|
|
|
|
project: function (latlng, zoom) { // (LatLng[, Number]) -> Point
|
|
|
|
zoom = zoom === undefined ? this._zoom : zoom;
|
|
|
|
return this.options.crs.latLngToPoint(L.latLng(latlng), zoom);
|
|
|
|
},
|
|
|
|
|
|
|
|
unproject: function (point, zoom) { // (Point[, Number]) -> LatLng
|
|
|
|
zoom = zoom === undefined ? this._zoom : zoom;
|
|
|
|
return this.options.crs.pointToLatLng(L.point(point), zoom);
|
|
|
|
},
|
|
|
|
|
|
|
|
layerPointToLatLng: function (point) { // (Point)
|
2013-02-19 22:54:29 +08:00
|
|
|
var projectedPoint = L.point(point).add(this.getPixelOrigin());
|
2012-07-16 11:28:13 +08:00
|
|
|
return this.unproject(projectedPoint);
|
|
|
|
},
|
|
|
|
|
|
|
|
latLngToLayerPoint: function (latlng) { // (LatLng)
|
|
|
|
var projectedPoint = this.project(L.latLng(latlng))._round();
|
2013-02-19 22:54:29 +08:00
|
|
|
return projectedPoint._subtract(this.getPixelOrigin());
|
2012-07-16 11:28:13 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
containerPointToLayerPoint: function (point) { // (Point)
|
|
|
|
return L.point(point).subtract(this._getMapPanePos());
|
|
|
|
},
|
|
|
|
|
|
|
|
layerPointToContainerPoint: function (point) { // (Point)
|
|
|
|
return L.point(point).add(this._getMapPanePos());
|
|
|
|
},
|
|
|
|
|
|
|
|
containerPointToLatLng: function (point) {
|
|
|
|
var layerPoint = this.containerPointToLayerPoint(L.point(point));
|
|
|
|
return this.layerPointToLatLng(layerPoint);
|
|
|
|
},
|
|
|
|
|
|
|
|
latLngToContainerPoint: function (latlng) {
|
|
|
|
return this.layerPointToContainerPoint(this.latLngToLayerPoint(L.latLng(latlng)));
|
|
|
|
},
|
|
|
|
|
|
|
|
mouseEventToContainerPoint: function (e) { // (MouseEvent)
|
|
|
|
return L.DomEvent.getMousePosition(e, this._container);
|
|
|
|
},
|
|
|
|
|
|
|
|
mouseEventToLayerPoint: function (e) { // (MouseEvent)
|
|
|
|
return this.containerPointToLayerPoint(this.mouseEventToContainerPoint(e));
|
|
|
|
},
|
|
|
|
|
|
|
|
mouseEventToLatLng: function (e) { // (MouseEvent)
|
|
|
|
return this.layerPointToLatLng(this.mouseEventToLayerPoint(e));
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// map initialization methods
|
|
|
|
|
|
|
|
_initContainer: function (id) {
|
|
|
|
var container = this._container = L.DomUtil.get(id);
|
|
|
|
|
|
|
|
if (container._leaflet) {
|
|
|
|
throw new Error("Map container is already initialized.");
|
|
|
|
}
|
|
|
|
|
|
|
|
container._leaflet = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
_initLayout: function () {
|
|
|
|
var container = this._container;
|
|
|
|
|
|
|
|
L.DomUtil.addClass(container, 'leaflet-container');
|
|
|
|
|
|
|
|
if (L.Browser.touch) {
|
|
|
|
L.DomUtil.addClass(container, 'leaflet-touch');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.fadeAnimation) {
|
|
|
|
L.DomUtil.addClass(container, 'leaflet-fade-anim');
|
|
|
|
}
|
|
|
|
|
|
|
|
var position = L.DomUtil.getStyle(container, 'position');
|
|
|
|
|
|
|
|
if (position !== 'absolute' && position !== 'relative' && position !== 'fixed') {
|
|
|
|
container.style.position = 'relative';
|
|
|
|
}
|
|
|
|
|
|
|
|
this._initPanes();
|
|
|
|
|
|
|
|
if (this._initControlPos) {
|
|
|
|
this._initControlPos();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_initPanes: function () {
|
|
|
|
var panes = this._panes = {};
|
|
|
|
|
|
|
|
this._mapPane = panes.mapPane = this._createPane('leaflet-map-pane', this._container);
|
|
|
|
|
|
|
|
this._tilePane = panes.tilePane = this._createPane('leaflet-tile-pane', this._mapPane);
|
2012-11-07 20:13:06 +08:00
|
|
|
panes.objectsPane = this._createPane('leaflet-objects-pane', this._mapPane);
|
2012-07-16 11:28:13 +08:00
|
|
|
panes.shadowPane = this._createPane('leaflet-shadow-pane');
|
|
|
|
panes.overlayPane = this._createPane('leaflet-overlay-pane');
|
|
|
|
panes.markerPane = this._createPane('leaflet-marker-pane');
|
|
|
|
panes.popupPane = this._createPane('leaflet-popup-pane');
|
|
|
|
|
|
|
|
var zoomHide = ' leaflet-zoom-hide';
|
|
|
|
|
|
|
|
if (!this.options.markerZoomAnimation) {
|
|
|
|
L.DomUtil.addClass(panes.markerPane, zoomHide);
|
|
|
|
L.DomUtil.addClass(panes.shadowPane, zoomHide);
|
|
|
|
L.DomUtil.addClass(panes.popupPane, zoomHide);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_createPane: function (className, container) {
|
2012-11-07 20:13:06 +08:00
|
|
|
return L.DomUtil.create('div', className, container || this._panes.objectsPane);
|
2012-07-16 11:28:13 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_initLayers: function (layers) {
|
2013-01-22 06:22:01 +08:00
|
|
|
layers = layers ? (L.Util.isArray(layers) ? layers : [layers]) : [];
|
2012-07-16 11:28:13 +08:00
|
|
|
|
|
|
|
this._layers = {};
|
2012-11-19 05:07:27 +08:00
|
|
|
this._zoomBoundLayers = {};
|
2012-07-16 11:28:13 +08:00
|
|
|
this._tileLayersNum = 0;
|
|
|
|
|
|
|
|
var i, len;
|
|
|
|
|
|
|
|
for (i = 0, len = layers.length; i < len; i++) {
|
|
|
|
this.addLayer(layers[i]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// private methods that modify map state
|
|
|
|
|
|
|
|
_resetView: function (center, zoom, preserveMapOffset, afterZoomAnim) {
|
|
|
|
|
|
|
|
var zoomChanged = (this._zoom !== zoom);
|
|
|
|
|
|
|
|
if (!afterZoomAnim) {
|
|
|
|
this.fire('movestart');
|
|
|
|
|
|
|
|
if (zoomChanged) {
|
|
|
|
this.fire('zoomstart');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._zoom = zoom;
|
2013-02-21 02:11:47 +08:00
|
|
|
this._initialCenter = center;
|
2012-07-16 11:28:13 +08:00
|
|
|
|
|
|
|
this._initialTopLeftPoint = this._getNewTopLeftPoint(center);
|
|
|
|
|
|
|
|
if (!preserveMapOffset) {
|
|
|
|
L.DomUtil.setPosition(this._mapPane, new L.Point(0, 0));
|
|
|
|
} else {
|
|
|
|
this._initialTopLeftPoint._add(this._getMapPanePos());
|
|
|
|
}
|
|
|
|
|
|
|
|
this._tileLayersToLoad = this._tileLayersNum;
|
|
|
|
|
2012-10-05 05:02:46 +08:00
|
|
|
var loading = !this._loaded;
|
|
|
|
this._loaded = true;
|
|
|
|
|
2012-07-16 11:28:13 +08:00
|
|
|
this.fire('viewreset', {hard: !preserveMapOffset});
|
|
|
|
|
|
|
|
this.fire('move');
|
|
|
|
|
|
|
|
if (zoomChanged || afterZoomAnim) {
|
|
|
|
this.fire('zoomend');
|
|
|
|
}
|
|
|
|
|
2012-07-24 22:55:48 +08:00
|
|
|
this.fire('moveend', {hard: !preserveMapOffset});
|
2012-07-16 11:28:13 +08:00
|
|
|
|
2012-10-05 05:02:46 +08:00
|
|
|
if (loading) {
|
2012-07-16 11:28:13 +08:00
|
|
|
this.fire('load');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_rawPanBy: function (offset) {
|
|
|
|
L.DomUtil.setPosition(this._mapPane, this._getMapPanePos().subtract(offset));
|
|
|
|
},
|
|
|
|
|
2013-02-14 19:00:08 +08:00
|
|
|
_getZoomSpan: function () {
|
|
|
|
return this.getMaxZoom() - this.getMinZoom();
|
|
|
|
},
|
|
|
|
|
2012-11-19 05:07:27 +08:00
|
|
|
_updateZoomLevels: function () {
|
2012-11-19 11:36:13 +08:00
|
|
|
var i,
|
|
|
|
minZoom = Infinity,
|
2013-02-14 10:04:10 +08:00
|
|
|
maxZoom = -Infinity,
|
2013-02-14 19:00:08 +08:00
|
|
|
oldZoomSpan = this._getZoomSpan();
|
2012-11-19 11:36:13 +08:00
|
|
|
|
2012-11-19 05:07:27 +08:00
|
|
|
for (i in this._zoomBoundLayers) {
|
2012-11-30 04:43:40 +08:00
|
|
|
if (this._zoomBoundLayers.hasOwnProperty(i)) {
|
|
|
|
var layer = this._zoomBoundLayers[i];
|
|
|
|
if (!isNaN(layer.options.minZoom)) {
|
|
|
|
minZoom = Math.min(minZoom, layer.options.minZoom);
|
|
|
|
}
|
|
|
|
if (!isNaN(layer.options.maxZoom)) {
|
|
|
|
maxZoom = Math.max(maxZoom, layer.options.maxZoom);
|
|
|
|
}
|
2012-11-19 11:36:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 04:43:40 +08:00
|
|
|
if (i === undefined) { // we have no tilelayers
|
2012-11-19 11:36:13 +08:00
|
|
|
this._layersMaxZoom = this._layersMinZoom = undefined;
|
|
|
|
} else {
|
|
|
|
this._layersMaxZoom = maxZoom;
|
|
|
|
this._layersMinZoom = minZoom;
|
2012-11-19 05:07:27 +08:00
|
|
|
}
|
2013-02-14 10:04:10 +08:00
|
|
|
|
2013-02-14 19:00:08 +08:00
|
|
|
if (oldZoomSpan !== this._getZoomSpan()) {
|
2013-02-14 10:04:10 +08:00
|
|
|
this.fire("zoomlevelschange");
|
|
|
|
}
|
2012-11-19 05:07:27 +08:00
|
|
|
},
|
2012-07-16 11:28:13 +08:00
|
|
|
|
2013-02-21 02:11:47 +08:00
|
|
|
_checkIfLoaded: function () {
|
|
|
|
if (!this._loaded) {
|
|
|
|
throw new Error('Set map center and zoom first.');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-07-16 11:28:13 +08:00
|
|
|
// map events
|
|
|
|
|
2013-02-20 09:35:19 +08:00
|
|
|
_initEvents: function (onOff) {
|
2012-07-16 11:28:13 +08:00
|
|
|
if (!L.DomEvent) { return; }
|
|
|
|
|
2013-02-20 09:35:19 +08:00
|
|
|
onOff = onOff || 'on';
|
|
|
|
|
|
|
|
L.DomEvent[onOff](this._container, 'click', this._onMouseClick, this);
|
2012-07-16 11:28:13 +08:00
|
|
|
|
2012-11-07 20:13:06 +08:00
|
|
|
var events = ['dblclick', 'mousedown', 'mouseup', 'mouseenter',
|
|
|
|
'mouseleave', 'mousemove', 'contextmenu'],
|
|
|
|
i, len;
|
2012-07-16 11:28:13 +08:00
|
|
|
|
|
|
|
for (i = 0, len = events.length; i < len; i++) {
|
2013-02-20 09:35:19 +08:00
|
|
|
L.DomEvent[onOff](this._container, events[i], this._fireMouseEvent, this);
|
2012-07-16 11:28:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.trackResize) {
|
2013-02-20 09:35:19 +08:00
|
|
|
L.DomEvent[onOff](window, 'resize', this._onResize, this);
|
2012-07-16 11:28:13 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onResize: function () {
|
|
|
|
L.Util.cancelAnimFrame(this._resizeRequest);
|
2012-11-07 20:13:06 +08:00
|
|
|
this._resizeRequest = L.Util.requestAnimFrame(
|
|
|
|
this.invalidateSize, this, false, this._container);
|
2012-07-16 11:28:13 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_onMouseClick: function (e) {
|
|
|
|
if (!this._loaded || (this.dragging && this.dragging.moved())) { return; }
|
|
|
|
|
|
|
|
this.fire('preclick');
|
|
|
|
this._fireMouseEvent(e);
|
|
|
|
},
|
|
|
|
|
|
|
|
_fireMouseEvent: function (e) {
|
|
|
|
if (!this._loaded) { return; }
|
|
|
|
|
|
|
|
var type = e.type;
|
|
|
|
|
|
|
|
type = (type === 'mouseenter' ? 'mouseover' : (type === 'mouseleave' ? 'mouseout' : type));
|
|
|
|
|
|
|
|
if (!this.hasEventListeners(type)) { return; }
|
|
|
|
|
|
|
|
if (type === 'contextmenu') {
|
|
|
|
L.DomEvent.preventDefault(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
var containerPoint = this.mouseEventToContainerPoint(e),
|
2012-11-07 20:13:06 +08:00
|
|
|
layerPoint = this.containerPointToLayerPoint(containerPoint),
|
|
|
|
latlng = this.layerPointToLatLng(layerPoint);
|
2012-07-16 11:28:13 +08:00
|
|
|
|
|
|
|
this.fire(type, {
|
|
|
|
latlng: latlng,
|
|
|
|
layerPoint: layerPoint,
|
|
|
|
containerPoint: containerPoint,
|
|
|
|
originalEvent: e
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_onTileLayerLoad: function () {
|
|
|
|
this._tileLayersToLoad--;
|
2013-02-21 00:40:00 +08:00
|
|
|
if (this._tileLayersNum && !this._tileLayersToLoad) {
|
|
|
|
this.fire('tilelayersload');
|
2012-07-16 11:28:13 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-13 03:49:12 +08:00
|
|
|
whenReady: function (callback, context) {
|
|
|
|
if (this._loaded) {
|
|
|
|
callback.call(context || this, this);
|
|
|
|
} else {
|
|
|
|
this.on('load', callback, context);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2012-07-16 11:28:13 +08:00
|
|
|
|
|
|
|
// private methods for getting map state
|
|
|
|
|
|
|
|
_getMapPanePos: function () {
|
|
|
|
return L.DomUtil.getPosition(this._mapPane);
|
|
|
|
},
|
|
|
|
|
2013-02-21 02:11:47 +08:00
|
|
|
_moved: function () {
|
|
|
|
var pos = this._getMapPanePos();
|
|
|
|
return pos && !pos.equals(new L.Point(0, 0));
|
|
|
|
},
|
|
|
|
|
2012-07-16 11:28:13 +08:00
|
|
|
_getTopLeftPoint: function () {
|
2013-02-19 22:54:29 +08:00
|
|
|
return this.getPixelOrigin().subtract(this._getMapPanePos());
|
2012-07-16 11:28:13 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_getNewTopLeftPoint: function (center, zoom) {
|
2012-08-11 03:40:55 +08:00
|
|
|
var viewHalf = this.getSize()._divideBy(2);
|
2012-07-16 11:28:13 +08:00
|
|
|
// TODO round on display, not calculation to increase precision?
|
|
|
|
return this.project(center, zoom)._subtract(viewHalf)._round();
|
|
|
|
},
|
|
|
|
|
|
|
|
_latLngToNewLayerPoint: function (latlng, newZoom, newCenter) {
|
|
|
|
var topLeft = this._getNewTopLeftPoint(newCenter, newZoom).add(this._getMapPanePos());
|
2012-07-25 16:14:21 +08:00
|
|
|
return this.project(latlng, newZoom)._subtract(topLeft);
|
2012-07-16 11:28:13 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_getCenterLayerPoint: function () {
|
2012-08-11 03:40:55 +08:00
|
|
|
return this.containerPointToLayerPoint(this.getSize()._divideBy(2));
|
2012-07-16 11:28:13 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_getCenterOffset: function (center) {
|
|
|
|
return this.latLngToLayerPoint(center).subtract(this._getCenterLayerPoint());
|
|
|
|
},
|
|
|
|
|
|
|
|
_limitZoom: function (zoom) {
|
|
|
|
var min = this.getMinZoom(),
|
2012-11-07 20:13:06 +08:00
|
|
|
max = this.getMaxZoom();
|
2012-07-16 11:28:13 +08:00
|
|
|
|
|
|
|
return Math.max(min, Math.min(max, zoom));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
L.map = function (id, options) {
|
|
|
|
return new L.Map(id, options);
|
|
|
|
};
|