+ // @method mouseEventToLatLng(ev: MouseEvent): LatLng
+ // Given a MouseEvent object, returns geographical coordinate where the
+ // event took place.
+ mouseEventToLatLng: function (e) { // (MouseEvent)
+ return this.layerPointToLatLng(this.mouseEventToLayerPoint(e));
+ },
+
+
+ // map initialization methods
+
+ _initContainer: function (id) {
+ var container = this._container = get(id);
+
+ if (!container) {
+ throw new Error('Map container not found.');
+ } else if (container._leaflet_id) {
+ throw new Error('Map container is already initialized.');
+ }
+
+ on(container, 'scroll', this._onScroll, this);
+ this._containerId = stamp(container);
+ },
+
+ _initLayout: function () {
+ var container = this._container;
+
+ this._fadeAnimated = this.options.fadeAnimation && any3d;
+
+ addClass(container, 'leaflet-container' +
+ (touch ? ' leaflet-touch' : '') +
+ (retina ? ' leaflet-retina' : '') +
+ (ielt9 ? ' leaflet-oldie' : '') +
+ (safari ? ' leaflet-safari' : '') +
+ (this._fadeAnimated ? ' leaflet-fade-anim' : ''));
+
+ var position = 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._paneRenderers = {};
+
+ // @section
+ //
+ // Panes are DOM elements used to control the ordering of layers on the map. You
+ // can access panes with [`map.getPane`](#map-getpane) or
+ // [`map.getPanes`](#map-getpanes) methods. New panes can be created with the
+ // [`map.createPane`](#map-createpane) method.
+ //
+ // Every map has the following default panes that differ only in zIndex.
+ //
+ // @pane mapPane: HTMLElement = 'auto'
+ // Pane that contains all other map panes
+
+ this._mapPane = this.createPane('mapPane', this._container);
+ setPosition(this._mapPane, new Point(0, 0));
+
+ // @pane tilePane: HTMLElement = 200
+ // Pane for `GridLayer`s and `TileLayer`s
+ this.createPane('tilePane');
+ // @pane overlayPane: HTMLElement = 400
+ // Pane for vectors (`Path`s, like `Polyline`s and `Polygon`s), `ImageOverlay`s and `VideoOverlay`s
+ this.createPane('shadowPane');
+ // @pane shadowPane: HTMLElement = 500
+ // Pane for overlay shadows (e.g. `Marker` shadows)
+ this.createPane('overlayPane');
+ // @pane markerPane: HTMLElement = 600
+ // Pane for `Icon`s of `Marker`s
+ this.createPane('markerPane');
+ // @pane tooltipPane: HTMLElement = 650
+ // Pane for `Tooltip`s.
+ this.createPane('tooltipPane');
+ // @pane popupPane: HTMLElement = 700
+ // Pane for `Popup`s.
+ this.createPane('popupPane');
+
+ if (!this.options.markerZoomAnimation) {
+ addClass(panes.markerPane, 'leaflet-zoom-hide');
+ addClass(panes.shadowPane, 'leaflet-zoom-hide');
+ }
+ },
+
+
+ // private methods that modify map state
+
+ // @section Map state change events
+ _resetView: function (center, zoom) {
+ setPosition(this._mapPane, new Point(0, 0));
+
+ var loading = !this._loaded;
+ this._loaded = true;
+ zoom = this._limitZoom(zoom);
+
+ this.fire('viewprereset');
+
+ var zoomChanged = this._zoom !== zoom;
+ this
+ ._moveStart(zoomChanged, false)
+ ._move(center, zoom)
+ ._moveEnd(zoomChanged);
+
+ // @event viewreset: Event
+ // Fired when the map needs to redraw its content (this usually happens
+ // on map zoom or load). Very useful for creating custom overlays.
+ this.fire('viewreset');
+
+ // @event load: Event
+ // Fired when the map is initialized (when its center and zoom are set
+ // for the first time).
+ if (loading) {
+ this.fire('load');
+ }
+ },
+
+ _moveStart: function (zoomChanged, noMoveStart) {
+ // @event zoomstart: Event
+ // Fired when the map zoom is about to change (e.g. before zoom animation).
+ // @event movestart: Event
+ // Fired when the view of the map starts changing (e.g. user starts dragging the map).
+ if (zoomChanged) {
+ this.fire('zoomstart');
+ }
+ if (!noMoveStart) {
+ this.fire('movestart');
+ }
+ return this;
+ },
+
+ _move: function (center, zoom, data) {
+ if (zoom === undefined) {
+ zoom = this._zoom;
+ }
+ var zoomChanged = this._zoom !== zoom;
+
+ this._zoom = zoom;
+ this._lastCenter = center;
+ this._pixelOrigin = this._getNewPixelOrigin(center);
+
+ // @event zoom: Event
+ // Fired repeatedly during any change in zoom level, including zoom
+ // and fly animations.
+ if (zoomChanged || (data && data.pinch)) { // Always fire 'zoom' if pinching because #3530
+ this.fire('zoom', data);
+ }
+
+ // @event move: Event
+ // Fired repeatedly during any movement of the map, including pan and
+ // fly animations.
+ return this.fire('move', data);
+ },
+
+ _moveEnd: function (zoomChanged) {
+ // @event zoomend: Event
+ // Fired when the map has changed, after any animations.
+ if (zoomChanged) {
+ this.fire('zoomend');
+ }
+
+ // @event moveend: Event
+ // Fired when the center of the map stops changing (e.g. user stopped
+ // dragging the map).
+ return this.fire('moveend');
+ },
+
+ _stop: function () {
+ cancelAnimFrame(this._flyToFrame);
+ if (this._panAnim) {
+ this._panAnim.stop();
+ }
+ return this;
+ },
+
+ _rawPanBy: function (offset) {
+ setPosition(this._mapPane, this._getMapPanePos().subtract(offset));
+ },
+
+ _getZoomSpan: function () {
+ return this.getMaxZoom() - this.getMinZoom();
+ },
+
+ _panInsideMaxBounds: function () {
+ if (!this._enforcingBounds) {
+ this.panInsideBounds(this.options.maxBounds);
+ }
+ },
+
+ _checkIfLoaded: function () {
+ if (!this._loaded) {
+ throw new Error('Set map center and zoom first.');
+ }
+ },
+
+ // DOM event handling
+
+ // @section Interaction events
+ _initEvents: function (remove$$1) {
+ this._targets = {};
+ this._targets[stamp(this._container)] = this;
+
+ var onOff = remove$$1 ? off : on;
+
+ // @event click: MouseEvent
+ // Fired when the user clicks (or taps) the map.
+ // @event dblclick: MouseEvent
+ // Fired when the user double-clicks (or double-taps) the map.
+ // @event mousedown: MouseEvent
+ // Fired when the user pushes the mouse button on the map.
+ // @event mouseup: MouseEvent
+ // Fired when the user releases the mouse button on the map.
+ // @event mouseover: MouseEvent
+ // Fired when the mouse enters the map.
+ // @event mouseout: MouseEvent
+ // Fired when the mouse leaves the map.
+ // @event mousemove: MouseEvent
+ // Fired while the mouse moves over the map.
+ // @event contextmenu: MouseEvent
+ // Fired when the user pushes the right mouse button on the map, prevents
+ // default browser context menu from showing if there are listeners on
+ // this event. Also fired on mobile when the user holds a single touch
+ // for a second (also called long press).
+ // @event keypress: KeyboardEvent
+ // Fired when the user presses a key from the keyboard that produces a character value while the map is focused.
+ // @event keydown: KeyboardEvent
+ // Fired when the user presses a key from the keyboard while the map is focused. Unlike the `keypress` event,
+ // the `keydown` event is fired for keys that produce a character value and for keys
+ // that do not produce a character value.
+ // @event keyup: KeyboardEvent
+ // Fired when the user releases a key from the keyboard while the map is focused.
+ onOff(this._container, 'click dblclick mousedown mouseup ' +
+ 'mouseover mouseout mousemove contextmenu keypress keydown keyup', this._handleDOMEvent, this);
+
+ if (this.options.trackResize) {
+ onOff(window, 'resize', this._onResize, this);
+ }
+
+ if (any3d && this.options.transform3DLimit) {
+ (remove$$1 ? this.off : this.on).call(this, 'moveend', this._onMoveEnd);
+ }
+ },
+
+ _onResize: function () {
+ cancelAnimFrame(this._resizeRequest);
+ this._resizeRequest = requestAnimFrame(
+ function () { this.invalidateSize({debounceMoveend: true}); }, this);
+ },
+
+ _onScroll: function () {
+ this._container.scrollTop = 0;
+ this._container.scrollLeft = 0;
+ },
+
+ _onMoveEnd: function () {
+ var pos = this._getMapPanePos();
+ if (Math.max(Math.abs(pos.x), Math.abs(pos.y)) >= this.options.transform3DLimit) {
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=1203873 but Webkit also have
+ // a pixel offset on very high values, see: http://jsfiddle.net/dg6r5hhb/
+ this._resetView(this.getCenter(), this.getZoom());
+ }
+ },
+
+ _findEventTargets: function (e, type) {
+ var targets = [],
+ target,
+ isHover = type === 'mouseout' || type === 'mouseover',
+ src = e.target || e.srcElement,
+ dragging = false;
+
+ while (src) {
+ target = this._targets[stamp(src)];
+ if (target && (type === 'click' || type === 'preclick') && !e._simulated && this._draggableMoved(target)) {
+ // Prevent firing click after you just dragged an object.
+ dragging = true;
+ break;
+ }
+ if (target && target.listens(type, true)) {
+ if (isHover && !isExternalTarget(src, e)) { break; }
+ targets.push(target);
+ if (isHover) { break; }
+ }
+ if (src === this._container) { break; }
+ src = src.parentNode;
+ }
+ if (!targets.length && !dragging && !isHover && isExternalTarget(src, e)) {
+ targets = [this];
+ }
+ return targets;
+ },
+
+ _handleDOMEvent: function (e) {
+ if (!this._loaded || skipped(e)) { return; }
+
+ var type = e.type;
+
+ if (type === 'mousedown' || type === 'keypress' || type === 'keyup' || type === 'keydown') {
+ // prevents outline when clicking on keyboard-focusable element
+ preventOutline(e.target || e.srcElement);
+ }
+
+ this._fireDOMEvent(e, type);
+ },
+
+ _mouseEvents: ['click', 'dblclick', 'mouseover', 'mouseout', 'contextmenu'],
+
+ _fireDOMEvent: function (e, type, targets) {
+
+ if (e.type === 'click') {
+ // Fire a synthetic 'preclick' event which propagates up (mainly for closing popups).
+ // @event preclick: MouseEvent
+ // Fired before mouse click on the map (sometimes useful when you
+ // want something to happen on click before any existing click
+ // handlers start running).
+ var synth = extend({}, e);
+ synth.type = 'preclick';
+ this._fireDOMEvent(synth, synth.type, targets);
+ }
+
+ if (e._stopped) { return; }
+
+ // Find the layer the event is propagating from and its parents.
+ targets = (targets || []).concat(this._findEventTargets(e, type));
+
+ if (!targets.length) { return; }
+
+ var target = targets[0];
+ if (type === 'contextmenu' && target.listens(type, true)) {
+ preventDefault(e);
+ }
+
+ var data = {
+ originalEvent: e
+ };
+
+ if (e.type !== 'keypress' && e.type !== 'keydown' && e.type !== 'keyup') {
+ var isMarker = target.getLatLng && (!target._radius || target._radius <= 10);
+ data.containerPoint = isMarker ?
+ this.latLngToContainerPoint(target.getLatLng()) : this.mouseEventToContainerPoint(e);
+ data.layerPoint = this.containerPointToLayerPoint(data.containerPoint);
+ data.latlng = isMarker ? target.getLatLng() : this.layerPointToLatLng(data.layerPoint);
+ }
+
+ for (var i = 0; i < targets.length; i++) {
+ targets[i].fire(type, data, true);
+ if (data.originalEvent._stopped ||
+ (targets[i].options.bubblingMouseEvents === false && indexOf(this._mouseEvents, type) !== -1)) { return; }
+ }
+ },
+
+ _draggableMoved: function (obj) {
+ obj = obj.dragging && obj.dragging.enabled() ? obj : this;
+ return (obj.dragging && obj.dragging.moved()) || (this.boxZoom && this.boxZoom.moved());
+ },
+
+ _clearHandlers: function () {
+ for (var i = 0, len = this._handlers.length; i < len; i++) {
+ this._handlers[i].disable();
+ }
+ },
+
+ // @section Other Methods
+
+ // @method whenReady(fn: Function, context?: Object): this
+ // Runs the given function `fn` when the map gets initialized with
+ // a view (center and zoom) and at least one layer, or immediately
+ // if it's already initialized, optionally passing a function context.
+ whenReady: function (callback, context) {
+ if (this._loaded) {
+ callback.call(context || this, {target: this});
+ } else {
+ this.on('load', callback, context);
+ }
+ return this;
+ },
+
+
+ // private methods for getting map state
+
+ _getMapPanePos: function () {
+ return getPosition(this._mapPane) || new Point(0, 0);
+ },
+
+ _moved: function () {
+ var pos = this._getMapPanePos();
+ return pos && !pos.equals([0, 0]);
+ },
+
+ _getTopLeftPoint: function (center, zoom) {
+ var pixelOrigin = center && zoom !== undefined ?
+ this._getNewPixelOrigin(center, zoom) :
+ this.getPixelOrigin();
+ return pixelOrigin.subtract(this._getMapPanePos());
+ },
+
+ _getNewPixelOrigin: function (center, zoom) {
+ var viewHalf = this.getSize()._divideBy(2);
+ return this.project(center, zoom)._subtract(viewHalf)._add(this._getMapPanePos())._round();
+ },
+
+ _latLngToNewLayerPoint: function (latlng, zoom, center) {
+ var topLeft = this._getNewPixelOrigin(center, zoom);
+ return this.project(latlng, zoom)._subtract(topLeft);
+ },
+
+ _latLngBoundsToNewLayerBounds: function (latLngBounds, zoom, center) {
+ var topLeft = this._getNewPixelOrigin(center, zoom);
+ return toBounds([
+ this.project(latLngBounds.getSouthWest(), zoom)._subtract(topLeft),
+ this.project(latLngBounds.getNorthWest(), zoom)._subtract(topLeft),
+ this.project(latLngBounds.getSouthEast(), zoom)._subtract(topLeft),
+ this.project(latLngBounds.getNorthEast(), zoom)._subtract(topLeft)
+ ]);
+ },
+
+ // layer point of the current center
+ _getCenterLayerPoint: function () {
+ return this.containerPointToLayerPoint(this.getSize()._divideBy(2));
+ },
+
+ // offset of the specified place to the current center in pixels
+ _getCenterOffset: function (latlng) {
+ return this.latLngToLayerPoint(latlng).subtract(this._getCenterLayerPoint());
+ },
+
+ // adjust center for view to get inside bounds
+ _limitCenter: function (center, zoom, bounds) {
+
+ if (!bounds) { return center; }
+
+ var centerPoint = this.project(center, zoom),
+ viewHalf = this.getSize().divideBy(2),
+ viewBounds = new Bounds(centerPoint.subtract(viewHalf), centerPoint.add(viewHalf)),
+ offset = this._getBoundsOffset(viewBounds, bounds, zoom);
+
+ // If offset is less than a pixel, ignore.
+ // This prevents unstable projections from getting into
+ // an infinite loop of tiny offsets.
+ if (offset.round().equals([0, 0])) {
+ return center;
+ }
+
+ return this.unproject(centerPoint.add(offset), zoom);
+ },
+
+ // adjust offset for view to get inside bounds
+ _limitOffset: function (offset, bounds) {
+ if (!bounds) { return offset; }
+
+ var viewBounds = this.getPixelBounds(),
+ newBounds = new Bounds(viewBounds.min.add(offset), viewBounds.max.add(offset));
+
+ return offset.add(this._getBoundsOffset(newBounds, bounds));
+ },
+
+ // returns offset needed for pxBounds to get inside maxBounds at a specified zoom
+ _getBoundsOffset: function (pxBounds, maxBounds, zoom) {
+ var projectedMaxBounds = toBounds(
+ this.project(maxBounds.getNorthEast(), zoom),
+ this.project(maxBounds.getSouthWest(), zoom)
+ ),
+ minOffset = projectedMaxBounds.min.subtract(pxBounds.min),
+ maxOffset = projectedMaxBounds.max.subtract(pxBounds.max),
+
+ dx = this._rebound(minOffset.x, -maxOffset.x),
+ dy = this._rebound(minOffset.y, -maxOffset.y);
+
+ return new Point(dx, dy);
+ },
+
+ _rebound: function (left, right) {
+ return left + right > 0 ?
+ Math.round(left - right) / 2 :
+ Math.max(0, Math.ceil(left)) - Math.max(0, Math.floor(right));
+ },
+
+ _limitZoom: function (zoom) {
+ var min = this.getMinZoom(),
+ max = this.getMaxZoom(),
+ snap = any3d ? this.options.zoomSnap : 1;
+ if (snap) {
+ zoom = Math.round(zoom / snap) * snap;
+ }
+ return Math.max(min, Math.min(max, zoom));
+ },
+
+ _onPanTransitionStep: function () {
+ this.fire('move');
+ },
+
+ _onPanTransitionEnd: function () {
+ removeClass(this._mapPane, 'leaflet-pan-anim');
+ this.fire('moveend');
+ },
+
+ _tryAnimatedPan: function (center, options) {
+ // difference between the new and current centers in pixels
+ var offset = this._getCenterOffset(center)._trunc();
+
+ // don't animate too far unless animate: true specified in options
+ if ((options && options.animate) !== true && !this.getSize().contains(offset)) { return false; }
+
+ this.panBy(offset, options);
+
+ return true;
+ },
+
+ _createAnimProxy: function () {
+
+ var proxy = this._proxy = create$1('div', 'leaflet-proxy leaflet-zoom-animated');
+ this._panes.mapPane.appendChild(proxy);
+
+ this.on('zoomanim', function (e) {
+ var prop = TRANSFORM,
+ transform = this._proxy.style[prop];