refactor map init code

This commit is contained in:
Mourner 2011-03-22 17:58:35 +02:00
parent 60fbf680d2
commit 306e42a25c
4 changed files with 56 additions and 29 deletions

View File

@ -14,6 +14,7 @@ L.ImageOverlay = L.Class.extend({
this._image.style.visibility = 'hidden'; this._image.style.visibility = 'hidden';
//TODO opacity option //TODO opacity option
//TODO createImage util method to remove duplication
L.Util.extend(this._image, { L.Util.extend(this._image, {
galleryimg: 'no', galleryimg: 'no',
onselectstart: L.Util.falseFn, onselectstart: L.Util.falseFn,

View File

@ -12,8 +12,8 @@ L.Map = L.Class.extend({
scaling: function(zoom) { return 256 * (1 << zoom); }, scaling: function(zoom) { return 256 * (1 << zoom); },
// state // state
center: new L.LatLng(0, 0), center: null,
zoom: 0, zoom: null,
layers: [], layers: [],
//interaction //interaction
@ -43,7 +43,12 @@ L.Map = L.Class.extend({
if (L.Handler) { this._initInteraction(); } if (L.Handler) { this._initInteraction(); }
} }
this.setView(this.options.center, this.options.zoom, true); var center = this.options.center,
zoom = this.options.zoom;
if (center !== null && zoom !== null) {
this.setView(center, zoom, true);
}
var layers = this.options.layers; var layers = this.options.layers;
layers = (layers instanceof Array ? layers : [layers]); layers = (layers instanceof Array ? layers : [layers]);
@ -53,14 +58,10 @@ L.Map = L.Class.extend({
// public methods that modify map state // public methods that modify map state
// replaced by animation-powered implementation in Map.Animation.js // replaced by animation-powered implementation in Map.PanAnimation.js
setView: function(center, zoom, forceReset) { setView: function(center, zoom, forceReset) {
zoom = this._limitZoom(zoom);
var zoomChanged = (this._zoom != zoom);
// reset the map view // reset the map view
this._resetView(center, zoom); this._resetView(center, this._limitZoom(zoom));
return this; return this;
}, },
@ -101,8 +102,6 @@ L.Map = L.Class.extend({
var id = L.Util.stamp(layer); var id = L.Util.stamp(layer);
if (!this._layers[id]) { if (!this._layers[id]) {
layer.onAdd(this);
this._layers[id] = layer; this._layers[id] = layer;
if (layer.options && !isNaN(layer.options.maxZoom)) { if (layer.options && !isNaN(layer.options.maxZoom)) {
@ -113,8 +112,17 @@ L.Map = L.Class.extend({
} }
//TODO getMaxZoom, getMinZoom //TODO getMaxZoom, getMinZoom
function addLayer() {
layer.onAdd(this);
this.fire('layeradd', {layer: layer}); this.fire('layeradd', {layer: layer});
} }
if (this._loaded) {
addLayer.call(this);
} else {
this.on('load', addLayer, this);
}
}
return this; return this;
}, },
@ -207,6 +215,13 @@ L.Map = L.Class.extend({
return this._initialTopLeftPoint; return this._initialTopLeftPoint;
}, },
getPanes: function() {
return this._panes;
},
// conversion methods
mouseEventToContainerPoint: function(/*MouseEvent*/ e) { mouseEventToContainerPoint: function(/*MouseEvent*/ e) {
return L.DomEvent.getMousePosition(e, this._container); return L.DomEvent.getMousePosition(e, this._container);
}, },
@ -248,27 +263,32 @@ L.Map = L.Class.extend({
return this.options.projection.unproject(untransformedPoint, unbounded); return this.options.projection.unproject(untransformedPoint, unbounded);
}, },
getPanes: function() {
return this._panes;
},
// private methods that modify map state // private methods that modify map state
_initLayout: function() { _initLayout: function() {
this._container.className += ' leaflet-container'; var container = this._container;
var position = L.DomUtil.getStyle(this._container, 'position'); container.className += ' leaflet-container';
this._container.style.position = (position == 'absolute' ? 'absolute' : 'relative');
this._panes = {}; var position = L.DomUtil.getStyle(container, 'position');
this._mapPane = this._panes.mapPane = this._createPane('leaflet-map-pane', this._container); container.style.position = (position == 'absolute' ? 'absolute' : 'relative');
this._tilePane = this._panes.tilePane = this._createPane('leaflet-tile-pane'); this._initPanes();
this._panes.shadowPane = this._createPane('leaflet-shadow-pane');
this._panes.overlayPane = this._createPane('leaflet-overlay-pane'); if (this._initControlPos) this._initControlPos();
this._panes.markerPane = this._createPane('leaflet-marker-pane'); },
this._panes.popupPane = this._createPane('leaflet-popup-pane');
_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');
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');
}, },
_createPane: function(className, container) { _createPane: function(className, container) {
@ -290,6 +310,11 @@ L.Map = L.Class.extend({
this.fire('move'); this.fire('move');
if (zoomChanged) { this.fire('zoomend'); } if (zoomChanged) { this.fire('zoomend'); }
this.fire('moveend'); this.fire('moveend');
if (!this._loaded) {
this._loaded = true;
this.fire('load');
}
}, },
_initLayers: function(layers) { _initLayers: function(layers) {
@ -349,6 +374,7 @@ L.Map = L.Class.extend({
// private methods for getting map state // private methods for getting map state
_getTopLeftPoint: function() { _getTopLeftPoint: function() {
if (!this._loaded) throw new Error('Set map center and zoom first.');
var offset = L.DomUtil.getPosition(this._mapPane); var offset = L.DomUtil.getPosition(this._mapPane);
return this._initialTopLeftPoint.subtract(offset); return this._initialTopLeftPoint.subtract(offset);
}, },

View File

@ -3,7 +3,7 @@ L.Map.include(!(L.Transition && L.Transition.implemented()) ? {} : {
zoom = this._limitZoom(zoom); zoom = this._limitZoom(zoom);
var zoomChanged = (this._zoom != zoom); var zoomChanged = (this._zoom != zoom);
if (!forceReset && this._layers) { if (this._loaded && !forceReset && this._layers) {
// difference between the new and current centers in pixels // difference between the new and current centers in pixels
var offset = this._getNewTopLeftPoint(center).subtract(this._getTopLeftPoint()); var offset = this._getNewTopLeftPoint(center).subtract(this._getTopLeftPoint());

View File

@ -14,13 +14,13 @@ L.Map.include(!(L.Transition && L.Transition.implemented()) ? {} : {
//if offset does not exceed half of the view //if offset does not exceed half of the view
if (!this._offsetIsWithinView(offset, 1)) { return false; } if (!this._offsetIsWithinView(offset, 1)) { return false; }
this._initPanes(); this._initTilePanes();
this._runAnimation(center, zoom, scale, offset); this._runAnimation(center, zoom, scale, offset);
return true; return true;
}, },
_initPanes: function() { _initTilePanes: function() {
if (!this._tileBg) { if (!this._tileBg) {
this._tileBg = this._createPane('leaflet-tile-pane'); this._tileBg = this._createPane('leaflet-tile-pane');
this._tileBg.style.zIndex = 1; this._tileBg.style.zIndex = 1;