+ targetCenter = toLatLng(targetCenter);
+ targetZoom = targetZoom === undefined ? startZoom : targetZoom;
+
+ var w0 = Math.max(size.x, size.y),
+ w1 = w0 * this.getZoomScale(startZoom, targetZoom),
+ u1 = (to.distanceTo(from)) || 1,
+ rho = 1.42,
+ rho2 = rho * rho;
+
+ function r(i) {
+ var s1 = i ? -1 : 1,
+ s2 = i ? w1 : w0,
+ t1 = w1 * w1 - w0 * w0 + s1 * rho2 * rho2 * u1 * u1,
+ b1 = 2 * s2 * rho2 * u1,
+ b = t1 / b1,
+ sq = Math.sqrt(b * b + 1) - b;
+
+ // workaround for floating point precision bug when sq = 0, log = -Infinite,
+ // thus triggering an infinite loop in flyTo
+ var log = sq < 0.000000001 ? -18 : Math.log(sq);
+
+ return log;
+ }
+
+ function sinh(n) { return (Math.exp(n) - Math.exp(-n)) / 2; }
+ function cosh(n) { return (Math.exp(n) + Math.exp(-n)) / 2; }
+ function tanh(n) { return sinh(n) / cosh(n); }
+
+ var r0 = r(0);
+
+ function w(s) { return w0 * (cosh(r0) / cosh(r0 + rho * s)); }
+ function u(s) { return w0 * (cosh(r0) * tanh(r0 + rho * s) - sinh(r0)) / rho2; }
+
+ function easeOut(t) { return 1 - Math.pow(1 - t, 1.5); }
+
+ var start = Date.now(),
+ S = (r(1) - r0) / rho,
+ duration = options.duration ? 1000 * options.duration : 1000 * S * 0.8;
+
+ function frame() {
+ var t = (Date.now() - start) / duration,
+ s = easeOut(t) * S;
+
+ if (t <= 1) {
+ this._flyToFrame = requestAnimFrame(frame, this);
+
+ this._move(
+ this.unproject(from.add(to.subtract(from).multiplyBy(u(s) / u1)), startZoom),
+ this.getScaleZoom(w0 / w(s), startZoom),
+ {flyTo: true});
+
+ } else {
+ this
+ ._move(targetCenter, targetZoom)
+ ._moveEnd(true);
+ }
+ }
+
+ this._moveStart(true, options.noMoveStart);
+
+ frame.call(this);
+ return this;
+ },
+
+ // @method flyToBounds(bounds: LatLngBounds, options?: fitBounds options): this
+ // Sets the view of the map with a smooth animation like [`flyTo`](#map-flyto),
+ // but takes a bounds parameter like [`fitBounds`](#map-fitbounds).
+ flyToBounds: function (bounds, options) {
+ var target = this._getBoundsCenterZoom(bounds, options);
+ return this.flyTo(target.center, target.zoom, options);
+ },
+
+ // @method setMaxBounds(bounds: Bounds): this
+ // Restricts the map view to the given bounds (see the [maxBounds](#map-maxbounds) option).
+ setMaxBounds: function (bounds) {
+ bounds = toLatLngBounds(bounds);
+
+ if (!bounds.isValid()) {
+ this.options.maxBounds = null;
+ return this.off('moveend', this._panInsideMaxBounds);
+ } else if (this.options.maxBounds) {
+ this.off('moveend', this._panInsideMaxBounds);
+ }
+
+ this.options.maxBounds = bounds;
+
+ if (this._loaded) {
+ this._panInsideMaxBounds();
+ }
+
+ return this.on('moveend', this._panInsideMaxBounds);
+ },
+
+ // @method setMinZoom(zoom: Number): this
+ // Sets the lower limit for the available zoom levels (see the [minZoom](#map-minzoom) option).
+ setMinZoom: function (zoom) {
+ var oldZoom = this.options.minZoom;
+ this.options.minZoom = zoom;
+
+ if (this._loaded && oldZoom !== zoom) {
+ this.fire('zoomlevelschange');
+
+ if (this.getZoom() < this.options.minZoom) {
+ return this.setZoom(zoom);
+ }
+ }
+
+ return this;
+ },
+
+ // @method setMaxZoom(zoom: Number): this
+ // Sets the upper limit for the available zoom levels (see the [maxZoom](#map-maxzoom) option).
+ setMaxZoom: function (zoom) {
+ var oldZoom = this.options.maxZoom;
+ this.options.maxZoom = zoom;
+
+ if (this._loaded && oldZoom !== zoom) {
+ this.fire('zoomlevelschange');
+
+ if (this.getZoom() > this.options.maxZoom) {
+ return this.setZoom(zoom);
+ }
+ }
+
+ return this;
+ },
+
+ // @method panInsideBounds(bounds: LatLngBounds, options?: Pan options): this
+ // Pans the map to the closest view that would lie inside the given bounds (if it's not already), controlling the animation using the options specific, if any.
+ panInsideBounds: function (bounds, options) {
+ this._enforcingBounds = true;
+ var center = this.getCenter(),
+ newCenter = this._limitCenter(center, this._zoom, toLatLngBounds(bounds));
+
+ if (!center.equals(newCenter)) {
+ this.panTo(newCenter, options);
+ }
+
+ this._enforcingBounds = false;
+ return this;
+ },
+
+ // @method panInside(latlng: LatLng, options?: options): this
+ // Pans the map the minimum amount to make the `latlng` visible. Use
+ // `padding`, `paddingTopLeft` and `paddingTopRight` options to fit
+ // the display to more restricted bounds, like [`fitBounds`](#map-fitbounds).
+ // If `latlng` is already within the (optionally padded) display bounds,
+ // the map will not be panned.
+ panInside: function (latlng, options) {
+ options = options || {};
+
+ var paddingTL = toPoint(options.paddingTopLeft || options.padding || [0, 0]),
+ paddingBR = toPoint(options.paddingBottomRight || options.padding || [0, 0]),
+ center = this.getCenter(),
+ pixelCenter = this.project(center),
+ pixelPoint = this.project(latlng),
+ pixelBounds = this.getPixelBounds(),
+ halfPixelBounds = pixelBounds.getSize().divideBy(2),
+ paddedBounds = toBounds([pixelBounds.min.add(paddingTL), pixelBounds.max.subtract(paddingBR)]);
+
+ if (!paddedBounds.contains(pixelPoint)) {
+ this._enforcingBounds = true;
+ var diff = pixelCenter.subtract(pixelPoint),
+ newCenter = toPoint(pixelPoint.x + diff.x, pixelPoint.y + diff.y);
+
+ if (pixelPoint.x < paddedBounds.min.x || pixelPoint.x > paddedBounds.max.x) {
+ newCenter.x = pixelCenter.x - diff.x;
+ if (diff.x > 0) {
+ newCenter.x += halfPixelBounds.x - paddingTL.x;
+ } else {
+ newCenter.x -= halfPixelBounds.x - paddingBR.x;
+ }
+ }
+ if (pixelPoint.y < paddedBounds.min.y || pixelPoint.y > paddedBounds.max.y) {
+ newCenter.y = pixelCenter.y - diff.y;
+ if (diff.y > 0) {
+ newCenter.y += halfPixelBounds.y - paddingTL.y;
+ } else {
+ newCenter.y -= halfPixelBounds.y - paddingBR.y;
+ }
+ }
+ this.panTo(this.unproject(newCenter), options);
+ this._enforcingBounds = false;
+ }
+ return this;
+ },
+
+ // @method invalidateSize(options: Zoom/pan options): this
+ // Checks if the map container size changed and updates the map if so —
+ // call it after you've changed the map size dynamically, also animating
+ // pan by default. If `options.pan` is `false`, panning will not occur.
+ // If `options.debounceMoveend` is `true`, it will delay `moveend` event so
+ // that it doesn't happen often even if the method is called many
+ // times in a row.
+
+ // @alternative
+ // @method invalidateSize(animate: Boolean): this
+ // Checks if the map container size changed and updates the map if so —
+ // call it after you've changed the map size dynamically, also animating
+ // pan by default.
+ invalidateSize: function (options) {
+ if (!this._loaded) { return this; }
+
+ options = extend({
+ animate: false,
+ pan: true
+ }, options === true ? {animate: true} : options);
+
+ var oldSize = this.getSize();
+ this._sizeChanged = true;
+ this._lastCenter = null;
+
+ var newSize = this.getSize(),
+ oldCenter = oldSize.divideBy(2).round(),
+ newCenter = newSize.divideBy(2).round(),
+ offset = oldCenter.subtract(newCenter);
+
+ if (!offset.x && !offset.y) { return this; }
+
+ if (options.animate && options.pan) {
+ this.panBy(offset);
+
+ } else {
+ if (options.pan) {
+ this._rawPanBy(offset);
+ }
+
+ this.fire('move');
+
+ if (options.debounceMoveend) {
+ clearTimeout(this._sizeTimer);
+ this._sizeTimer = setTimeout(bind(this.fire, this, 'moveend'), 200);
+ } else {
+ this.fire('moveend');
+ }
+ }
+
+ // @section Map state change events
+ // @event resize: ResizeEvent
+ // Fired when the map is resized.
+ return this.fire('resize', {
+ oldSize: oldSize,
+ newSize: newSize
+ });
+ },
+
+ // @section Methods for modifying map state
+ // @method stop(): this
+ // Stops the currently running `panTo` or `flyTo` animation, if any.
+ stop: function () {
+ this.setZoom(this._limitZoom(this._zoom));
+ if (!this.options.zoomSnap) {
+ this.fire('viewreset');
+ }
+ return this._stop();
+ },
+
+ // @section Geolocation methods
+ // @method locate(options?: Locate options): this
+ // Tries to locate the user using the Geolocation API, firing a [`locationfound`](#map-locationfound)
+ // event with location data on success or a [`locationerror`](#map-locationerror) event on failure,
+ // and optionally sets the map view to the user's location with respect to
+ // detection accuracy (or to the world view if geolocation failed).
+ // Note that, if your page doesn't use HTTPS, this method will fail in
+ // modern browsers ([Chrome 50 and newer](https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins))
+ // See `Locate options` for more details.
+ locate: function (options) {
+
+ options = this._locateOptions = extend({
+ timeout: 10000,
+ watch: false
+ // setView: false
+ // maxZoom: <Number>
+ // maximumAge: 0
+ // enableHighAccuracy: false
+ }, options);
+
+ if (!('geolocation' in navigator)) {
+ this._handleGeolocationError({
+ code: 0,
+ message: 'Geolocation not supported.'
+ });
+ return this;
+ }
+
+ var onResponse = bind(this._handleGeolocationResponse, this),
+ onError = bind(this._handleGeolocationError, this);
+
+ if (options.watch) {
+ this._locationWatchId =
+ navigator.geolocation.watchPosition(onResponse, onError, options);
+ } else {
+ navigator.geolocation.getCurrentPosition(onResponse, onError, options);
+ }
+ return this;
+ },
+
+ // @method stopLocate(): this
+ // Stops watching location previously initiated by `map.locate({watch: true})`
+ // and aborts resetting the map view if map.locate was called with
+ // `{setView: true}`.
+ stopLocate: function () {
+ if (navigator.geolocation && navigator.geolocation.clearWatch) {
+ navigator.geolocation.clearWatch(this._locationWatchId);
+ }
+ if (this._locateOptions) {
+ this._locateOptions.setView = false;
+ }
+ return this;
+ },
+
+ _handleGeolocationError: function (error) {
+ var c = error.code,
+ message = error.message ||
+ (c === 1 ? 'permission denied' :
+ (c === 2 ? 'position unavailable' : 'timeout'));
+
+ if (this._locateOptions.setView && !this._loaded) {
+ this.fitWorld();
+ }
+
+ // @section Location events
+ // @event locationerror: ErrorEvent
+ // Fired when geolocation (using the [`locate`](#map-locate) method) failed.
+ this.fire('locationerror', {
+ code: c,
+ message: 'Geolocation error: ' + message + '.'
+ });
+ },
+
+ _handleGeolocationResponse: function (pos) {
+ var lat = pos.coords.latitude,
+ lng = pos.coords.longitude,
+ latlng = new LatLng(lat, lng),
+ bounds = latlng.toBounds(pos.coords.accuracy * 2),
+ options = this._locateOptions;
+
+ if (options.setView) {
+ var zoom = this.getBoundsZoom(bounds);
+ this.setView(latlng, options.maxZoom ? Math.min(zoom, options.maxZoom) : zoom);
+ }
+
+ var data = {
+ latlng: latlng,
+ bounds: bounds,
+ timestamp: pos.timestamp
+ };
+
+ for (var i in pos.coords) {
+ if (typeof pos.coords[i] === 'number') {
+ data[i] = pos.coords[i];
+ }
+ }
+
+ // @event locationfound: LocationEvent
+ // Fired when geolocation (using the [`locate`](#map-locate) method)
+ // went successfully.
+ this.fire('locationfound', data);
+ },
+
+ // TODO Appropriate docs section?
+ // @section Other Methods
+ // @method addHandler(name: String, HandlerClass: Function): this
+ // Adds a new `Handler` to the map, given its name and constructor function.
+ addHandler: function (name, HandlerClass) {
+ if (!HandlerClass) { return this; }
+
+ var handler = this[name] = new HandlerClass(this);
+
+ this._handlers.push(handler);
+
+ if (this.options[name]) {
+ handler.enable();
+ }
+
+ return this;
+ },
+
+ // @method remove(): this
+ // Destroys the map and clears all related event listeners.
+ remove: function () {
+
+ this._initEvents(true);
+
+ if (this._containerId !== this._container._leaflet_id) {
+ throw new Error('Map container is being reused by another instance');
+ }
+
+ try {
+ // throws error in IE6-8
+ delete this._container._leaflet_id;
+ delete this._containerId;
+ } catch (e) {
+ /*eslint-disable */
+ this._container._leaflet_id = undefined;
+ /* eslint-enable */
+ this._containerId = undefined;
+ }
+
+ if (this._locationWatchId !== undefined) {
+ this.stopLocate();
+ }
+
+ this._stop();
+
+ remove(this._mapPane);
+
+ if (this._clearControlPos) {
+ this._clearControlPos();
+ }
+ if (this._resizeRequest) {
+ cancelAnimFrame(this._resizeRequest);
+ this._resizeRequest = null;
+ }
+
+ this._clearHandlers();
+
+ if (this._loaded) {
+ // @section Map state change events
+ // @event unload: Event
+ // Fired when the map is destroyed with [remove](#map-remove) method.
+ this.fire('unload');
+ }
+
+ var i;
+ for (i in this._layers) {
+ this._layers[i].remove();
+ }
+ for (i in this._panes) {
+ remove(this._panes[i]);
+ }
+
+ this._layers = [];
+ this._panes = [];
+ delete this._mapPane;
+ delete this._renderer;
+
+ return this;
+ },
+
+ // @section Other Methods
+ // @method createPane(name: String, container?: HTMLElement): HTMLElement
+ // Creates a new [map pane](#map-pane) with the given name if it doesn't exist already,
+ // then returns it. The pane is created as a child of `container`, or
+ // as a child of the main map pane if not set.
+ createPane: function (name, container) {
+ var className = 'leaflet-pane' + (name ? ' leaflet-' + name.replace('Pane', '') + '-pane' : ''),
+ pane = create$1('div', className, container || this._mapPane);
+
+ if (name) {
+ this._panes[name] = pane;
+ }
+ return pane;
+ },
+
+ // @section Methods for Getting Map State
+
+ // @method getCenter(): LatLng
+ // Returns the geographical center of the map view
+ getCenter: function () {
+ this._checkIfLoaded();
+
+ if (this._lastCenter && !this._moved()) {
+ return this._lastCenter;
+ }
+ return this.layerPointToLatLng(this._getCenterLayerPoint());
+ },
+
+ // @method getZoom(): Number
+ // Returns the current zoom level of the map view
+ getZoom: function () {
+ return this._zoom;
+ },
+
+ // @method getBounds(): LatLngBounds
+ // Returns the geographical bounds visible in the current map view
+ getBounds: function () {
+ var bounds = this.getPixelBounds(),
+ sw = this.unproject(bounds.getBottomLeft()),
+ ne = this.unproject(bounds.getTopRight());
+
+ return new LatLngBounds(sw, ne);
+ },
+
+ // @method getMinZoom(): Number
+ // Returns the minimum zoom level of the map (if set in the `minZoom` option of the map or of any layers), or `0` by default.
+ getMinZoom: function () {
+ return this.options.minZoom === undefined ? this._layersMinZoom || 0 : this.options.minZoom;
+ },
+
+ // @method getMaxZoom(): Number
+ // Returns the maximum zoom level of the map (if set in the `maxZoom` option of the map or of any layers).
+ getMaxZoom: function () {
+ return this.options.maxZoom === undefined ?
+ (this._layersMaxZoom === undefined ? Infinity : this._layersMaxZoom) :
+ this.options.maxZoom;
+ },
+
+ // @method getBoundsZoom(bounds: LatLngBounds, inside?: Boolean, padding?: Point): Number
+ // Returns the maximum zoom level on which the given bounds fit to the map
+ // view in its entirety. If `inside` (optional) is set to `true`, the method
+ // instead returns the minimum zoom level on which the map view fits into
+ // the given bounds in its entirety.
+ getBoundsZoom: function (bounds, inside, padding) { // (LatLngBounds[, Boolean, Point]) -> Number
+ bounds = toLatLngBounds(bounds);
+ padding = toPoint(padding || [0, 0]);
+
+ var zoom = this.getZoom() || 0,
+ min = this.getMinZoom(),
+ max = this.getMaxZoom(),
+ nw = bounds.getNorthWest(),
+ se = bounds.getSouthEast(),
+ size = this.getSize().subtract(padding),
+ boundsSize = toBounds(this.project(se, zoom), this.project(nw, zoom)).getSize(),
+ snap = any3d ? this.options.zoomSnap : 1,
+ scalex = size.x / boundsSize.x,
+ scaley = size.y / boundsSize.y,
+ scale = inside ? Math.max(scalex, scaley) : Math.min(scalex, scaley);
+
+ zoom = this.getScaleZoom(scale, zoom);
+
+ if (snap) {
+ zoom = Math.round(zoom / (snap / 100)) * (snap / 100); // don't jump if within 1% of a snap level
+ zoom = inside ? Math.ceil(zoom / snap) * snap : Math.floor(zoom / snap) * snap;
+ }
+
+ return Math.max(min, Math.min(max, zoom));
+ },
+
+ // @method getSize(): Point
+ // Returns the current size of the map container (in pixels).
+ getSize: function () {
+ if (!this._size || this._sizeChanged) {
+ this._size = new Point(
+ this._container.clientWidth || 0,
+ this._container.clientHeight || 0);
+
+ this._sizeChanged = false;
+ }
+ return this._size.clone();
+ },
+
+ // @method getPixelBounds(): Bounds
+ // Returns the bounds of the current map view in projected pixel
+ // coordinates (sometimes useful in layer and overlay implementations).
+ getPixelBounds: function (center, zoom) {
+ var topLeftPoint = this._getTopLeftPoint(center, zoom);
+ return new Bounds(topLeftPoint, topLeftPoint.add(this.getSize()));
+ },
+
+ // TODO: Check semantics - isn't the pixel origin the 0,0 coord relative to
+ // the map pane? "left point of the map layer" can be confusing, specially
+ // since there can be negative offsets.
+ // @method getPixelOrigin(): Point
+ // Returns the projected pixel coordinates of the top left point of
+ // the map layer (useful in custom layer and overlay implementations).
+ getPixelOrigin: function () {
+ this._checkIfLoaded();
+ return this._pixelOrigin;
+ },