more Map code

This commit is contained in:
mourner 2010-09-13 18:57:08 +03:00
parent 00717ab05b
commit 09d2ee9100

View File

@ -21,7 +21,7 @@ L.Map = L.Class.extend({
L.Util.extend(this.options, options); L.Util.extend(this.options, options);
this._initLayout(); this._initLayout();
//TODO this._initLayers(); this._initLayers(this._options.layers);
this.setView(this.options.center, this.options.zoom, true); this.setView(this.options.center, this.options.zoom, true);
}, },
@ -30,11 +30,11 @@ L.Map = L.Class.extend({
setView: function(center, zoom, forceReset) { setView: function(center, zoom, forceReset) {
zoom = this._limitZoom(zoom); zoom = this._limitZoom(zoom);
if (!forceReset) {
var zoomChanged = (this._zoom != zoom); var zoomChanged = (this._zoom != zoom);
if (!forceReset) {
// difference between the new and current centers in pixels // difference between the new and current centers in pixels
var offset = this._getCenterOffset(center); var offset = this._getNewTopLeftPoint(center).subtract(this._getTopLeftPoint());
var done = (zoomChanged ? var done = (zoomChanged ?
this._zoomToIfCenterInView(zoom, offset) : this._zoomToIfCenterInView(zoom, offset) :
@ -57,19 +57,13 @@ L.Map = L.Class.extend({
getCenter: function(unbounded) { getCenter: function(unbounded) {
var viewHalf = this.getSize().divideBy(2), var viewHalf = this.getSize().divideBy(2),
topLeftPoint = this._topLeftPoint.add(this._getMapPaneOffset()), centerPoint = this._getTopLeftPoint().add(viewHalf);
centerPoint = topLeftPoint.add(viewHalf); return this.unproject(centerPoint, this._zoom, unbounded);
return this.unproject(centerPoint, this._zoom, true);
}, },
addLayer: function(layer) { addLayer: function(layer) {
this._layers.push(layer); this._addLayer(layer);
layer.onAdd(this);
map.on('viewreset', layer.draw);
map.on('viewload', layer.load);
this.setView(this.getCenter(), this._zoom, true); this.setView(this.getCenter(), this._zoom, true);
}, },
@ -103,7 +97,6 @@ L.Map = L.Class.extend({
return new L.Bounds( return new L.Bounds(
this._topLeftPoint, this._topLeftPoint,
this._topLeftPoint.add(this.getSize())); this._topLeftPoint.add(this.getSize()));
//TODO L.Bounds
}, },
@ -126,47 +119,35 @@ L.Map = L.Class.extend({
}, },
_limitZoom: function(zoom) { _limitZoom: function(zoom) {
//TODO minZoom, maxZoom? var min = this.options.minZoom || this._layersMinZoom || 0;
return Math.max(this._minZoom, Math.min(zoom, this._maxZoom)); var max = this.options.maxZoom || this._layersMaxZoom || Infinity;
return Math.max(min, Math.min(max, zoom));
}, },
_resetView: function(center, zoom) { _resetView: function(center, zoom) {
this._zoom = zoom; this._zoom = zoom;
this._topLeftPoint = this._getNewTopLeftPoint(center); this._topLeftPoint = this._getNewTopLeftPoint(center);
this._setMapPaneOffset(new L.Point(0, 0)); L.DomUtil.setPosition(this._mapPane, new L.Point(0, 0));
this.fire('viewreset'); this.fire('viewreset');
this.fire('viewload'); this.fire('viewload');
}, },
_getCenterOffset: function(center) { _getTopLeftPoint: function() {
var oldTopLeftPoint = this._topLeftPoint.add(this._getMapPaneOffset()), var offset = L.DomUtil.getPosition(this._mapPane);
newTopLeftPoint = this._getNewTopLeftPoint(center); return this._topLeftPoint.add(offset);
return newTopLeftPoint.subtract(oldTopLeftPoint);
}, },
_getNewTopLeftPoint: function(center) { _getNewTopLeftPoint: function(center) {
var viewHalf = this.getSize().divideBy(2, true); var viewHalf = this.getSize().divideBy(2, true);
return this.project(center).subtract(viewHalf); return this.project(center).subtract(viewHalf);
}, },
_getMapPaneOffset: function() {
return L.Point(
parseInt(this._mapPane.style.left),
parseInt(this._mapPane.style.top));
//TODO translate instead of top/left for webkit
},
_setMapPaneOffset: function(offset) {
this._mapPane.style.left = offset.x + 'px';
this._mapPane.style.top = offset.y + 'px';
//TODO translate instead of top/left for webkit
},
_rawPanBy: function(offset) { _rawPanBy: function(offset) {
this._setMapPaneOffset(this._getMapPaneOffset().add(offset)); var mapPaneOffset = L.DomUtil.getPosition(this._mapPane);
L.DomUtil.setPosition(this._mapPane, mapPaneOffset.add(offset));
}, },
_panByIfClose: function(offset) { _panByIfClose: function(offset) {
@ -189,8 +170,28 @@ L.Map = L.Class.extend({
_offsetIsWithinView: function(offset, multiplyFactor) { _offsetIsWithinView: function(offset, multiplyFactor) {
var m = multiplyFactor || 1, var m = multiplyFactor || 1,
size = map.getSize(); size = this.getSize();
return (Math.abs(offset.x) <= size.width * m) && return (Math.abs(offset.x) <= size.width * m) &&
(Math.abs(offset.y) <= size.height * m); (Math.abs(offset.y) <= size.height * m);
},
_initLayers: function(layers) {
for (var i = 0, len = layers.length; i < len; i++) {
this._addLayer(layers[i]);
}
},
_addLayer: function(layer) {
this._layers.push(layer);
layer.onAdd(this);
this.on('viewreset', layer.draw);
this.on('viewload', layer.load);
this._layersMaxZoom = Math.max(this._layersMaxZoom, layer.options.maxZoomLevel);
this._layersMinZoom = Math.min(this._layersMinZoom, layer.options.minZoomLevel);
this.fire('layeradded', {layer: layer});
} }
}); });