X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/e5d81734f2212675f6342bf7660de15dadbdafa1..f4a4632b45af73f051f846bc01f4d45c58f77f8b:/vendor/assets/leaflet/leaflet.js?ds=sidebyside
diff --git a/vendor/assets/leaflet/leaflet.js b/vendor/assets/leaflet/leaflet.js
index d3d563574..045b30719 100644
--- a/vendor/assets/leaflet/leaflet.js
+++ b/vendor/assets/leaflet/leaflet.js
@@ -1,13 +1,22 @@
/*
- Leaflet, a JavaScript library for mobile-friendly interactive maps. http://leafletjs.com
- (c) 2010-2013, Vladimir Agafonkin
- (c) 2010-2011, CloudMade
+ Leaflet 1.0.0, a JS library for interactive maps. http://leafletjs.com
+ (c) 2010-2016 Vladimir Agafonkin, (c) 2010-2011 CloudMade
*/
(function (window, document, undefined) {
-var oldL = window.L,
- L = {};
+var L = {
+ version: "1.0.0"
+};
+
+function expose() {
+ var oldL = window.L;
+
+ L.noConflict = function () {
+ window.L = oldL;
+ return this;
+ };
-L.version = '0.7.7';
+ window.L = L;
+}
// define Leaflet for Node module pattern loaders, including Browserify
if (typeof module === 'object' && typeof module.exports === 'object') {
@@ -19,114 +28,160 @@ if (typeof module === 'object' && typeof module.exports === 'object') {
}
// define Leaflet as a global L variable, saving the original L to restore later if needed
+if (typeof window !== 'undefined') {
+ expose();
+}
-L.noConflict = function () {
- window.L = oldL;
- return this;
-};
-
-window.L = L;
/*
- * L.Util contains various utility functions used throughout Leaflet code.
+ * @namespace Util
+ *
+ * Various utility functions, used by Leaflet internally.
*/
L.Util = {
- extend: function (dest) { // (Object[, Object, ...]) ->
- var sources = Array.prototype.slice.call(arguments, 1),
- i, j, len, src;
- for (j = 0, len = sources.length; j < len; j++) {
- src = sources[j] || {};
+ // @function extend(dest: Object, src?: Object): Object
+ // Merges the properties of the `src` object (or multiple objects) into `dest` object and returns the latter. Has an `L.extend` shortcut.
+ extend: function (dest) {
+ var i, j, len, src;
+
+ for (j = 1, len = arguments.length; j < len; j++) {
+ src = arguments[j];
for (i in src) {
- if (src.hasOwnProperty(i)) {
- dest[i] = src[i];
- }
+ dest[i] = src[i];
}
}
return dest;
},
- bind: function (fn, obj) { // (Function, Object) -> Function
- var args = arguments.length > 2 ? Array.prototype.slice.call(arguments, 2) : null;
- return function () {
- return fn.apply(obj, args || arguments);
- };
- },
-
- stamp: (function () {
- var lastId = 0,
- key = '_leaflet_id';
- return function (obj) {
- obj[key] = obj[key] || ++lastId;
- return obj[key];
+ // @function create(proto: Object, properties?: Object): Object
+ // Compatibility polyfill for [Object.create](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/create)
+ create: Object.create || (function () {
+ function F() {}
+ return function (proto) {
+ F.prototype = proto;
+ return new F();
};
- }()),
+ })(),
- invokeEach: function (obj, method, context) {
- var i, args;
+ // @function bind(fn: Function, â¦): Function
+ // Returns a new function bound to the arguments passed, like [Function.prototype.bind](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function/bind).
+ // Has a `L.bind()` shortcut.
+ bind: function (fn, obj) {
+ var slice = Array.prototype.slice;
- if (typeof obj === 'object') {
- args = Array.prototype.slice.call(arguments, 3);
-
- for (i in obj) {
- method.apply(context, [i, obj[i]].concat(args));
- }
- return true;
+ if (fn.bind) {
+ return fn.bind.apply(fn, slice.call(arguments, 1));
}
- return false;
- },
-
- limitExecByInterval: function (fn, time, context) {
- var lock, execOnUnlock;
+ var args = slice.call(arguments, 2);
- return function wrapperFn() {
- var args = arguments;
+ return function () {
+ return fn.apply(obj, args.length ? args.concat(slice.call(arguments)) : arguments);
+ };
+ },
- if (lock) {
- execOnUnlock = true;
- return;
+ // @function stamp(obj: Object): Number
+ // Returns the unique ID of an object, assiging it one if it doesn't have it.
+ stamp: function (obj) {
+ /*eslint-disable */
+ obj._leaflet_id = obj._leaflet_id || ++L.Util.lastId;
+ return obj._leaflet_id;
+ /*eslint-enable */
+ },
+
+ // @property lastId: Number
+ // Last unique ID used by [`stamp()`](#util-stamp)
+ lastId: 0,
+
+ // @function throttle(fn: Function, time: Number, context: Object): Function
+ // Returns a function which executes function `fn` with the given scope `context`
+ // (so that the `this` keyword refers to `context` inside `fn`'s code). The function
+ // `fn` will be called no more than one time per given amount of `time`. The arguments
+ // received by the bound function will be any arguments passed when binding the
+ // function, followed by any arguments passed when invoking the bound function.
+ // Has an `L.bind` shortcut.
+ throttle: function (fn, time, context) {
+ var lock, args, wrapperFn, later;
+
+ later = function () {
+ // reset lock and call if queued
+ lock = false;
+ if (args) {
+ wrapperFn.apply(context, args);
+ args = false;
}
+ };
- lock = true;
-
- setTimeout(function () {
- lock = false;
-
- if (execOnUnlock) {
- wrapperFn.apply(context, args);
- execOnUnlock = false;
- }
- }, time);
+ wrapperFn = function () {
+ if (lock) {
+ // called too soon, queue to call later
+ args = arguments;
- fn.apply(context, args);
+ } else {
+ // call and lock until later
+ fn.apply(context, arguments);
+ setTimeout(later, time);
+ lock = true;
+ }
};
+
+ return wrapperFn;
},
- falseFn: function () {
- return false;
+ // @function wrapNum(num: Number, range: Number[], includeMax?: Boolean): Number
+ // Returns the number `num` modulo `range` in such a way so it lies within
+ // `range[0]` and `range[1]`. The returned value will be always smaller than
+ // `range[1]` unless `includeMax` is set to `true`.
+ wrapNum: function (x, range, includeMax) {
+ var max = range[1],
+ min = range[0],
+ d = max - min;
+ return x === max && includeMax ? x : ((x - min) % d + d) % d + min;
},
+ // @function falseFn(): Function
+ // Returns a function which always returns `false`.
+ falseFn: function () { return false; },
+
+ // @function formatNum(num: Number, digits?: Number): Number
+ // Returns the number `num` rounded to `digits` decimals, or to 5 decimals by default.
formatNum: function (num, digits) {
var pow = Math.pow(10, digits || 5);
return Math.round(num * pow) / pow;
},
+ // @function trim(str: String): String
+ // Compatibility polyfill for [String.prototype.trim](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)
trim: function (str) {
return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, '');
},
+ // @function splitWords(str: String): String[]
+ // Trims and splits the string on whitespace and returns the array of parts.
splitWords: function (str) {
return L.Util.trim(str).split(/\s+/);
},
+ // @function setOptions(obj: Object, options: Object): Object
+ // Merges the given properties to the `options` of the `obj` object, returning the resulting options. See `Class options`. Has an `L.setOptions` shortcut.
setOptions: function (obj, options) {
- obj.options = L.extend({}, obj.options, options);
+ if (!obj.hasOwnProperty('options')) {
+ obj.options = obj.options ? L.Util.create(obj.options) : {};
+ }
+ for (var i in options) {
+ obj.options[i] = options[i];
+ }
return obj.options;
},
+ // @function getParamString(obj: Object, existingUrl?: String, uppercase?: Boolean): String
+ // Converts an object into a parameter URL string, e.g. `{a: "foo", b: "bar"}`
+ // translates to `'?a=foo&b=bar'`. If `existingUrl` is set, the parameters will
+ // be appended at the end. If `uppercase` is `true`, the parameter names will
+ // be uppercased (e.g. `'?A=foo&B=bar'`)
getParamString: function (obj, existingUrl, uppercase) {
var params = [];
for (var i in obj) {
@@ -134,11 +189,19 @@ L.Util = {
}
return ((!existingUrl || existingUrl.indexOf('?') === -1) ? '?' : '&') + params.join('&');
},
+
+ // @function template(str: String, data: Object): String
+ // Simple templating facility, accepts a template string of the form `'Hello {a}, {b}'`
+ // and a data object like `{a: 'foo', b: 'bar'}`, returns evaluated string
+ // `('Hello foo, bar')`. You can also specify functions instead of strings for
+ // data values â they will be evaluated passing `data` as an argument.
template: function (str, data) {
- return str.replace(/\{ *([\w_]+) *\}/g, function (str, key) {
+ return str.replace(L.Util.templateRe, function (str, key) {
var value = data[key];
+
if (value === undefined) {
throw new Error('No value provided for variable ' + str);
+
} else if (typeof value === 'function') {
value = value(data);
}
@@ -146,30 +209,40 @@ L.Util = {
});
},
+ templateRe: /\{ *([\w_\-]+) *\}/g,
+
+ // @function isArray(obj): Boolean
+ // Compatibility polyfill for [Array.isArray](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)
isArray: Array.isArray || function (obj) {
return (Object.prototype.toString.call(obj) === '[object Array]');
},
+ // @function indexOf(array: Array, el: Object): Number
+ // Compatibility polyfill for [Array.prototype.indexOf](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)
+ indexOf: function (array, el) {
+ for (var i = 0; i < array.length; i++) {
+ if (array[i] === el) { return i; }
+ }
+ return -1;
+ },
+
+ // @property emptyImageUrl: String
+ // Data URI string containing a base64-encoded empty GIF image.
+ // Used as a hack to free memory from unused images on WebKit-powered
+ // mobile devices (by setting image `src` to this string).
emptyImageUrl: 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='
};
(function () {
-
// inspired by http://paulirish.com/2011/requestanimationframe-for-smart-animating/
function getPrefixed(name) {
- var i, fn,
- prefixes = ['webkit', 'moz', 'o', 'ms'];
-
- for (i = 0; i < prefixes.length && !fn; i++) {
- fn = window[prefixes[i] + name];
- }
-
- return fn;
+ return window['webkit' + name] || window['moz' + name] || window['ms' + name];
}
var lastTime = 0;
+ // fallback for IE 7-8
function timeoutDefer(fn) {
var time = +new Date(),
timeToCall = Math.max(0, 16 - (time - lastTime));
@@ -178,32 +251,33 @@ L.Util = {
return window.setTimeout(fn, timeToCall);
}
- var requestFn = window.requestAnimationFrame ||
- getPrefixed('RequestAnimationFrame') || timeoutDefer;
-
- var cancelFn = window.cancelAnimationFrame ||
- getPrefixed('CancelAnimationFrame') ||
- getPrefixed('CancelRequestAnimationFrame') ||
- function (id) { window.clearTimeout(id); };
+ var requestFn = window.requestAnimationFrame || getPrefixed('RequestAnimationFrame') || timeoutDefer,
+ cancelFn = window.cancelAnimationFrame || getPrefixed('CancelAnimationFrame') ||
+ getPrefixed('CancelRequestAnimationFrame') || function (id) { window.clearTimeout(id); };
- L.Util.requestAnimFrame = function (fn, context, immediate, element) {
- fn = L.bind(fn, context);
-
+ // @function requestAnimFrame(fn: Function, context?: Object, immediate?: Boolean): Number
+ // Schedules `fn` to be executed when the browser repaints. `fn` is bound to
+ // `context` if given. When `immediate` is set, `fn` is called immediately if
+ // the browser doesn't have native support for
+ // [`window.requestAnimationFrame`](https://developer.mozilla.org/docs/Web/API/window/requestAnimationFrame),
+ // otherwise it's delayed. Returns a request ID that can be used to cancel the request.
+ L.Util.requestAnimFrame = function (fn, context, immediate) {
if (immediate && requestFn === timeoutDefer) {
- fn();
+ fn.call(context);
} else {
- return requestFn.call(window, fn, element);
+ return requestFn.call(window, L.bind(fn, context));
}
};
+ // @function cancelAnimFrame(id: Number): undefined
+ // Cancels a previous `requestAnimFrame`. See also [window.cancelAnimationFrame](https://developer.mozilla.org/docs/Web/API/window/cancelAnimationFrame).
L.Util.cancelAnimFrame = function (id) {
if (id) {
cancelFn.call(window, id);
}
};
-
-}());
+})();
// shortcuts for most used utility functions
L.extend = L.Util.extend;
@@ -212,16 +286,23 @@ L.stamp = L.Util.stamp;
L.setOptions = L.Util.setOptions;
-/*
- * L.Class powers the OOP facilities of the library.
- * Thanks to John Resig and Dean Edwards for inspiration!
- */
+
+
+// @class Class
+// @aka L.Class
+
+// @section
+// @uninheritable
+
+// Thanks to John Resig and Dean Edwards for inspiration!
L.Class = function () {};
L.Class.extend = function (props) {
- // extended class with the new prototype
+ // @function extend(props: Object): Function
+ // [Extends the current class](#class-inheritance) given the properties to be included.
+ // Returns a Javascript function that is a class constructor (to be called with `new`).
var NewClass = function () {
// call the constructor
@@ -230,21 +311,17 @@ L.Class.extend = function (props) {
}
// call all constructor hooks
- if (this._initHooks) {
- this.callInitHooks();
- }
+ this.callInitHooks();
};
- // instantiate class without calling constructor
- var F = function () {};
- F.prototype = this.prototype;
+ var parentProto = NewClass.__super__ = this.prototype;
- var proto = new F();
+ var proto = L.Util.create(parentProto);
proto.constructor = NewClass;
NewClass.prototype = proto;
- //inherit parent's statics
+ // inherit parent's statics
for (var i in this) {
if (this.hasOwnProperty(i) && i !== 'prototype') {
NewClass[i] = this[i];
@@ -264,8 +341,8 @@ L.Class.extend = function (props) {
}
// merge options
- if (props.options && proto.options) {
- props.options = L.extend({}, proto.options, props.options);
+ if (proto.options) {
+ props.options = L.Util.extend(L.Util.create(proto.options), props.options);
}
// mix given properties into the prototype
@@ -273,17 +350,13 @@ L.Class.extend = function (props) {
proto._initHooks = [];
- var parent = this;
- // jshint camelcase: false
- NewClass.__super__ = parent.prototype;
-
// add method for calling all hooks
proto.callInitHooks = function () {
if (this._initHooksCalled) { return; }
- if (parent.prototype.callInitHooks) {
- parent.prototype.callInitHooks.call(this);
+ if (parentProto.callInitHooks) {
+ parentProto.callInitHooks.call(this);
}
this._initHooksCalled = true;
@@ -297,17 +370,22 @@ L.Class.extend = function (props) {
};
-// method for adding properties to prototype
+// @function include(properties: Object): this
+// [Includes a mixin](#class-includes) into the current class.
L.Class.include = function (props) {
L.extend(this.prototype, props);
+ return this;
};
-// merge new default options to the Class
+// @function mergeOptions(options: Object): this
+// [Merges `options`](#class-options) into the defaults of the class.
L.Class.mergeOptions = function (options) {
L.extend(this.prototype.options, options);
+ return this;
};
-// add a constructor hook
+// @function addInitHook(fn: Function): this
+// Adds a [constructor hook](#class-constructor-hooks) to the class.
L.Class.addInitHook = function (fn) { // (Function) || (String, args...)
var args = Array.prototype.slice.call(arguments, 1);
@@ -317,284 +395,504 @@ L.Class.addInitHook = function (fn) { // (Function) || (String, args...)
this.prototype._initHooks = this.prototype._initHooks || [];
this.prototype._initHooks.push(init);
+ return this;
};
+
/*
- * L.Mixin.Events is used to add custom events functionality to Leaflet classes.
+ * @class Evented
+ * @aka L.Evented
+ * @inherits Class
+ *
+ * A set of methods shared between event-powered classes (like `Map` and `Marker`). Generally, events allow you to execute some function when something happens with an object (e.g. the user clicks on the map, causing the map to fire `'click'` event).
+ *
+ * @example
+ *
+ * ```js
+ * map.on('click', function(e) {
+ * alert(e.latlng);
+ * } );
+ * ```
+ *
+ * Leaflet deals with event listeners by reference, so if you want to add a listener and then remove it, define it as a function:
+ *
+ * ```js
+ * function onClick(e) { ... }
+ *
+ * map.on('click', onClick);
+ * map.off('click', onClick);
+ * ```
*/
-var eventsKey = '_leaflet_events';
-
-L.Mixin = {};
-L.Mixin.Events = {
+L.Evented = L.Class.extend({
- addEventListener: function (types, fn, context) { // (String, Function[, Object]) or (Object[, Object])
+ /* @method on(type: String, fn: Function, context?: Object): this
+ * Adds a listener function (`fn`) to a particular event type of the object. You can optionally specify the context of the listener (object the this keyword will point to). You can also pass several space-separated types (e.g. `'click dblclick'`).
+ *
+ * @alternative
+ * @method on(eventMap: Object): this
+ * Adds a set of type/listener pairs, e.g. `{click: onClick, mousemove: onMouseMove}`
+ */
+ on: function (types, fn, context) {
// types can be a map of types/handlers
- if (L.Util.invokeEach(types, this.addEventListener, this, fn, context)) { return this; }
-
- var events = this[eventsKey] = this[eventsKey] || {},
- contextId = context && context !== this && L.stamp(context),
- i, len, event, type, indexKey, indexLenKey, typeIndex;
-
- // types can be a string of space-separated words
- types = L.Util.splitWords(types);
-
- for (i = 0, len = types.length; i < len; i++) {
- event = {
- action: fn,
- context: context || this
- };
- type = types[i];
+ if (typeof types === 'object') {
+ for (var type in types) {
+ // we don't process space-separated events here for performance;
+ // it's a hot path since Layer uses the on(obj) syntax
+ this._on(type, types[type], fn);
+ }
- if (contextId) {
- // store listeners of a particular context in a separate hash (if it has an id)
- // gives a major performance boost when removing thousands of map layers
+ } else {
+ // types can be a string of space-separated words
+ types = L.Util.splitWords(types);
- indexKey = type + '_idx';
- indexLenKey = indexKey + '_len';
+ for (var i = 0, len = types.length; i < len; i++) {
+ this._on(types[i], fn, context);
+ }
+ }
- typeIndex = events[indexKey] = events[indexKey] || {};
+ return this;
+ },
- if (!typeIndex[contextId]) {
- typeIndex[contextId] = [];
+ /* @method off(type: String, fn?: Function, context?: Object): this
+ * Removes a previously added listener function. If no function is specified, it will remove all the listeners of that particular event from the object. Note that if you passed a custom context to `on`, you must pass the same context to `off` in order to remove the listener.
+ *
+ * @alternative
+ * @method off(eventMap: Object): this
+ * Removes a set of type/listener pairs.
+ *
+ * @alternative
+ * @method off: this
+ * Removes all listeners to all events on the object.
+ */
+ off: function (types, fn, context) {
- // keep track of the number of keys in the index to quickly check if it's empty
- events[indexLenKey] = (events[indexLenKey] || 0) + 1;
- }
+ if (!types) {
+ // clear all listeners if called without arguments
+ delete this._events;
- typeIndex[contextId].push(event);
+ } else if (typeof types === 'object') {
+ for (var type in types) {
+ this._off(type, types[type], fn);
+ }
+ } else {
+ types = L.Util.splitWords(types);
- } else {
- events[type] = events[type] || [];
- events[type].push(event);
+ for (var i = 0, len = types.length; i < len; i++) {
+ this._off(types[i], fn, context);
}
}
return this;
},
- hasEventListeners: function (type) { // (String) -> Boolean
- var events = this[eventsKey];
- return !!events && ((type in events && events[type].length > 0) ||
- (type + '_idx' in events && events[type + '_idx_len'] > 0));
- },
-
- removeEventListener: function (types, fn, context) { // ([String, Function, Object]) or (Object[, Object])
+ // attach listener (without syntactic sugar now)
+ _on: function (type, fn, context) {
+ this._events = this._events || {};
- if (!this[eventsKey]) {
- return this;
+ /* get/init listeners for type */
+ var typeListeners = this._events[type];
+ if (!typeListeners) {
+ typeListeners = [];
+ this._events[type] = typeListeners;
}
- if (!types) {
- return this.clearAllEventListeners();
+ if (context === this) {
+ // Less memory footprint.
+ context = undefined;
}
+ var newListener = {fn: fn, ctx: context},
+ listeners = typeListeners;
- if (L.Util.invokeEach(types, this.removeEventListener, this, fn, context)) { return this; }
-
- var events = this[eventsKey],
- contextId = context && context !== this && L.stamp(context),
- i, len, type, listeners, j, indexKey, indexLenKey, typeIndex, removed;
+ // check if fn already there
+ for (var i = 0, len = listeners.length; i < len; i++) {
+ if (listeners[i].fn === fn && listeners[i].ctx === context) {
+ return;
+ }
+ }
- types = L.Util.splitWords(types);
+ listeners.push(newListener);
+ typeListeners.count++;
+ },
- for (i = 0, len = types.length; i < len; i++) {
- type = types[i];
- indexKey = type + '_idx';
- indexLenKey = indexKey + '_len';
+ _off: function (type, fn, context) {
+ var listeners,
+ i,
+ len;
- typeIndex = events[indexKey];
+ if (!this._events) { return; }
- if (!fn) {
- // clear all listeners for a type if function isn't specified
- delete events[type];
- delete events[indexKey];
- delete events[indexLenKey];
+ listeners = this._events[type];
- } else {
- listeners = contextId && typeIndex ? typeIndex[contextId] : events[type];
-
- if (listeners) {
- for (j = listeners.length - 1; j >= 0; j--) {
- if ((listeners[j].action === fn) && (!context || (listeners[j].context === context))) {
- removed = listeners.splice(j, 1);
- // set the old action to a no-op, because it is possible
- // that the listener is being iterated over as part of a dispatch
- removed[0].action = L.Util.falseFn;
- }
- }
+ if (!listeners) {
+ return;
+ }
- if (context && typeIndex && (listeners.length === 0)) {
- delete typeIndex[contextId];
- events[indexLenKey]--;
- }
- }
+ if (!fn) {
+ // Set all removed listeners to noop so they are not called if remove happens in fire
+ for (i = 0, len = listeners.length; i < len; i++) {
+ listeners[i].fn = L.Util.falseFn;
}
+ // clear all listeners for a type if function isn't specified
+ delete this._events[type];
+ return;
}
- return this;
- },
-
- clearAllEventListeners: function () {
- delete this[eventsKey];
- return this;
- },
-
- fireEvent: function (type, data) { // (String[, Object])
- if (!this.hasEventListeners(type)) {
- return this;
+ if (context === this) {
+ context = undefined;
}
- var event = L.Util.extend({}, data, { type: type, target: this });
+ if (listeners) {
- var events = this[eventsKey],
- listeners, i, len, typeIndex, contextId;
+ // find fn and remove it
+ for (i = 0, len = listeners.length; i < len; i++) {
+ var l = listeners[i];
+ if (l.ctx !== context) { continue; }
+ if (l.fn === fn) {
- if (events[type]) {
- // make sure adding/removing listeners inside other listeners won't cause infinite loop
- listeners = events[type].slice();
+ // set the removed listener to noop so that's not called if remove happens in fire
+ l.fn = L.Util.falseFn;
- for (i = 0, len = listeners.length; i < len; i++) {
- listeners[i].action.call(listeners[i].context, event);
+ if (this._firingCount) {
+ /* copy array in case events are being fired */
+ this._events[type] = listeners = listeners.slice();
+ }
+ listeners.splice(i, 1);
+
+ return;
+ }
}
}
+ },
+
+ // @method fire(type: String, data?: Object, propagate?: Boolean): this
+ // Fires an event of the specified type. You can optionally provide an data
+ // object â the first argument of the listener function will contain its
+ // properties. The event might can optionally be propagated to event parents.
+ fire: function (type, data, propagate) {
+ if (!this.listens(type, propagate)) { return this; }
- // fire event for the context-indexed listeners as well
- typeIndex = events[type + '_idx'];
+ var event = L.Util.extend({}, data, {type: type, target: this});
- for (contextId in typeIndex) {
- listeners = typeIndex[contextId].slice();
+ if (this._events) {
+ var listeners = this._events[type];
if (listeners) {
- for (i = 0, len = listeners.length; i < len; i++) {
- listeners[i].action.call(listeners[i].context, event);
+ this._firingCount = (this._firingCount + 1) || 1;
+ for (var i = 0, len = listeners.length; i < len; i++) {
+ var l = listeners[i];
+ l.fn.call(l.ctx || this, event);
}
+
+ this._firingCount--;
}
}
+ if (propagate) {
+ // propagate the event to parents (set with addEventParent)
+ this._propagateEvent(event);
+ }
+
return this;
},
- addOneTimeEventListener: function (types, fn, context) {
+ // @method listens(type: String): Boolean
+ // Returns `true` if a particular event type has any listeners attached to it.
+ listens: function (type, propagate) {
+ var listeners = this._events && this._events[type];
+ if (listeners && listeners.length) { return true; }
+
+ if (propagate) {
+ // also check parents for listeners if event propagates
+ for (var id in this._eventParents) {
+ if (this._eventParents[id].listens(type, propagate)) { return true; }
+ }
+ }
+ return false;
+ },
+
+ // @method once(â¦): this
+ // Behaves as [`on(â¦)`](#evented-on), except the listener will only get fired once and then removed.
+ once: function (types, fn, context) {
- if (L.Util.invokeEach(types, this.addOneTimeEventListener, this, fn, context)) { return this; }
+ if (typeof types === 'object') {
+ for (var type in types) {
+ this.once(type, types[type], fn);
+ }
+ return this;
+ }
var handler = L.bind(function () {
this
- .removeEventListener(types, fn, context)
- .removeEventListener(types, handler, context);
+ .off(types, fn, context)
+ .off(types, handler, context);
}, this);
+ // add a listener that's executed once and removed after that
return this
- .addEventListener(types, fn, context)
- .addEventListener(types, handler, context);
+ .on(types, fn, context)
+ .on(types, handler, context);
+ },
+
+ // @method addEventParent(obj: Evented): this
+ // Adds an event parent - an `Evented` that will receive propagated events
+ addEventParent: function (obj) {
+ this._eventParents = this._eventParents || {};
+ this._eventParents[L.stamp(obj)] = obj;
+ return this;
+ },
+
+ // @method removeEventParent(obj: Evented): this
+ // Removes an event parent, so it will stop receiving propagated events
+ removeEventParent: function (obj) {
+ if (this._eventParents) {
+ delete this._eventParents[L.stamp(obj)];
+ }
+ return this;
+ },
+
+ _propagateEvent: function (e) {
+ for (var id in this._eventParents) {
+ this._eventParents[id].fire(e.type, L.extend({layer: e.target}, e), true);
+ }
}
-};
+});
+
+var proto = L.Evented.prototype;
+
+// aliases; we should ditch those eventually
+
+// @method addEventListener(â¦): this
+// Alias to [`on(â¦)`](#evented-on)
+proto.addEventListener = proto.on;
+
+// @method removeEventListener(â¦): this
+// Alias to [`off(â¦)`](#evented-off)
+
+// @method clearAllEventListeners(â¦): this
+// Alias to [`off()`](#evented-off)
+proto.removeEventListener = proto.clearAllEventListeners = proto.off;
+
+// @method addOneTimeEventListener(â¦): this
+// Alias to [`once(â¦)`](#evented-once)
+proto.addOneTimeEventListener = proto.once;
+
+// @method fireEvent(â¦): this
+// Alias to [`fire(â¦)`](#evented-fire)
+proto.fireEvent = proto.fire;
+
+// @method hasEventListeners(â¦): Boolean
+// Alias to [`listens(â¦)`](#evented-listens)
+proto.hasEventListeners = proto.listens;
+
+L.Mixin = {Events: proto};
-L.Mixin.Events.on = L.Mixin.Events.addEventListener;
-L.Mixin.Events.off = L.Mixin.Events.removeEventListener;
-L.Mixin.Events.once = L.Mixin.Events.addOneTimeEventListener;
-L.Mixin.Events.fire = L.Mixin.Events.fireEvent;
/*
- * L.Browser handles different browser and feature detections for internal Leaflet use.
+ * @namespace Browser
+ * @aka L.Browser
+ *
+ * A namespace with static properties for browser/feature detection used by Leaflet internally.
+ *
+ * @example
+ *
+ * ```js
+ * if (L.Browser.ielt9) {
+ * alert('Upgrade your browser, dude!');
+ * }
+ * ```
*/
(function () {
- var ie = 'ActiveXObject' in window,
- ielt9 = ie && !document.addEventListener,
+ var ua = navigator.userAgent.toLowerCase(),
+ doc = document.documentElement,
+
+ ie = 'ActiveXObject' in window,
- // terrible browser detection to work around Safari / iOS / Android browser bugs
- ua = navigator.userAgent.toLowerCase(),
- webkit = ua.indexOf('webkit') !== -1,
- chrome = ua.indexOf('chrome') !== -1,
+ webkit = ua.indexOf('webkit') !== -1,
phantomjs = ua.indexOf('phantom') !== -1,
- android = ua.indexOf('android') !== -1,
android23 = ua.search('android [23]') !== -1,
- gecko = ua.indexOf('gecko') !== -1,
+ chrome = ua.indexOf('chrome') !== -1,
+ gecko = ua.indexOf('gecko') !== -1 && !webkit && !window.opera && !ie,
- mobile = typeof orientation !== undefined + '',
+ win = navigator.platform.indexOf('Win') === 0,
+
+ mobile = typeof orientation !== 'undefined' || ua.indexOf('mobile') !== -1,
msPointer = !window.PointerEvent && window.MSPointerEvent,
- pointer = (window.PointerEvent && window.navigator.pointerEnabled) ||
- msPointer,
- retina = ('devicePixelRatio' in window && window.devicePixelRatio > 1) ||
- ('matchMedia' in window && window.matchMedia('(min-resolution:144dpi)') &&
- window.matchMedia('(min-resolution:144dpi)').matches),
+ pointer = window.PointerEvent || msPointer,
- doc = document.documentElement,
ie3d = ie && ('transition' in doc.style),
webkit3d = ('WebKitCSSMatrix' in window) && ('m11' in new window.WebKitCSSMatrix()) && !android23,
gecko3d = 'MozPerspective' in doc.style,
- opera3d = 'OTransition' in doc.style,
- any3d = !window.L_DISABLE_3D && (ie3d || webkit3d || gecko3d || opera3d) && !phantomjs;
+ opera12 = 'OTransition' in doc.style;
- var touch = !window.L_NO_TOUCH && !phantomjs && (pointer || 'ontouchstart' in window ||
- (window.DocumentTouch && document instanceof window.DocumentTouch));
+
+ var touch = !window.L_NO_TOUCH && (pointer || 'ontouchstart' in window ||
+ (window.DocumentTouch && document instanceof window.DocumentTouch));
L.Browser = {
+
+ // @property ie: Boolean
+ // `true` for all Internet Explorer versions (not Edge).
ie: ie,
- ielt9: ielt9,
+
+ // @property ielt9: Boolean
+ // `true` for Internet Explorer versions less than 9.
+ ielt9: ie && !document.addEventListener,
+
+ // @property edge: Boolean
+ // `true` for the Edge web browser.
+ edge: 'msLaunchUri' in navigator && !('documentMode' in document),
+
+ // @property webkit: Boolean
+ // `true` for webkit-based browsers like Chrome and Safari (including mobile versions).
webkit: webkit,
- gecko: gecko && !webkit && !window.opera && !ie,
- android: android,
+ // @property gecko: Boolean
+ // `true` for gecko-based browsers like Firefox.
+ gecko: gecko,
+
+ // @property android: Boolean
+ // `true` for any browser running on an Android platform.
+ android: ua.indexOf('android') !== -1,
+
+ // @property android23: Boolean
+ // `true` for browsers running on Android 2 or Android 3.
android23: android23,
+ // @property chrome: Boolean
+ // `true` for the Chrome browser.
chrome: chrome,
+ // @property safari: Boolean
+ // `true` for the Safari browser.
+ safari: !chrome && ua.indexOf('safari') !== -1,
+
+
+ // @property win: Boolean
+ // `true` when the browser is running in a Windows platform
+ win: win,
+
+
+ // @property ie3d: Boolean
+ // `true` for all Internet Explorer versions supporting CSS transforms.
ie3d: ie3d,
+
+ // @property webkit3d: Boolean
+ // `true` for webkit-based browsers supporting CSS transforms.
webkit3d: webkit3d,
+
+ // @property gecko3d: Boolean
+ // `true` for gecko-based browsers supporting CSS transforms.
gecko3d: gecko3d,
- opera3d: opera3d,
- any3d: any3d,
+ // @property opera12: Boolean
+ // `true` for the Opera browser supporting CSS transforms (version 12 or later).
+ opera12: opera12,
+
+ // @property any3d: Boolean
+ // `true` for all browsers supporting CSS transforms.
+ any3d: !window.L_DISABLE_3D && (ie3d || webkit3d || gecko3d) && !opera12 && !phantomjs,
+
+
+ // @property mobile: Boolean
+ // `true` for all browsers running in a mobile device.
mobile: mobile,
+
+ // @property mobileWebkit: Boolean
+ // `true` for all webkit-based browsers in a mobile device.
mobileWebkit: mobile && webkit,
+
+ // @property mobileWebkit3d: Boolean
+ // `true` for all webkit-based browsers in a mobile device supporting CSS transforms.
mobileWebkit3d: mobile && webkit3d,
+
+ // @property mobileOpera: Boolean
+ // `true` for the Opera browser in a mobile device.
mobileOpera: mobile && window.opera,
- touch: touch,
- msPointer: msPointer,
- pointer: pointer,
+ // @property mobileGecko: Boolean
+ // `true` for gecko-based browsers running in a mobile device.
+ mobileGecko: mobile && gecko,
+
- retina: retina
+ // @property touch: Boolean
+ // `true` for all browsers supporting [touch events](https://developer.mozilla.org/docs/Web/API/Touch_events).
+ touch: !!touch,
+
+ // @property msPointer: Boolean
+ // `true` for browsers implementing the Microsoft touch events model (notably IE10).
+ msPointer: !!msPointer,
+
+ // @property pointer: Boolean
+ // `true` for all browsers supporting [pointer events](https://msdn.microsoft.com/en-us/library/dn433244%28v=vs.85%29.aspx).
+ pointer: !!pointer,
+
+
+ // @property retina: Boolean
+ // `true` for browsers on a high-resolution "retina" screen.
+ retina: (window.devicePixelRatio || (window.screen.deviceXDPI / window.screen.logicalXDPI)) > 1
};
}());
+
/*
- * L.Point represents a point with x and y coordinates.
+ * @class Point
+ * @aka L.Point
+ *
+ * Represents a point with `x` and `y` coordinates in pixels.
+ *
+ * @example
+ *
+ * ```js
+ * var point = L.point(200, 300);
+ * ```
+ *
+ * All Leaflet methods and options that accept `Point` objects also accept them in a simple Array form (unless noted otherwise), so these lines are equivalent:
+ *
+ * ```js
+ * map.panBy([200, 300]);
+ * map.panBy(L.point(200, 300));
+ * ```
*/
-L.Point = function (/*Number*/ x, /*Number*/ y, /*Boolean*/ round) {
+L.Point = function (x, y, round) {
this.x = (round ? Math.round(x) : x);
this.y = (round ? Math.round(y) : y);
};
L.Point.prototype = {
+ // @method clone(): Point
+ // Returns a copy of the current point.
clone: function () {
return new L.Point(this.x, this.y);
},
- // non-destructive, returns a new point
+ // @method add(otherPoint: Point): Point
+ // Returns the result of addition of the current and the given points.
add: function (point) {
+ // non-destructive, returns a new point
return this.clone()._add(L.point(point));
},
- // destructive, used directly for performance in situations where it's safe to modify existing point
_add: function (point) {
+ // destructive, used directly for performance in situations where it's safe to modify existing point
this.x += point.x;
this.y += point.y;
return this;
},
+ // @method subtract(otherPoint: Point): Point
+ // Returns the result of subtraction of the given point from the current.
subtract: function (point) {
return this.clone()._subtract(L.point(point));
},
@@ -605,6 +903,8 @@ L.Point.prototype = {
return this;
},
+ // @method divideBy(num: Number): Point
+ // Returns the result of division of the current point by the given number.
divideBy: function (num) {
return this.clone()._divideBy(num);
},
@@ -615,6 +915,8 @@ L.Point.prototype = {
return this;
},
+ // @method multiplyBy(num: Number): Point
+ // Returns the result of multiplication of the current point by the given number.
multiplyBy: function (num) {
return this.clone()._multiplyBy(num);
},
@@ -625,6 +927,24 @@ L.Point.prototype = {
return this;
},
+ // @method scaleBy(scale: Point): Point
+ // Multiply each coordinate of the current point by each coordinate of
+ // `scale`. In linear algebra terms, multiply the point by the
+ // [scaling matrix](https://en.wikipedia.org/wiki/Scaling_%28geometry%29#Matrix_representation)
+ // defined by `scale`.
+ scaleBy: function (point) {
+ return new L.Point(this.x * point.x, this.y * point.y);
+ },
+
+ // @method unscaleBy(scale: Point): Point
+ // Inverse of `scaleBy`. Divide each coordinate of the current point by
+ // each coordinate of `scale`.
+ unscaleBy: function (point) {
+ return new L.Point(this.x / point.x, this.y / point.y);
+ },
+
+ // @method round(): Point
+ // Returns a copy of the current point with rounded coordinates.
round: function () {
return this.clone()._round();
},
@@ -635,6 +955,8 @@ L.Point.prototype = {
return this;
},
+ // @method floor(): Point
+ // Returns a copy of the current point with floored coordinates (rounded down).
floor: function () {
return this.clone()._floor();
},
@@ -645,6 +967,20 @@ L.Point.prototype = {
return this;
},
+ // @method ceil(): Point
+ // Returns a copy of the current point with ceiled coordinates (rounded up).
+ ceil: function () {
+ return this.clone()._ceil();
+ },
+
+ _ceil: function () {
+ this.x = Math.ceil(this.x);
+ this.y = Math.ceil(this.y);
+ return this;
+ },
+
+ // @method distanceTo(otherPoint: Point): Number
+ // Returns the cartesian distance between the current and the given points.
distanceTo: function (point) {
point = L.point(point);
@@ -654,6 +990,8 @@ L.Point.prototype = {
return Math.sqrt(x * x + y * y);
},
+ // @method equals(otherPoint: Point): Boolean
+ // Returns `true` if the given point has the same coordinates.
equals: function (point) {
point = L.point(point);
@@ -661,6 +999,8 @@ L.Point.prototype = {
point.y === this.y;
},
+ // @method contains(otherPoint: Point): Boolean
+ // Returns `true` if both coordinates of the given point are less than the corresponding current point coordinates (in absolute values).
contains: function (point) {
point = L.point(point);
@@ -668,6 +1008,8 @@ L.Point.prototype = {
Math.abs(point.y) <= Math.abs(this.y);
},
+ // @method toString(): String
+ // Returns a string representation of the point for debugging purposes.
toString: function () {
return 'Point(' +
L.Util.formatNum(this.x) + ', ' +
@@ -675,6 +1017,16 @@ L.Point.prototype = {
}
};
+// @factory L.point(x: Number, y: Number, round?: Boolean)
+// Creates a Point object with the given `x` and `y` coordinates. If optional `round` is set to true, rounds the `x` and `y` values.
+
+// @alternative
+// @factory L.point(coords: Number[])
+// Expects an array of the form `[x, y]` instead.
+
+// @alternative
+// @factory L.point(coords: Object)
+// Expects a plain object of the form `{x: Number, y: Number}` instead.
L.point = function (x, y, round) {
if (x instanceof L.Point) {
return x;
@@ -685,15 +1037,36 @@ L.point = function (x, y, round) {
if (x === undefined || x === null) {
return x;
}
+ if (typeof x === 'object' && 'x' in x && 'y' in x) {
+ return new L.Point(x.x, x.y);
+ }
return new L.Point(x, y, round);
};
+
/*
- * L.Bounds represents a rectangular area on the screen in pixel coordinates.
+ * @class Bounds
+ * @aka L.Bounds
+ *
+ * Represents a rectangular area in pixel coordinates.
+ *
+ * @example
+ *
+ * ```js
+ * var p1 = L.point(10, 10),
+ * p2 = L.point(40, 60),
+ * bounds = L.bounds(p1, p2);
+ * ```
+ *
+ * All Leaflet methods that accept `Bounds` objects also accept them in a simple Array form (unless noted otherwise), so the bounds example above can be passed like this:
+ *
+ * ```js
+ * otherBounds.intersects([[10, 10], [40, 60]]);
+ * ```
*/
-L.Bounds = function (a, b) { //(Point, Point) or Point[]
+L.Bounds = function (a, b) {
if (!a) { return; }
var points = b ? [a, b] : a;
@@ -704,10 +1077,15 @@ L.Bounds = function (a, b) { //(Point, Point) or Point[]
};
L.Bounds.prototype = {
- // extend the bounds to contain the given point
+ // @method extend(point: Point): this
+ // Extends the bounds to contain the given point.
extend: function (point) { // (Point)
point = L.point(point);
+ // @property min: Point
+ // The top left corner of the rectangle.
+ // @property max: Point
+ // The bottom right corner of the rectangle.
if (!this.min && !this.max) {
this.min = point.clone();
this.max = point.clone();
@@ -720,25 +1098,38 @@ L.Bounds.prototype = {
return this;
},
- getCenter: function (round) { // (Boolean) -> Point
+ // @method getCenter(round?: Boolean): Point
+ // Returns the center point of the bounds.
+ getCenter: function (round) {
return new L.Point(
(this.min.x + this.max.x) / 2,
(this.min.y + this.max.y) / 2, round);
},
- getBottomLeft: function () { // -> Point
+ // @method getBottomLeft(): Point
+ // Returns the bottom-left point of the bounds.
+ getBottomLeft: function () {
return new L.Point(this.min.x, this.max.y);
},
+ // @method getTopRight(): Point
+ // Returns the top-right point of the bounds.
getTopRight: function () { // -> Point
return new L.Point(this.max.x, this.min.y);
},
+ // @method getSize(): Point
+ // Returns the size of the given bounds
getSize: function () {
return this.max.subtract(this.min);
},
- contains: function (obj) { // (Bounds) or (Point) -> Boolean
+ // @method contains(otherBounds: Bounds): Boolean
+ // Returns `true` if the rectangle contains the given one.
+ // @alternative
+ // @method contains(point: Point): Boolean
+ // Returns `true` if the rectangle contains the given point.
+ contains: function (obj) {
var min, max;
if (typeof obj[0] === 'number' || obj instanceof L.Point) {
@@ -760,6 +1151,9 @@ L.Bounds.prototype = {
(max.y <= this.max.y);
},
+ // @method intersects(otherBounds: Bounds): Boolean
+ // Returns `true` if the rectangle intersects the given bounds. Two bounds
+ // intersect if they have at least one point in common.
intersects: function (bounds) { // (Bounds) -> Boolean
bounds = L.bounds(bounds);
@@ -773,12 +1167,34 @@ L.Bounds.prototype = {
return xIntersects && yIntersects;
},
+ // @method overlaps(otherBounds: Bounds): Boolean
+ // Returns `true` if the rectangle overlaps the given bounds. Two bounds
+ // overlap if their intersection is an area.
+ overlaps: function (bounds) { // (Bounds) -> Boolean
+ bounds = L.bounds(bounds);
+
+ var min = this.min,
+ max = this.max,
+ min2 = bounds.min,
+ max2 = bounds.max,
+ xOverlaps = (max2.x > min.x) && (min2.x < max.x),
+ yOverlaps = (max2.y > min.y) && (min2.y < max.y);
+
+ return xOverlaps && yOverlaps;
+ },
+
isValid: function () {
return !!(this.min && this.max);
}
};
-L.bounds = function (a, b) { // (Bounds) or (Point, Point) or (Point[])
+
+// @factory L.bounds(topLeft: Point, bottomRight: Point)
+// Creates a Bounds object from two coordinates (usually top-left and bottom-right corners).
+// @alternative
+// @factory L.bounds(points: Point[])
+// Creates a Bounds object from the points it contains
+L.bounds = function (a, b) {
if (!a || a instanceof L.Bounds) {
return a;
}
@@ -786,10 +1202,28 @@ L.bounds = function (a, b) { // (Bounds) or (Point, Point) or (Point[])
};
+
/*
- * L.Transformation is an utility class to perform simple point transformations through a 2d-matrix.
+ * @class Transformation
+ * @aka L.Transformation
+ *
+ * Represents an affine transformation: a set of coefficients `a`, `b`, `c`, `d`
+ * for transforming a point of a form `(x, y)` into `(a*x + b, c*y + d)` and doing
+ * the reverse. Used by Leaflet in its projections code.
+ *
+ * @example
+ *
+ * ```js
+ * var transformation = new L.Transformation(2, 5, -1, 10),
+ * p = L.point(1, 2),
+ * p2 = transformation.transform(p), // L.point(7, 8)
+ * p3 = transformation.untransform(p2); // L.point(1, 2)
+ * ```
*/
+
+// factory new L.Transformation(a: Number, b: Number, c: Number, d: Number)
+// Creates a `Transformation` object with the given coefficients.
L.Transformation = function (a, b, c, d) {
this._a = a;
this._b = b;
@@ -798,6 +1232,9 @@ L.Transformation = function (a, b, c, d) {
};
L.Transformation.prototype = {
+ // @method transform(point: Point, scale?: Number): Point
+ // Returns a transformed point, optionally multiplied by the given scale.
+ // Only accepts real `L.Point` instances, not arrays.
transform: function (point, scale) { // (Point, Number) -> Point
return this._transform(point.clone(), scale);
},
@@ -810,6 +1247,9 @@ L.Transformation.prototype = {
return point;
},
+ // @method untransform(point: Point, scale?: Number): Point
+ // Returns the reverse transformation of the given point, optionally divided
+ // by the given scale. Only accepts real `L.Point` instances, not arrays.
untransform: function (point, scale) {
scale = scale || 1;
return new L.Point(
@@ -819,22 +1259,33 @@ L.Transformation.prototype = {
};
+
/*
- * L.DomUtil contains various utility functions for working with DOM.
+ * @namespace DomUtil
+ *
+ * Utility functions to work with the [DOM](https://developer.mozilla.org/docs/Web/API/Document_Object_Model)
+ * tree, used by Leaflet internally.
+ *
+ * Most functions expecting or returning a `HTMLElement` also work for
+ * SVG elements. The only difference is that classes refer to CSS classes
+ * in HTML and SVG classes in SVG.
*/
L.DomUtil = {
+
+ // @function get(id: String|HTMLElement): HTMLElement
+ // Returns an element given its DOM id, or returns the element itself
+ // if it was passed directly.
get: function (id) {
- return (typeof id === 'string' ? document.getElementById(id) : id);
+ return typeof id === 'string' ? document.getElementById(id) : id;
},
+ // @function getStyle(el: HTMLElement, styleAttrib: String): String
+ // Returns the value for a certain style attribute on an element,
+ // including computed values or values set through CSS.
getStyle: function (el, style) {
- var value = el.style[style];
-
- if (!value && el.currentStyle) {
- value = el.currentStyle[style];
- }
+ var value = el.style[style] || (el.currentStyle && el.currentStyle[style]);
if ((!value || value === 'auto') && document.defaultView) {
var css = document.defaultView.getComputedStyle(el, null);
@@ -844,94 +1295,62 @@ L.DomUtil = {
return value === 'auto' ? null : value;
},
- getViewportOffset: function (element) {
-
- var top = 0,
- left = 0,
- el = element,
- docBody = document.body,
- docEl = document.documentElement,
- pos;
-
- do {
- top += el.offsetTop || 0;
- left += el.offsetLeft || 0;
-
- //add borders
- top += parseInt(L.DomUtil.getStyle(el, 'borderTopWidth'), 10) || 0;
- left += parseInt(L.DomUtil.getStyle(el, 'borderLeftWidth'), 10) || 0;
-
- pos = L.DomUtil.getStyle(el, 'position');
-
- if (el.offsetParent === docBody && pos === 'absolute') { break; }
-
- if (pos === 'fixed') {
- top += docBody.scrollTop || docEl.scrollTop || 0;
- left += docBody.scrollLeft || docEl.scrollLeft || 0;
- break;
- }
-
- if (pos === 'relative' && !el.offsetLeft) {
- var width = L.DomUtil.getStyle(el, 'width'),
- maxWidth = L.DomUtil.getStyle(el, 'max-width'),
- r = el.getBoundingClientRect();
-
- if (width !== 'none' || maxWidth !== 'none') {
- left += r.left + el.clientLeft;
- }
-
- //calculate full y offset since we're breaking out of the loop
- top += r.top + (docBody.scrollTop || docEl.scrollTop || 0);
-
- break;
- }
-
- el = el.offsetParent;
-
- } while (el);
-
- el = element;
-
- do {
- if (el === docBody) { break; }
+ // @function create(tagName: String, className?: String, container?: HTMLElement): HTMLElement
+ // Creates an HTML element with `tagName`, sets its class to `className`, and optionally appends it to `container` element.
+ create: function (tagName, className, container) {
- top -= el.scrollTop || 0;
- left -= el.scrollLeft || 0;
+ var el = document.createElement(tagName);
+ el.className = className || '';
- el = el.parentNode;
- } while (el);
+ if (container) {
+ container.appendChild(el);
+ }
- return new L.Point(left, top);
+ return el;
},
- documentIsLtr: function () {
- if (!L.DomUtil._docIsLtrCached) {
- L.DomUtil._docIsLtrCached = true;
- L.DomUtil._docIsLtr = L.DomUtil.getStyle(document.body, 'direction') === 'ltr';
+ // @function remove(el: HTMLElement)
+ // Removes `el` from its parent element
+ remove: function (el) {
+ var parent = el.parentNode;
+ if (parent) {
+ parent.removeChild(el);
}
- return L.DomUtil._docIsLtr;
},
- create: function (tagName, className, container) {
-
- var el = document.createElement(tagName);
- el.className = className;
-
- if (container) {
- container.appendChild(el);
+ // @function empty(el: HTMLElement)
+ // Removes all of `el`'s children elements from `el`
+ empty: function (el) {
+ while (el.firstChild) {
+ el.removeChild(el.firstChild);
}
+ },
- return el;
+ // @function toFront(el: HTMLElement)
+ // Makes `el` the last children of its parent, so it renders in front of the other children.
+ toFront: function (el) {
+ el.parentNode.appendChild(el);
},
+ // @function toBack(el: HTMLElement)
+ // Makes `el` the first children of its parent, so it renders back from the other children.
+ toBack: function (el) {
+ var parent = el.parentNode;
+ parent.insertBefore(el, parent.firstChild);
+ },
+
+ // @function hasClass(el: HTMLElement, name: String): Boolean
+ // Returns `true` if the element's class attribute contains `name`.
hasClass: function (el, name) {
if (el.classList !== undefined) {
return el.classList.contains(name);
}
- var className = L.DomUtil._getClass(el);
+ var className = L.DomUtil.getClass(el);
return className.length > 0 && new RegExp('(^|\\s)' + name + '(\\s|$)').test(className);
},
+ // @function addClass(el: HTMLElement, name: String)
+ // Adds `name` to the element's class attribute.
addClass: function (el, name) {
if (el.classList !== undefined) {
var classes = L.Util.splitWords(name);
@@ -939,20 +1358,24 @@ L.DomUtil = {
el.classList.add(classes[i]);
}
} else if (!L.DomUtil.hasClass(el, name)) {
- var className = L.DomUtil._getClass(el);
- L.DomUtil._setClass(el, (className ? className + ' ' : '') + name);
+ var className = L.DomUtil.getClass(el);
+ L.DomUtil.setClass(el, (className ? className + ' ' : '') + name);
}
},
+ // @function removeClass(el: HTMLElement, name: String)
+ // Removes `name` from the element's class attribute.
removeClass: function (el, name) {
if (el.classList !== undefined) {
el.classList.remove(name);
} else {
- L.DomUtil._setClass(el, L.Util.trim((' ' + L.DomUtil._getClass(el) + ' ').replace(' ' + name + ' ', ' ')));
+ L.DomUtil.setClass(el, L.Util.trim((' ' + L.DomUtil.getClass(el) + ' ').replace(' ' + name + ' ', ' ')));
}
},
- _setClass: function (el, name) {
+ // @function setClass(el: HTMLElement, name: String)
+ // Sets the element's class.
+ setClass: function (el, name) {
if (el.className.baseVal === undefined) {
el.className = name;
} else {
@@ -961,40 +1384,52 @@ L.DomUtil = {
}
},
- _getClass: function (el) {
+ // @function getClass(el: HTMLElement): String
+ // Returns the element's class.
+ getClass: function (el) {
return el.className.baseVal === undefined ? el.className : el.className.baseVal;
},
+ // @function setOpacity(el: HTMLElement, opacity: Number)
+ // Set the opacity of an element (including old IE support).
+ // `opacity` must be a number from `0` to `1`.
setOpacity: function (el, value) {
if ('opacity' in el.style) {
el.style.opacity = value;
} else if ('filter' in el.style) {
+ L.DomUtil._setOpacityIE(el, value);
+ }
+ },
- var filter = false,
- filterName = 'DXImageTransform.Microsoft.Alpha';
+ _setOpacityIE: function (el, value) {
+ var filter = false,
+ filterName = 'DXImageTransform.Microsoft.Alpha';
- // filters collection throws an error if we try to retrieve a filter that doesn't exist
- try {
- filter = el.filters.item(filterName);
- } catch (e) {
- // don't set opacity to 1 if we haven't already set an opacity,
- // it isn't needed and breaks transparent pngs.
- if (value === 1) { return; }
- }
+ // filters collection throws an error if we try to retrieve a filter that doesn't exist
+ try {
+ filter = el.filters.item(filterName);
+ } catch (e) {
+ // don't set opacity to 1 if we haven't already set an opacity,
+ // it isn't needed and breaks transparent pngs.
+ if (value === 1) { return; }
+ }
- value = Math.round(value * 100);
+ value = Math.round(value * 100);
- if (filter) {
- filter.Enabled = (value !== 100);
- filter.Opacity = value;
- } else {
- el.style.filter += ' progid:' + filterName + '(opacity=' + value + ')';
- }
+ if (filter) {
+ filter.Enabled = (value !== 100);
+ filter.Opacity = value;
+ } else {
+ el.style.filter += ' progid:' + filterName + '(opacity=' + value + ')';
}
},
+ // @function testProp(props: String[]): String|false
+ // Goes through the array of style names and returns the first name
+ // that is a valid style name for an element. If no such name is found,
+ // it returns false. Useful for vendor-prefixed styles like `transform`.
testProp: function (props) {
var style = document.documentElement.style;
@@ -1007,137 +1442,192 @@ L.DomUtil = {
return false;
},
- getTranslateString: function (point) {
- // on WebKit browsers (Chrome/Safari/iOS Safari/Android) using translate3d instead of translate
- // makes animation smoother as it ensures HW accel is used. Firefox 13 doesn't care
- // (same speed either way), Opera 12 doesn't support translate3d
-
- var is3d = L.Browser.webkit3d,
- open = 'translate' + (is3d ? '3d' : '') + '(',
- close = (is3d ? ',0' : '') + ')';
+ // @function setTransform(el: HTMLElement, offset: Point, scale?: Number)
+ // Resets the 3D CSS transform of `el` so it is translated by `offset` pixels
+ // and optionally scaled by `scale`. Does not have an effect if the
+ // browser doesn't support 3D CSS transforms.
+ setTransform: function (el, offset, scale) {
+ var pos = offset || new L.Point(0, 0);
- return open + point.x + 'px,' + point.y + 'px' + close;
+ el.style[L.DomUtil.TRANSFORM] =
+ (L.Browser.ie3d ?
+ 'translate(' + pos.x + 'px,' + pos.y + 'px)' :
+ 'translate3d(' + pos.x + 'px,' + pos.y + 'px,0)') +
+ (scale ? ' scale(' + scale + ')' : '');
},
- getScaleString: function (scale, origin) {
+ // @function setPosition(el: HTMLElement, position: Point)
+ // Sets the position of `el` to coordinates specified by `position`,
+ // using CSS translate or top/left positioning depending on the browser
+ // (used by Leaflet internally to position its layers).
+ setPosition: function (el, point) { // (HTMLElement, Point[, Boolean])
- var preTranslateStr = L.DomUtil.getTranslateString(origin.add(origin.multiplyBy(-1 * scale))),
- scaleStr = ' scale(' + scale + ') ';
-
- return preTranslateStr + scaleStr;
- },
-
- setPosition: function (el, point, disable3D) { // (HTMLElement, Point[, Boolean])
-
- // jshint camelcase: false
+ /*eslint-disable */
el._leaflet_pos = point;
+ /*eslint-enable */
- if (!disable3D && L.Browser.any3d) {
- el.style[L.DomUtil.TRANSFORM] = L.DomUtil.getTranslateString(point);
+ if (L.Browser.any3d) {
+ L.DomUtil.setTransform(el, point);
} else {
el.style.left = point.x + 'px';
el.style.top = point.y + 'px';
}
},
+ // @function getPosition(el: HTMLElement): Point
+ // Returns the coordinates of an element previously positioned with setPosition.
getPosition: function (el) {
// this method is only used for elements previously positioned using setPosition,
// so it's safe to cache the position for performance
- // jshint camelcase: false
- return el._leaflet_pos;
+ return el._leaflet_pos || new L.Point(0, 0);
}
};
-// prefix style property names
+(function () {
+ // prefix style property names
-L.DomUtil.TRANSFORM = L.DomUtil.testProp(
- ['transform', 'WebkitTransform', 'OTransform', 'MozTransform', 'msTransform']);
+ // @property TRANSFORM: String
+ // Vendor-prefixed fransform style name (e.g. `'webkitTransform'` for WebKit).
+ L.DomUtil.TRANSFORM = L.DomUtil.testProp(
+ ['transform', 'WebkitTransform', 'OTransform', 'MozTransform', 'msTransform']);
-// webkitTransition comes first because some browser versions that drop vendor prefix don't do
-// the same for the transitionend event, in particular the Android 4.1 stock browser
-L.DomUtil.TRANSITION = L.DomUtil.testProp(
- ['webkitTransition', 'transition', 'OTransition', 'MozTransition', 'msTransition']);
+ // webkitTransition comes first because some browser versions that drop vendor prefix don't do
+ // the same for the transitionend event, in particular the Android 4.1 stock browser
-L.DomUtil.TRANSITION_END =
- L.DomUtil.TRANSITION === 'webkitTransition' || L.DomUtil.TRANSITION === 'OTransition' ?
- L.DomUtil.TRANSITION + 'End' : 'transitionend';
+ // @property TRANSITION: String
+ // Vendor-prefixed transform style name.
+ var transition = L.DomUtil.TRANSITION = L.DomUtil.testProp(
+ ['webkitTransition', 'transition', 'OTransition', 'MozTransition', 'msTransition']);
-(function () {
- if ('onselectstart' in document) {
- L.extend(L.DomUtil, {
- disableTextSelection: function () {
- L.DomEvent.on(window, 'selectstart', L.DomEvent.preventDefault);
- },
-
- enableTextSelection: function () {
- L.DomEvent.off(window, 'selectstart', L.DomEvent.preventDefault);
- }
- });
- } else {
- var userSelectProperty = L.DomUtil.testProp(
- ['userSelect', 'WebkitUserSelect', 'OUserSelect', 'MozUserSelect', 'msUserSelect']);
-
- L.extend(L.DomUtil, {
- disableTextSelection: function () {
- if (userSelectProperty) {
- var style = document.documentElement.style;
- this._userSelect = style[userSelectProperty];
- style[userSelectProperty] = 'none';
- }
- },
-
- enableTextSelection: function () {
- if (userSelectProperty) {
- document.documentElement.style[userSelectProperty] = this._userSelect;
- delete this._userSelect;
- }
- }
- });
- }
-
- L.extend(L.DomUtil, {
- disableImageDrag: function () {
- L.DomEvent.on(window, 'dragstart', L.DomEvent.preventDefault);
- },
+ L.DomUtil.TRANSITION_END =
+ transition === 'webkitTransition' || transition === 'OTransition' ? transition + 'End' : 'transitionend';
- enableImageDrag: function () {
- L.DomEvent.off(window, 'dragstart', L.DomEvent.preventDefault);
- }
- });
+ // @function disableTextSelection()
+ // Prevents the user from generating `selectstart` DOM events, usually generated
+ // when the user drags the mouse through a page with text. Used internally
+ // by Leaflet to override the behaviour of any click-and-drag interaction on
+ // the map. Affects drag interactions on the whole document.
+
+ // @function enableTextSelection()
+ // Cancels the effects of a previous [`L.DomUtil.disableTextSelection`](#domutil-disabletextselection).
+ if ('onselectstart' in document) {
+ L.DomUtil.disableTextSelection = function () {
+ L.DomEvent.on(window, 'selectstart', L.DomEvent.preventDefault);
+ };
+ L.DomUtil.enableTextSelection = function () {
+ L.DomEvent.off(window, 'selectstart', L.DomEvent.preventDefault);
+ };
+
+ } else {
+ var userSelectProperty = L.DomUtil.testProp(
+ ['userSelect', 'WebkitUserSelect', 'OUserSelect', 'MozUserSelect', 'msUserSelect']);
+
+ L.DomUtil.disableTextSelection = function () {
+ if (userSelectProperty) {
+ var style = document.documentElement.style;
+ this._userSelect = style[userSelectProperty];
+ style[userSelectProperty] = 'none';
+ }
+ };
+ L.DomUtil.enableTextSelection = function () {
+ if (userSelectProperty) {
+ document.documentElement.style[userSelectProperty] = this._userSelect;
+ delete this._userSelect;
+ }
+ };
+ }
+
+ // @function disableImageDrag()
+ // As [`L.DomUtil.disableTextSelection`](#domutil-disabletextselection), but
+ // for `dragstart` DOM events, usually generated when the user drags an image.
+ L.DomUtil.disableImageDrag = function () {
+ L.DomEvent.on(window, 'dragstart', L.DomEvent.preventDefault);
+ };
+
+ // @function enableImageDrag()
+ // Cancels the effects of a previous [`L.DomUtil.disableImageDrag`](#domutil-disabletextselection).
+ L.DomUtil.enableImageDrag = function () {
+ L.DomEvent.off(window, 'dragstart', L.DomEvent.preventDefault);
+ };
+
+ // @function preventOutline(el: HTMLElement)
+ // Makes the [outline](https://developer.mozilla.org/docs/Web/CSS/outline)
+ // of the element `el` invisible. Used internally by Leaflet to prevent
+ // focusable elements from displaying an outline when the user performs a
+ // drag interaction on them.
+ L.DomUtil.preventOutline = function (element) {
+ while (element.tabIndex === -1) {
+ element = element.parentNode;
+ }
+ if (!element || !element.style) { return; }
+ L.DomUtil.restoreOutline();
+ this._outlineElement = element;
+ this._outlineStyle = element.style.outline;
+ element.style.outline = 'none';
+ L.DomEvent.on(window, 'keydown', L.DomUtil.restoreOutline, this);
+ };
+
+ // @function restoreOutline()
+ // Cancels the effects of a previous [`L.DomUtil.preventOutline`]().
+ L.DomUtil.restoreOutline = function () {
+ if (!this._outlineElement) { return; }
+ this._outlineElement.style.outline = this._outlineStyle;
+ delete this._outlineElement;
+ delete this._outlineStyle;
+ L.DomEvent.off(window, 'keydown', L.DomUtil.restoreOutline, this);
+ };
})();
-/*
- * L.LatLng represents a geographical point with latitude and longitude coordinates.
- */
-L.LatLng = function (lat, lng, alt) { // (Number, Number, Number)
- lat = parseFloat(lat);
- lng = parseFloat(lng);
+/* @class LatLng
+ * @aka L.LatLng
+ *
+ * Represents a geographical point with a certain latitude and longitude.
+ *
+ * @example
+ *
+ * ```
+ * var latlng = L.latLng(50.5, 30.5);
+ * ```
+ *
+ * All Leaflet methods that accept LatLng objects also accept them in a simple Array form and simple object form (unless noted otherwise), so these lines are equivalent:
+ *
+ * ```
+ * map.panTo([50, 30]);
+ * map.panTo({lon: 30, lat: 50});
+ * map.panTo({lat: 50, lng: 30});
+ * map.panTo(L.latLng(50, 30));
+ * ```
+ */
+L.LatLng = function (lat, lng, alt) {
if (isNaN(lat) || isNaN(lng)) {
throw new Error('Invalid LatLng object: (' + lat + ', ' + lng + ')');
}
- this.lat = lat;
- this.lng = lng;
+ // @property lat: Number
+ // Latitude in degrees
+ this.lat = +lat;
+
+ // @property lng: Number
+ // Longitude in degrees
+ this.lng = +lng;
+ // @property alt: Number
+ // Altitude in meters (optional)
if (alt !== undefined) {
- this.alt = parseFloat(alt);
+ this.alt = +alt;
}
};
-L.extend(L.LatLng, {
- DEG_TO_RAD: Math.PI / 180,
- RAD_TO_DEG: 180 / Math.PI,
- MAX_MARGIN: 1.0E-9 // max margin of error for the "equals" check
-});
-
L.LatLng.prototype = {
- equals: function (obj) { // (LatLng) -> Boolean
+ // @method equals(otherLatLng: LatLng, maxMargin?: Number): Boolean
+ // Returns `true` if the given `LatLng` point is at the same position (within a small margin of error). The margin of error can be overriden by setting `maxMargin` to a small number.
+ equals: function (obj, maxMargin) {
if (!obj) { return false; }
obj = L.latLng(obj);
@@ -1146,73 +1636,107 @@ L.LatLng.prototype = {
Math.abs(this.lat - obj.lat),
Math.abs(this.lng - obj.lng));
- return margin <= L.LatLng.MAX_MARGIN;
+ return margin <= (maxMargin === undefined ? 1.0E-9 : maxMargin);
},
- toString: function (precision) { // (Number) -> String
+ // @method toString(): String
+ // Returns a string representation of the point (for debugging purposes).
+ toString: function (precision) {
return 'LatLng(' +
L.Util.formatNum(this.lat, precision) + ', ' +
L.Util.formatNum(this.lng, precision) + ')';
},
- // Haversine distance formula, see http://en.wikipedia.org/wiki/Haversine_formula
- // TODO move to projection code, LatLng shouldn't know about Earth
- distanceTo: function (other) { // (LatLng) -> Number
- other = L.latLng(other);
+ // @method distanceTo(otherLatLng: LatLng): Number
+ // Returns the distance (in meters) to the given `LatLng` calculated using the [Haversine formula](http://en.wikipedia.org/wiki/Haversine_formula).
+ distanceTo: function (other) {
+ return L.CRS.Earth.distance(this, L.latLng(other));
+ },
- var R = 6378137, // earth radius in meters
- d2r = L.LatLng.DEG_TO_RAD,
- dLat = (other.lat - this.lat) * d2r,
- dLon = (other.lng - this.lng) * d2r,
- lat1 = this.lat * d2r,
- lat2 = other.lat * d2r,
- sin1 = Math.sin(dLat / 2),
- sin2 = Math.sin(dLon / 2);
+ // @method wrap(): LatLng
+ // Returns a new `LatLng` object with the longitude wrapped so it's always between -180 and +180 degrees.
+ wrap: function () {
+ return L.CRS.Earth.wrapLatLng(this);
+ },
- var a = sin1 * sin1 + sin2 * sin2 * Math.cos(lat1) * Math.cos(lat2);
+ // @method toBounds(sizeInMeters: Number): LatLngBounds
+ // Returns a new `LatLngBounds` object in which each boundary is `sizeInMeters` meters apart from the `LatLng`.
+ toBounds: function (sizeInMeters) {
+ var latAccuracy = 180 * sizeInMeters / 40075017,
+ lngAccuracy = latAccuracy / Math.cos((Math.PI / 180) * this.lat);
- return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
+ return L.latLngBounds(
+ [this.lat - latAccuracy, this.lng - lngAccuracy],
+ [this.lat + latAccuracy, this.lng + lngAccuracy]);
},
- wrap: function (a, b) { // (Number, Number) -> LatLng
- var lng = this.lng;
+ clone: function () {
+ return new L.LatLng(this.lat, this.lng, this.alt);
+ }
+};
- a = a || -180;
- b = b || 180;
- lng = (lng + b) % (b - a) + (lng < a || lng === b ? b : a);
- return new L.LatLng(this.lat, lng);
- }
-};
+// @factory L.latLng(latitude: Number, longitude: Number, altitude?: Number): LatLng
+// Creates an object representing a geographical point with the given latitude and longitude (and optionally altitude).
+
+// @alternative
+// @factory L.latLng(coords: Array): LatLng
+// Expects an array of the form `[Number, Number]` or `[Number, Number, Number]` instead.
-L.latLng = function (a, b) { // (LatLng) or ([Number, Number]) or (Number, Number)
+// @alternative
+// @factory L.latLng(coords: Object): LatLng
+// Expects an plain object of the form `{lat: Number, lng: Number}` or `{lat: Number, lng: Number, alt: Number}` instead.
+
+L.latLng = function (a, b, c) {
if (a instanceof L.LatLng) {
return a;
}
- if (L.Util.isArray(a)) {
- if (typeof a[0] === 'number' || typeof a[0] === 'string') {
+ if (L.Util.isArray(a) && typeof a[0] !== 'object') {
+ if (a.length === 3) {
return new L.LatLng(a[0], a[1], a[2]);
- } else {
- return null;
}
+ if (a.length === 2) {
+ return new L.LatLng(a[0], a[1]);
+ }
+ return null;
}
if (a === undefined || a === null) {
return a;
}
if (typeof a === 'object' && 'lat' in a) {
- return new L.LatLng(a.lat, 'lng' in a ? a.lng : a.lon);
+ return new L.LatLng(a.lat, 'lng' in a ? a.lng : a.lon, a.alt);
}
if (b === undefined) {
return null;
}
- return new L.LatLng(a, b);
+ return new L.LatLng(a, b, c);
};
/*
- * L.LatLngBounds represents a rectangular area on the map in geographical coordinates.
+ * @class LatLngBounds
+ * @aka L.LatLngBounds
+ *
+ * Represents a rectangular geographical area on a map.
+ *
+ * @example
+ *
+ * ```js
+ * var southWest = L.latLng(40.712, -74.227),
+ * northEast = L.latLng(40.774, -74.125),
+ * bounds = L.latLngBounds(southWest, northEast);
+ * ```
+ *
+ * All Leaflet methods that accept LatLngBounds objects also accept them in a simple Array form (unless noted otherwise), so the bounds example above can be passed like this:
+ *
+ * ```js
+ * map.fitBounds([
+ * [40.712, -74.227],
+ * [40.774, -74.125]
+ * ]);
+ * ```
*/
L.LatLngBounds = function (southWest, northEast) { // (LatLng, LatLng) or (LatLng[])
@@ -1226,37 +1750,48 @@ L.LatLngBounds = function (southWest, northEast) { // (LatLng, LatLng) or (LatLn
};
L.LatLngBounds.prototype = {
- // extend the bounds to contain the given point or bounds
- extend: function (obj) { // (LatLng) or (LatLngBounds)
- if (!obj) { return this; }
- var latLng = L.latLng(obj);
- if (latLng !== null) {
- obj = latLng;
- } else {
- obj = L.latLngBounds(obj);
- }
+ // @method extend(latlng: LatLng): this
+ // Extend the bounds to contain the given point
+
+ // @alternative
+ // @method extend(otherBounds: LatLngBounds): this
+ // Extend the bounds to contain the given bounds
+ extend: function (obj) {
+ var sw = this._southWest,
+ ne = this._northEast,
+ sw2, ne2;
if (obj instanceof L.LatLng) {
- if (!this._southWest && !this._northEast) {
- this._southWest = new L.LatLng(obj.lat, obj.lng);
- this._northEast = new L.LatLng(obj.lat, obj.lng);
- } else {
- this._southWest.lat = Math.min(obj.lat, this._southWest.lat);
- this._southWest.lng = Math.min(obj.lng, this._southWest.lng);
+ sw2 = obj;
+ ne2 = obj;
- this._northEast.lat = Math.max(obj.lat, this._northEast.lat);
- this._northEast.lng = Math.max(obj.lng, this._northEast.lng);
- }
} else if (obj instanceof L.LatLngBounds) {
- this.extend(obj._southWest);
- this.extend(obj._northEast);
+ sw2 = obj._southWest;
+ ne2 = obj._northEast;
+
+ if (!sw2 || !ne2) { return this; }
+
+ } else {
+ return obj ? this.extend(L.latLng(obj) || L.latLngBounds(obj)) : this;
+ }
+
+ if (!sw && !ne) {
+ this._southWest = new L.LatLng(sw2.lat, sw2.lng);
+ this._northEast = new L.LatLng(ne2.lat, ne2.lng);
+ } else {
+ sw.lat = Math.min(sw2.lat, sw.lat);
+ sw.lng = Math.min(sw2.lng, sw.lng);
+ ne.lat = Math.max(ne2.lat, ne.lat);
+ ne.lng = Math.max(ne2.lng, ne.lng);
}
+
return this;
},
- // extend the bounds by a percentage
- pad: function (bufferRatio) { // (Number) -> LatLngBounds
+ // @method pad(bufferRatio: Number): LatLngBounds
+ // Returns bigger bounds created by extending the current bounds by a given percentage in each direction.
+ pad: function (bufferRatio) {
var sw = this._southWest,
ne = this._northEast,
heightBuffer = Math.abs(sw.lat - ne.lat) * bufferRatio,
@@ -1267,44 +1802,68 @@ L.LatLngBounds.prototype = {
new L.LatLng(ne.lat + heightBuffer, ne.lng + widthBuffer));
},
- getCenter: function () { // -> LatLng
+ // @method getCenter(): LatLng
+ // Returns the center point of the bounds.
+ getCenter: function () {
return new L.LatLng(
(this._southWest.lat + this._northEast.lat) / 2,
(this._southWest.lng + this._northEast.lng) / 2);
},
+ // @method getSouthWest(): LatLng
+ // Returns the south-west point of the bounds.
getSouthWest: function () {
return this._southWest;
},
+ // @method getNorthEast(): LatLng
+ // Returns the north-east point of the bounds.
getNorthEast: function () {
return this._northEast;
},
+ // @method getNorthWest(): LatLng
+ // Returns the north-west point of the bounds.
getNorthWest: function () {
return new L.LatLng(this.getNorth(), this.getWest());
},
+ // @method getSouthEast(): LatLng
+ // Returns the south-east point of the bounds.
getSouthEast: function () {
return new L.LatLng(this.getSouth(), this.getEast());
},
+ // @method getWest(): Number
+ // Returns the west longitude of the bounds
getWest: function () {
return this._southWest.lng;
},
+ // @method getSouth(): Number
+ // Returns the south latitude of the bounds
getSouth: function () {
return this._southWest.lat;
},
+ // @method getEast(): Number
+ // Returns the east longitude of the bounds
getEast: function () {
return this._northEast.lng;
},
+ // @method getNorth(): Number
+ // Returns the north latitude of the bounds
getNorth: function () {
return this._northEast.lat;
},
+ // @method contains(otherBounds: LatLngBounds): Boolean
+ // Returns `true` if the rectangle contains the given one.
+
+ // @alternative
+ // @method contains (latlng: LatLng): Boolean
+ // Returns `true` if the rectangle contains the given point.
contains: function (obj) { // (LatLngBounds) or (LatLng) -> Boolean
if (typeof obj[0] === 'number' || obj instanceof L.LatLng) {
obj = L.latLng(obj);
@@ -1327,7 +1886,9 @@ L.LatLngBounds.prototype = {
(sw2.lng >= sw.lng) && (ne2.lng <= ne.lng);
},
- intersects: function (bounds) { // (LatLngBounds)
+ // @method intersects(otherBounds: LatLngBounds): Boolean
+ // Returns `true` if the rectangle intersects the given bounds. Two bounds intersect if they have at least one point in common.
+ intersects: function (bounds) {
bounds = L.latLngBounds(bounds);
var sw = this._southWest,
@@ -1341,11 +1902,31 @@ L.LatLngBounds.prototype = {
return latIntersects && lngIntersects;
},
+ // @method overlaps(otherBounds: Bounds): Boolean
+ // Returns `true` if the rectangle overlaps the given bounds. Two bounds overlap if their intersection is an area.
+ overlaps: function (bounds) {
+ bounds = L.latLngBounds(bounds);
+
+ var sw = this._southWest,
+ ne = this._northEast,
+ sw2 = bounds.getSouthWest(),
+ ne2 = bounds.getNorthEast(),
+
+ latOverlaps = (ne2.lat > sw.lat) && (sw2.lat < ne.lat),
+ lngOverlaps = (ne2.lng > sw.lng) && (sw2.lng < ne.lng);
+
+ return latOverlaps && lngOverlaps;
+ },
+
+ // @method toBBoxString(): String
+ // Returns a string with bounding box coordinates in a 'southwest_lng,southwest_lat,northeast_lng,northeast_lat' format. Useful for sending requests to web services that return geo data.
toBBoxString: function () {
return [this.getWest(), this.getSouth(), this.getEast(), this.getNorth()].join(',');
},
- equals: function (bounds) { // (LatLngBounds)
+ // @method equals(otherBounds: LatLngBounds): Boolean
+ // Returns `true` if the rectangle is equivalent (within a small margin of error) to the given bounds.
+ equals: function (bounds) {
if (!bounds) { return false; }
bounds = L.latLngBounds(bounds);
@@ -1354,108 +1935,218 @@ L.LatLngBounds.prototype = {
this._northEast.equals(bounds.getNorthEast());
},
+ // @method isValid(): Boolean
+ // Returns `true` if the bounds are properly initialized.
isValid: function () {
return !!(this._southWest && this._northEast);
}
};
-//TODO International date line?
+// TODO International date line?
-L.latLngBounds = function (a, b) { // (LatLngBounds) or (LatLng, LatLng)
- if (!a || a instanceof L.LatLngBounds) {
+// @factory L.latLngBounds(southWest: LatLng, northEast: LatLng)
+// Creates a `LatLngBounds` object by defining south-west and north-east corners of the rectangle.
+
+// @alternative
+// @factory L.latLngBounds(latlngs: LatLng[])
+// Creates a `LatLngBounds` object defined by the geographical points it contains. Very useful for zooming the map to fit a particular set of locations with [`fitBounds`](#map-fitbounds).
+L.latLngBounds = function (a, b) {
+ if (a instanceof L.LatLngBounds) {
return a;
}
return new L.LatLngBounds(a, b);
};
+
/*
- * L.Projection contains various geographical projections used by CRS classes.
+ * @namespace Projection
+ * @section
+ * Leaflet comes with a set of already defined Projections out of the box:
+ *
+ * @projection L.Projection.LonLat
+ *
+ * Equirectangular, or Plate Carree projection â the most simple projection,
+ * mostly used by GIS enthusiasts. Directly maps `x` as longitude, and `y` as
+ * latitude. Also suitable for flat worlds, e.g. game maps. Used by the
+ * `EPSG:3395` and `Simple` CRS.
*/
L.Projection = {};
+L.Projection.LonLat = {
+ project: function (latlng) {
+ return new L.Point(latlng.lng, latlng.lat);
+ },
+
+ unproject: function (point) {
+ return new L.LatLng(point.y, point.x);
+ },
+
+ bounds: L.bounds([-180, -90], [180, 90])
+};
+
+
/*
- * Spherical Mercator is the most popular map projection, used by EPSG:3857 CRS used by default.
+ * @namespace Projection
+ * @projection L.Projection.SphericalMercator
+ *
+ * Spherical Mercator projection â the most common projection for online maps,
+ * used by almost all free and commercial tile providers. Assumes that Earth is
+ * a sphere. Used by the `EPSG:3857` CRS.
*/
L.Projection.SphericalMercator = {
+
+ R: 6378137,
MAX_LATITUDE: 85.0511287798,
- project: function (latlng) { // (LatLng) -> Point
- var d = L.LatLng.DEG_TO_RAD,
+ project: function (latlng) {
+ var d = Math.PI / 180,
max = this.MAX_LATITUDE,
lat = Math.max(Math.min(max, latlng.lat), -max),
- x = latlng.lng * d,
- y = lat * d;
+ sin = Math.sin(lat * d);
- y = Math.log(Math.tan((Math.PI / 4) + (y / 2)));
-
- return new L.Point(x, y);
+ return new L.Point(
+ this.R * latlng.lng * d,
+ this.R * Math.log((1 + sin) / (1 - sin)) / 2);
},
- unproject: function (point) { // (Point, Boolean) -> LatLng
- var d = L.LatLng.RAD_TO_DEG,
- lng = point.x * d,
- lat = (2 * Math.atan(Math.exp(point.y)) - (Math.PI / 2)) * d;
-
- return new L.LatLng(lat, lng);
- }
-};
-
-
-/*
- * Simple equirectangular (Plate Carree) projection, used by CRS like EPSG:4326 and Simple.
- */
+ unproject: function (point) {
+ var d = 180 / Math.PI;
-L.Projection.LonLat = {
- project: function (latlng) {
- return new L.Point(latlng.lng, latlng.lat);
+ return new L.LatLng(
+ (2 * Math.atan(Math.exp(point.y / this.R)) - (Math.PI / 2)) * d,
+ point.x * d / this.R);
},
- unproject: function (point) {
- return new L.LatLng(point.y, point.x);
- }
+ bounds: (function () {
+ var d = 6378137 * Math.PI;
+ return L.bounds([-d, -d], [d, d]);
+ })()
};
+
/*
- * L.CRS is a base object for all defined CRS (Coordinate Reference Systems) in Leaflet.
+ * @class CRS
+ * @aka L.CRS
+ * Abstract class that defines coordinate reference systems for projecting
+ * geographical points into pixel (screen) coordinates and back (and to
+ * coordinates in other units for [WMS](https://en.wikipedia.org/wiki/Web_Map_Service) services). See
+ * [spatial reference system](http://en.wikipedia.org/wiki/Coordinate_reference_system).
+ *
+ * Leaflet defines the most usual CRSs by default. If you want to use a
+ * CRS not defined by default, take a look at the
+ * [Proj4Leaflet](https://github.com/kartena/Proj4Leaflet) plugin.
*/
L.CRS = {
- latLngToPoint: function (latlng, zoom) { // (LatLng, Number) -> Point
+ // @method latLngToPoint(latlng: LatLng, zoom: Number): Point
+ // Projects geographical coordinates into pixel coordinates for a given zoom.
+ latLngToPoint: function (latlng, zoom) {
var projectedPoint = this.projection.project(latlng),
scale = this.scale(zoom);
return this.transformation._transform(projectedPoint, scale);
},
- pointToLatLng: function (point, zoom) { // (Point, Number[, Boolean]) -> LatLng
+ // @method pointToLatLng(point: Point, zoom: Number): LatLng
+ // The inverse of `latLngToPoint`. Projects pixel coordinates on a given
+ // zoom into geographical coordinates.
+ pointToLatLng: function (point, zoom) {
var scale = this.scale(zoom),
untransformedPoint = this.transformation.untransform(point, scale);
return this.projection.unproject(untransformedPoint);
},
+ // @method project(latlng: LatLng): Point
+ // Projects geographical coordinates into coordinates in units accepted for
+ // this CRS (e.g. meters for EPSG:3857, for passing it to WMS services).
project: function (latlng) {
return this.projection.project(latlng);
},
+ // @method unproject(point: Point): LatLng
+ // Given a projected coordinate returns the corresponding LatLng.
+ // The inverse of `project`.
+ unproject: function (point) {
+ return this.projection.unproject(point);
+ },
+
+ // @method scale(zoom: Number): Number
+ // Returns the scale used when transforming projected coordinates into
+ // pixel coordinates for a particular zoom. For example, it returns
+ // `256 * 2^zoom` for Mercator-based CRS.
scale: function (zoom) {
return 256 * Math.pow(2, zoom);
},
- getSize: function (zoom) {
- var s = this.scale(zoom);
- return L.point(s, s);
+ // @method zoom(scale: Number): Number
+ // Inverse of `scale()`, returns the zoom level corresponding to a scale
+ // factor of `scale`.
+ zoom: function (scale) {
+ return Math.log(scale / 256) / Math.LN2;
+ },
+
+ // @method getProjectedBounds(zoom: Number): Bounds
+ // Returns the projection's bounds scaled and transformed for the provided `zoom`.
+ getProjectedBounds: function (zoom) {
+ if (this.infinite) { return null; }
+
+ var b = this.projection.bounds,
+ s = this.scale(zoom),
+ min = this.transformation.transform(b.min, s),
+ max = this.transformation.transform(b.max, s);
+
+ return L.bounds(min, max);
+ },
+
+ // @method distance(latlng1: LatLng, latlng2: LatLng): Number
+ // Returns the distance between two geographical coordinates.
+
+ // @property code: String
+ // Standard code name of the CRS passed into WMS services (e.g. `'EPSG:3857'`)
+ //
+ // @property wrapLng: Number[]
+ // An array of two numbers defining whether the longitude (horizontal) coordinate
+ // axis wraps around a given range and how. Defaults to `[-180, 180]` in most
+ // geographical CRSs. If `undefined`, the longitude axis does not wrap around.
+ //
+ // @property wrapLat: Number[]
+ // Like `wrapLng`, but for the latitude (vertical) axis.
+
+ // wrapLng: [min, max],
+ // wrapLat: [min, max],
+
+ // @property infinite: Boolean
+ // If true, the coordinate space will be unbounded (infinite in both axes)
+ infinite: false,
+
+ // @method wrapLatLng(latlng: LatLng): LatLng
+ // Returns a `LatLng` where lat and lng has been wrapped according to the
+ // CRS's `wrapLat` and `wrapLng` properties, if they are outside the CRS's bounds.
+ wrapLatLng: function (latlng) {
+ var lng = this.wrapLng ? L.Util.wrapNum(latlng.lng, this.wrapLng, true) : latlng.lng,
+ lat = this.wrapLat ? L.Util.wrapNum(latlng.lat, this.wrapLat, true) : latlng.lat,
+ alt = latlng.alt;
+
+ return L.latLng(lat, lng, alt);
}
};
+
/*
- * A simple CRS that can be used for flat non-Earth maps like panoramas or game maps.
+ * @namespace CRS
+ * @crs L.CRS.Simple
+ *
+ * A simple CRS that maps longitude and latitude into `x` and `y` directly.
+ * May be used for maps of flat surfaces (e.g. game maps). Note that the `y`
+ * axis should still be inverted (going from bottom to top). `distance()` returns
+ * simple euclidean distance.
*/
L.CRS.Simple = L.extend({}, L.CRS, {
@@ -1464,26 +2155,73 @@ L.CRS.Simple = L.extend({}, L.CRS, {
scale: function (zoom) {
return Math.pow(2, zoom);
+ },
+
+ zoom: function (scale) {
+ return Math.log(scale) / Math.LN2;
+ },
+
+ distance: function (latlng1, latlng2) {
+ var dx = latlng2.lng - latlng1.lng,
+ dy = latlng2.lat - latlng1.lat;
+
+ return Math.sqrt(dx * dx + dy * dy);
+ },
+
+ infinite: true
+});
+
+
+
+/*
+ * @namespace CRS
+ * @crs L.CRS.Earth
+ *
+ * Serves as the base for CRS that are global such that they cover the earth.
+ * Can only be used as the base for other CRS and cannot be used directly,
+ * since it does not have a `code`, `projection` or `transformation`. `distance()` returns
+ * meters.
+ */
+
+L.CRS.Earth = L.extend({}, L.CRS, {
+ wrapLng: [-180, 180],
+
+ // Mean Earth Radius, as recommended for use by
+ // the International Union of Geodesy and Geophysics,
+ // see http://rosettacode.org/wiki/Haversine_formula
+ R: 6371000,
+
+ // distance between two geographical points using spherical law of cosines approximation
+ distance: function (latlng1, latlng2) {
+ var rad = Math.PI / 180,
+ lat1 = latlng1.lat * rad,
+ lat2 = latlng2.lat * rad,
+ a = Math.sin(lat1) * Math.sin(lat2) +
+ Math.cos(lat1) * Math.cos(lat2) * Math.cos((latlng2.lng - latlng1.lng) * rad);
+
+ return this.R * Math.acos(Math.min(a, 1));
}
});
+
/*
- * L.CRS.EPSG3857 (Spherical Mercator) is the most common CRS for web mapping
- * and is used by Leaflet by default.
+ * @namespace CRS
+ * @crs L.CRS.EPSG3857
+ *
+ * The most common CRS for online maps, used by almost all free and commercial
+ * tile providers. Uses Spherical Mercator projection. Set in by default in
+ * Map's `crs` option.
*/
-L.CRS.EPSG3857 = L.extend({}, L.CRS, {
+L.CRS.EPSG3857 = L.extend({}, L.CRS.Earth, {
code: 'EPSG:3857',
-
projection: L.Projection.SphericalMercator,
- transformation: new L.Transformation(0.5 / Math.PI, 0.5, -0.5 / Math.PI, 0.5),
- project: function (latlng) { // (LatLng) -> Point
- var projectedPoint = this.projection.project(latlng),
- earthRadius = 6378137;
- return projectedPoint.multiplyBy(earthRadius);
- }
+ transformation: (function () {
+ var scale = 0.5 / (Math.PI * L.Projection.SphericalMercator.R);
+ return new L.Transformation(scale, 0.5, -scale, 0.5);
+ }())
});
L.CRS.EPSG900913 = L.extend({}, L.CRS.EPSG3857, {
@@ -1491,44 +2229,125 @@ L.CRS.EPSG900913 = L.extend({}, L.CRS.EPSG3857, {
});
+
/*
- * L.CRS.EPSG4326 is a CRS popular among advanced GIS specialists.
+ * @namespace CRS
+ * @crs L.CRS.EPSG4326
+ *
+ * A common CRS among GIS enthusiasts. Uses simple Equirectangular projection.
*/
-L.CRS.EPSG4326 = L.extend({}, L.CRS, {
+L.CRS.EPSG4326 = L.extend({}, L.CRS.Earth, {
code: 'EPSG:4326',
-
projection: L.Projection.LonLat,
- transformation: new L.Transformation(1 / 360, 0.5, -1 / 360, 0.5)
+ transformation: new L.Transformation(1 / 180, 1, -1 / 180, 0.5)
});
+
/*
- * L.Map is the central class of the API - it is used to create a map.
+ * @class Map
+ * @aka L.Map
+ * @inherits Evented
+ *
+ * The central class of the API â it is used to create a map on a page and manipulate it.
+ *
+ * @example
+ *
+ * ```js
+ * // initialize the map on the "map" div with a given center and zoom
+ * var map = L.map('map', {
+ * center: [51.505, -0.09],
+ * zoom: 13
+ * });
+ * ```
+ *
*/
-L.Map = L.Class.extend({
-
- includes: L.Mixin.Events,
+L.Map = L.Evented.extend({
options: {
+ // @section Map State Options
+ // @option crs: CRS = L.CRS.EPSG3857
+ // The [Coordinate Reference System](#crs) to use. Don't change this if you're not
+ // sure what it means.
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
+ // @option center: LatLng = undefined
+ // Initial geographic center of the map
+ center: undefined,
+
+ // @option zoom: Number = undefined
+ // Initial map zoom level
+ zoom: undefined,
+
+ // @option minZoom: Number = undefined
+ // Minimum zoom level of the map. Overrides any `minZoom` option set on map layers.
+ minZoom: undefined,
+
+ // @option maxZoom: Number = undefined
+ // Maximum zoom level of the map. Overrides any `maxZoom` option set on map layers.
+ maxZoom: undefined,
+
+ // @option layers: Layer[] = []
+ // Array of layers that will be added to the map initially
+ layers: [],
+
+ // @option maxBounds: LatLngBounds = null
+ // When this option is set, the map restricts the view to the given
+ // geographical bounds, bouncing the user back when he tries to pan
+ // outside the view. To set the restriction dynamically, use
+ // [`setMaxBounds`](#map-setmaxbounds) method.
+ maxBounds: undefined,
+
+ // @option renderer: Renderer = *
+ // The default method for drawing vector layers on the map. `L.SVG`
+ // or `L.Canvas` by default depending on browser support.
+ renderer: undefined,
+
+
+ // @section Animation Options
+ // @option fadeAnimation: Boolean = true
+ // Whether the tile fade animation is enabled. By default it's enabled
+ // in all browsers that support CSS3 Transitions except Android.
+ fadeAnimation: true,
+
+ // @option markerZoomAnimation: Boolean = true
+ // Whether markers animate their zoom with the zoom animation, if disabled
+ // they will disappear for the length of the animation. By default it's
+ // enabled in all browsers that support CSS3 Transitions except Android.
+ markerZoomAnimation: true,
+
+ // @option transform3DLimit: Number = 2^23
+ // Defines the maximum size of a CSS translation transform. The default
+ // value should not be changed unless a web browser positions layers in
+ // the wrong place after doing a large `panBy`.
+ transform3DLimit: 8388608, // Precision limit of a 32-bit float
+
+ // @section Interaction Options
+ // @option zoomSnap: Number = 1
+ // Forces the map's zoom level to always be a multiple of this, particularly
+ // right after a [`fitBounds()`](#map-fitbounds) or a pinch-zoom.
+ // By default, the zoom level snaps to the nearest integer; lower values
+ // (e.g. `0.5` or `0.1`) allow for greater granularity. A value of `0`
+ // means the zoom level will not be snapped after `fitBounds` or a pinch-zoom.
+ zoomSnap: 1,
+
+ // @option zoomDelta: Number = 1
+ // Controls how much the map's zoom level will change after a
+ // [`zoomIn()`](#map-zoomin), [`zoomOut()`](#map-zoomout), pressing `+`
+ // or `-` on the keyboard, or using the [zoom controls](#control-zoom).
+ // Values smaller than `1` (e.g. `0.5`) allow for greater granularity.
+ zoomDelta: 1,
+
+ // @option trackResize: Boolean = true
+ // Whether the map automatically handles browser window resize to update itself.
+ trackResize: true
},
initialize: function (id, options) { // (HTMLElement or String, Object)
options = L.setOptions(this, options);
-
this._initContainer(id);
this._initLayout();
@@ -1541,47 +2360,67 @@ L.Map = L.Class.extend({
this.setMaxBounds(options.maxBounds);
}
+ if (options.zoom !== undefined) {
+ this._zoom = this._limitZoom(options.zoom);
+ }
+
if (options.center && options.zoom !== undefined) {
this.setView(L.latLng(options.center), options.zoom, {reset: true});
}
this._handlers = [];
-
this._layers = {};
this._zoomBoundLayers = {};
- this._tileLayersNum = 0;
+ this._sizeChanged = true;
this.callInitHooks();
- this._addLayers(options.layers);
+ this._addLayers(this.options.layers);
},
- // public methods that modify map state
+ // @section Methods for modifying map state
- // replaced by animation-powered implementation in Map.PanAnimation.js
+ // @method setView(center: LatLng, zoom: Number, options?: Zoom/pan options): this
+ // Sets the view of the map (geographical center and zoom) with the given
+ // animation options.
setView: function (center, zoom) {
+ // replaced by animation-powered implementation in Map.PanAnimation.js
zoom = zoom === undefined ? this.getZoom() : zoom;
- this._resetView(L.latLng(center), this._limitZoom(zoom));
+ this._resetView(L.latLng(center), zoom);
return this;
},
+ // @method setZoom(zoom: Number, options: Zoom/pan options): this
+ // Sets the zoom of the map.
setZoom: function (zoom, options) {
if (!this._loaded) {
- this._zoom = this._limitZoom(zoom);
+ this._zoom = zoom;
return this;
}
return this.setView(this.getCenter(), zoom, {zoom: options});
},
+ // @method zoomIn(delta?: Number, options?: Zoom options): this
+ // Increases the zoom of the map by `delta` ([`zoomDelta`](#map-zoomdelta) by default).
zoomIn: function (delta, options) {
- return this.setZoom(this._zoom + (delta || 1), options);
+ delta = delta || (L.Browser.any3d ? this.options.zoomDelta : 1);
+ return this.setZoom(this._zoom + delta, options);
},
+ // @method zoomOut(delta?: Number, options?: Zoom options): this
+ // Decreases the zoom of the map by `delta` ([`zoomDelta`](#map-zoomdelta) by default).
zoomOut: function (delta, options) {
- return this.setZoom(this._zoom - (delta || 1), options);
+ delta = delta || (L.Browser.any3d ? this.options.zoomDelta : 1);
+ return this.setZoom(this._zoom - delta, options);
},
+ // @method setZoomAround(latlng: LatLng, zoom: Number, options: Zoom options): this
+ // Zooms the map while keeping a specified geographical point on the map
+ // stationary (e.g. used internally for scroll zoom and double-click zoom).
+ // @alternative
+ // @method setZoomAround(offset: Point, zoom: Number, options: Zoom options): this
+ // Zooms the map while keeping a specified pixel on the map (relative to the top-left corner) stationary.
setZoomAround: function (latlng, zoom, options) {
var scale = this.getZoomScale(zoom),
viewHalf = this.getSize().divideBy(2),
@@ -1593,7 +2432,7 @@ L.Map = L.Class.extend({
return this.setView(newCenter, zoom, {zoom: options});
},
- fitBounds: function (bounds, options) {
+ _getBoundsCenterZoom: function (bounds, options) {
options = options || {};
bounds = bounds.getBounds ? bounds.getBounds() : L.latLngBounds(bounds);
@@ -1603,7 +2442,7 @@ L.Map = L.Class.extend({
zoom = this.getBoundsZoom(bounds, false, paddingTL.add(paddingBR));
- zoom = (options.maxZoom) ? Math.min(options.maxZoom, zoom) : zoom;
+ zoom = (typeof options.maxZoom === 'number') ? Math.min(options.maxZoom, zoom) : zoom;
var paddingOffset = paddingBR.subtract(paddingTL).divideBy(2),
@@ -1611,17 +2450,42 @@ L.Map = L.Class.extend({
nePoint = this.project(bounds.getNorthEast(), zoom),
center = this.unproject(swPoint.add(nePoint).divideBy(2).add(paddingOffset), zoom);
- return this.setView(center, zoom, options);
+ return {
+ center: center,
+ zoom: zoom
+ };
+ },
+
+ // @method fitBounds(bounds: LatLngBounds, options: fitBounds options): this
+ // Sets a map view that contains the given geographical bounds with the
+ // maximum zoom level possible.
+ fitBounds: function (bounds, options) {
+
+ bounds = L.latLngBounds(bounds);
+
+ if (!bounds.isValid()) {
+ throw new Error('Bounds are not valid.');
+ }
+
+ var target = this._getBoundsCenterZoom(bounds, options);
+ return this.setView(target.center, target.zoom, options);
},
+ // @method fitWorld(options?: fitBounds options): this
+ // Sets a map view that mostly contains the whole world with the maximum
+ // zoom level possible.
fitWorld: function (options) {
return this.fitBounds([[-90, -180], [90, 180]], options);
},
+ // @method panTo(latlng: LatLng, options?: Pan options): this
+ // Pans the map to a given center.
panTo: function (center, options) { // (LatLng)
return this.setView(center, this._zoom, {pan: options});
},
+ // @method panBy(offset: Point): this
+ // Pans the map by a given number of pixels (animated).
panBy: function (offset) { // (Point)
// replaced with animated panBy in Map.PanAnimation.js
this.fire('movestart');
@@ -1632,103 +2496,79 @@ L.Map = L.Class.extend({
return this.fire('moveend');
},
+ // @method setMaxBounds(bounds: Bounds): this
+ // Restricts the map view to the given bounds (see the [maxBounds](#map-maxbounds) option).
setMaxBounds: function (bounds) {
bounds = L.latLngBounds(bounds);
- this.options.maxBounds = bounds;
-
- if (!bounds) {
- return this.off('moveend', this._panInsideMaxBounds, this);
+ 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, this);
- },
-
- panInsideBounds: function (bounds, options) {
- var center = this.getCenter(),
- newCenter = this._limitCenter(center, this._zoom, bounds);
-
- if (center.equals(newCenter)) { return this; }
-
- return this.panTo(newCenter, options);
+ return this.on('moveend', this._panInsideMaxBounds);
},
- addLayer: function (layer) {
- // TODO method is too big, refactor
-
- var id = L.stamp(layer);
-
- if (this._layers[id]) { return this; }
-
- this._layers[id] = layer;
-
- // TODO getMaxZoom, getMinZoom in ILayer (instead of options)
- if (layer.options && (!isNaN(layer.options.maxZoom) || !isNaN(layer.options.minZoom))) {
- this._zoomBoundLayers[id] = layer;
- this._updateZoomLevels();
- }
-
- // TODO looks ugly, refactor!!!
- if (this.options.zoomAnimation && L.TileLayer && (layer instanceof L.TileLayer)) {
- this._tileLayersNum++;
- this._tileLayersToLoad++;
- layer.on('load', this._onTileLayerLoad, this);
- }
+ // @method setMinZoom(zoom: Number): this
+ // Sets the lower limit for the available zoom levels (see the [minZoom](#map-minzoom) option).
+ setMinZoom: function (zoom) {
+ this.options.minZoom = zoom;
- if (this._loaded) {
- this._layerAdd(layer);
+ if (this._loaded && this.getZoom() < this.options.minZoom) {
+ return this.setZoom(zoom);
}
return this;
},
- removeLayer: function (layer) {
- var id = L.stamp(layer);
-
- if (!this._layers[id]) { return this; }
-
- if (this._loaded) {
- layer.onRemove(this);
- }
-
- delete this._layers[id];
-
- if (this._loaded) {
- this.fire('layerremove', {layer: layer});
- }
-
- if (this._zoomBoundLayers[id]) {
- delete this._zoomBoundLayers[id];
- this._updateZoomLevels();
- }
+ // @method setMaxZoom(zoom: Number): this
+ // Sets the upper limit for the available zoom levels (see the [maxZoom](#map-maxzoom) option).
+ setMaxZoom: function (zoom) {
+ this.options.maxZoom = zoom;
- // TODO looks ugly, refactor
- if (this.options.zoomAnimation && L.TileLayer && (layer instanceof L.TileLayer)) {
- this._tileLayersNum--;
- this._tileLayersToLoad--;
- layer.off('load', this._onTileLayerLoad, this);
+ if (this._loaded && (this.getZoom() > this.options.maxZoom)) {
+ return this.setZoom(zoom);
}
return this;
},
- hasLayer: function (layer) {
- if (!layer) { return false; }
-
- return (L.stamp(layer) in this._layers);
- },
+ // @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, L.latLngBounds(bounds));
- eachLayer: function (method, context) {
- for (var i in this._layers) {
- method.call(context, this._layers[i]);
+ if (!center.equals(newCenter)) {
+ this.panTo(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; }
@@ -1739,7 +2579,7 @@ L.Map = L.Class.extend({
var oldSize = this.getSize();
this._sizeChanged = true;
- this._initialCenter = null;
+ this._lastCenter = null;
var newSize = this.getSize(),
oldCenter = oldSize.divideBy(2).round(),
@@ -1766,13 +2606,32 @@ L.Map = L.Class.extend({
}
}
+ // @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();
+ },
+
+
// TODO handler.addTo
+ // TODO Appropiate 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; }
@@ -1787,46 +2646,85 @@ L.Map = L.Class.extend({
return this;
},
+ // @method remove(): this
+ // Destroys the map and clears all related event listeners.
remove: function () {
- if (this._loaded) {
- this.fire('unload');
- }
- this._initEvents('off');
+ 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;
+ delete this._container._leaflet_id;
+ delete this._containerId;
} catch (e) {
- this._container._leaflet = undefined;
+ /*eslint-disable */
+ this._container._leaflet_id = undefined;
+ /*eslint-enable */
+ this._containerId = undefined;
}
- this._clearPanes();
+ L.DomUtil.remove(this._mapPane);
+
if (this._clearControlPos) {
this._clearControlPos();
}
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');
+ }
+
+ for (var i in this._layers) {
+ this._layers[i].remove();
+ }
+
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 children of `container`, or
+ // as a children of the main map pane if not set.
+ createPane: function (name, container) {
+ var className = 'leaflet-pane' + (name ? ' leaflet-' + name.replace('Pane', '') + '-pane' : ''),
+ pane = L.DomUtil.create('div', className, container || this._mapPane);
- // public methods for getting map state
+ if (name) {
+ this._panes[name] = pane;
+ }
+ return pane;
+ },
- getCenter: function () { // (Boolean) -> LatLng
+ // @section Methods for Getting Map State
+
+ // @method getCenter(): LatLng
+ // Returns the geographical center of the map view
+ getCenter: function () {
this._checkIfLoaded();
- if (this._initialCenter && !this._moved()) {
- return this._initialCenter;
+ 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()),
@@ -1835,47 +2733,51 @@ L.Map = L.Class.extend({
return new L.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 === undefined ? 0 : this._layersMinZoom) :
- this.options.minZoom;
+ 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): 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 = L.latLngBounds(bounds);
+ padding = L.point(padding || [0, 0]);
- var zoom = this.getMinZoom() - (inside ? 1 : 0),
- maxZoom = this.getMaxZoom(),
- size = this.getSize(),
-
+ var zoom = this.getZoom() || 0,
+ min = this.getMinZoom(),
+ max = this.getMaxZoom(),
nw = bounds.getNorthWest(),
se = bounds.getSouthEast(),
+ size = this.getSize().subtract(padding),
+ boundsSize = this.project(se, zoom).subtract(this.project(nw, zoom)),
+ snap = L.Browser.any3d ? this.options.zoomSnap : 1;
- zoomNotFound = true,
- boundsSize;
-
- padding = L.point(padding || [0, 0]);
-
- do {
- zoom++;
- boundsSize = this.project(se, zoom).subtract(this.project(nw, zoom)).add(padding);
- zoomNotFound = !inside ? size.contains(boundsSize) : boundsSize.x < size.x || boundsSize.y < size.y;
-
- } while (zoomNotFound && zoom <= maxZoom);
+ var scale = Math.min(size.x / boundsSize.x, size.y / boundsSize.y);
+ zoom = this.getScaleZoom(scale, zoom);
- if (zoomNotFound && inside) {
- return null;
+ 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 inside ? zoom : zoom - 1;
+ 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 L.Point(
@@ -1887,84 +2789,173 @@ L.Map = L.Class.extend({
return this._size.clone();
},
- getPixelBounds: function () {
- var topLeftPoint = this._getTopLeftPoint();
+ // @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 L.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._initialTopLeftPoint;
+ return this._pixelOrigin;
},
- getPanes: function () {
- return this._panes;
+ // @method getPixelWorldBounds(zoom?: Number): Bounds
+ // Returns the world's bounds in pixel coordinates for zoom level `zoom`.
+ // If `zoom` is omitted, the map's current zoom level is used.
+ getPixelWorldBounds: function (zoom) {
+ return this.options.crs.getProjectedBounds(zoom === undefined ? this.getZoom() : zoom);
},
- getContainer: function () {
- return this._container;
+ // @section Other Methods
+
+ // @method getPane(pane: String|HTMLElement): HTMLElement
+ // Returns a [map pane](#map-pane), given its name or its HTML element (its identity).
+ getPane: function (pane) {
+ return typeof pane === 'string' ? this._panes[pane] : pane;
+ },
+
+ // @method getPanes(): Object
+ // Returns a plain object containing the names of all [panes](#map-pane) as keys and
+ // the panes as values.
+ getPanes: function () {
+ return this._panes;
+ },
+
+ // @method getContainer: HTMLElement
+ // Returns the HTML element that contains the map.
+ getContainer: function () {
+ return this._container;
},
- // TODO replace with universal implementation after refactoring projections
+ // @section Conversion Methods
- getZoomScale: function (toZoom) {
+ // @method getZoomScale(toZoom: Number, fromZoom: Number): Number
+ // Returns the scale factor to be applied to a map transition from zoom level
+ // `fromZoom` to `toZoom`. Used internally to help with zoom animations.
+ getZoomScale: function (toZoom, fromZoom) {
+ // TODO replace with universal implementation after refactoring projections
var crs = this.options.crs;
- return crs.scale(toZoom) / crs.scale(this._zoom);
+ fromZoom = fromZoom === undefined ? this._zoom : fromZoom;
+ return crs.scale(toZoom) / crs.scale(fromZoom);
},
- getScaleZoom: function (scale) {
- return this._zoom + (Math.log(scale) / Math.LN2);
+ // @method getScaleZoom(scale: Number, fromZoom: Number): Number
+ // Returns the zoom level that the map would end up at, if it is at `fromZoom`
+ // level and everything is scaled by a factor of `scale`. Inverse of
+ // [`getZoomScale`](#map-getZoomScale).
+ getScaleZoom: function (scale, fromZoom) {
+ var crs = this.options.crs;
+ fromZoom = fromZoom === undefined ? this._zoom : fromZoom;
+ var zoom = crs.zoom(scale * crs.scale(fromZoom));
+ return isNaN(zoom) ? Infinity : zoom;
},
-
- // conversion methods
-
- project: function (latlng, zoom) { // (LatLng[, Number]) -> Point
+ // @method project(latlng: LatLng, zoom: Number): Point
+ // Projects a geographical coordinate `LatLng` according to the projection
+ // of the map's CRS, then scales it according to `zoom` and the CRS's
+ // `Transformation`. The result is pixel coordinate relative to
+ // the CRS origin.
+ project: function (latlng, zoom) {
zoom = zoom === undefined ? this._zoom : zoom;
return this.options.crs.latLngToPoint(L.latLng(latlng), zoom);
},
- unproject: function (point, zoom) { // (Point[, Number]) -> LatLng
+ // @method unproject(point: Point, zoom: Number): LatLng
+ // Inverse of [`project`](#map-project).
+ unproject: function (point, zoom) {
zoom = zoom === undefined ? this._zoom : zoom;
return this.options.crs.pointToLatLng(L.point(point), zoom);
},
- layerPointToLatLng: function (point) { // (Point)
+ // @method layerPointToLatLng(point: Point): LatLng
+ // Given a pixel coordinate relative to the [origin pixel](#map-getpixelorigin),
+ // returns the corresponding geographical coordinate (for the current zoom level).
+ layerPointToLatLng: function (point) {
var projectedPoint = L.point(point).add(this.getPixelOrigin());
return this.unproject(projectedPoint);
},
- latLngToLayerPoint: function (latlng) { // (LatLng)
+ // @method latLngToLayerPoint(latlng: LatLng): Point
+ // Given a geographical coordinate, returns the corresponding pixel coordinate
+ // relative to the [origin pixel](#map-getpixelorigin).
+ latLngToLayerPoint: function (latlng) {
var projectedPoint = this.project(L.latLng(latlng))._round();
return projectedPoint._subtract(this.getPixelOrigin());
},
+ // @method wrapLatLng(latlng: LatLng): LatLng
+ // Returns a `LatLng` where `lat` and `lng` has been wrapped according to the
+ // map's CRS's `wrapLat` and `wrapLng` properties, if they are outside the
+ // CRS's bounds.
+ // By default this means longitude is wrapped around the dateline so its
+ // value is between -180 and +180 degrees.
+ wrapLatLng: function (latlng) {
+ return this.options.crs.wrapLatLng(L.latLng(latlng));
+ },
+
+ // @method distance(latlng1: LatLng, latlng2: LatLng): Number
+ // Returns the distance between two geographical coordinates according to
+ // the map's CRS. By default this measures distance in meters.
+ distance: function (latlng1, latlng2) {
+ return this.options.crs.distance(L.latLng(latlng1), L.latLng(latlng2));
+ },
+
+ // @method containerPointToLayerPoint(point: Point): Point
+ // Given a pixel coordinate relative to the map container, returns the corresponding
+ // pixel coordinate relative to the [origin pixel](#map-getpixelorigin).
containerPointToLayerPoint: function (point) { // (Point)
return L.point(point).subtract(this._getMapPanePos());
},
+ // @method layerPointToContainerPoint(point: Point): Point
+ // Given a pixel coordinate relative to the [origin pixel](#map-getpixelorigin),
+ // returns the corresponding pixel coordinate relative to the map container.
layerPointToContainerPoint: function (point) { // (Point)
return L.point(point).add(this._getMapPanePos());
},
+ // @method containerPointToLatLng(point: Point): Point
+ // Given a pixel coordinate relative to the map container, returns
+ // the corresponding geographical coordinate (for the current zoom level).
containerPointToLatLng: function (point) {
var layerPoint = this.containerPointToLayerPoint(L.point(point));
return this.layerPointToLatLng(layerPoint);
},
+ // @method latLngToContainerPoint(latlng: LatLng): Point
+ // Given a geographical coordinate, returns the corresponding pixel coordinate
+ // relative to the map container.
latLngToContainerPoint: function (latlng) {
return this.layerPointToContainerPoint(this.latLngToLayerPoint(L.latLng(latlng)));
},
- mouseEventToContainerPoint: function (e) { // (MouseEvent)
+ // @method mouseEventToContainerPoint(ev: MouseEvent): Point
+ // Given a MouseEvent object, returns the pixel coordinate relative to the
+ // map container where the event took place.
+ mouseEventToContainerPoint: function (e) {
return L.DomEvent.getMousePosition(e, this._container);
},
- mouseEventToLayerPoint: function (e) { // (MouseEvent)
+ // @method mouseEventToLayerPoint(ev: MouseEvent): Point
+ // Given a MouseEvent object, returns the pixel coordinate relative to
+ // the [origin pixel](#map-getpixelorigin) where the event took place.
+ mouseEventToLayerPoint: function (e) {
return this.containerPointToLayerPoint(this.mouseEventToContainerPoint(e));
},
+ // @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));
},
@@ -1977,21 +2968,25 @@ L.Map = L.Class.extend({
if (!container) {
throw new Error('Map container not found.');
- } else if (container._leaflet) {
+ } else if (container._leaflet_id) {
throw new Error('Map container is already initialized.');
}
- container._leaflet = true;
+ L.DomEvent.addListener(container, 'scroll', this._onScroll, this);
+ this._containerId = L.Util.stamp(container);
},
_initLayout: function () {
var container = this._container;
+ this._fadeAnimated = this.options.fadeAnimation && L.Browser.any3d;
+
L.DomUtil.addClass(container, 'leaflet-container' +
(L.Browser.touch ? ' leaflet-touch' : '') +
(L.Browser.retina ? ' leaflet-retina' : '') +
(L.Browser.ielt9 ? ' leaflet-oldie' : '') +
- (this.options.fadeAnimation ? ' leaflet-fade-anim' : ''));
+ (L.Browser.safari ? ' leaflet-safari' : '') +
+ (this._fadeAnimated ? ' leaflet-fade-anim' : ''));
var position = L.DomUtil.getStyle(container, 'position');
@@ -2008,86 +3003,133 @@ L.Map = L.Class.extend({
_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);
- panes.objectsPane = this._createPane('leaflet-objects-pane', this._mapPane);
- 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';
+ 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);
+ L.DomUtil.setPosition(this._mapPane, new L.Point(0, 0));
+
+ // @pane tilePane: HTMLElement = 200
+ // Pane for `GridLayer`s and `TileLayer`s
+ this.createPane('tilePane');
+ // @pane overlayPane: HTMLElement = 400
+ // Pane for vector overlays (`Path`s), like `Polyline`s and `Polygon`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.
+ this.createPane('tooltipPane');
+ // @pane popupPane: HTMLElement = 700
+ // Pane for `Popup`s.
+ this.createPane('popupPane');
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) {
- return L.DomUtil.create('div', className, container || this._panes.objectsPane);
- },
-
- _clearPanes: function () {
- this._container.removeChild(this._mapPane);
- },
-
- _addLayers: function (layers) {
- layers = layers ? (L.Util.isArray(layers) ? layers : [layers]) : [];
-
- for (var i = 0, len = layers.length; i < len; i++) {
- this.addLayer(layers[i]);
+ L.DomUtil.addClass(panes.markerPane, 'leaflet-zoom-hide');
+ L.DomUtil.addClass(panes.shadowPane, 'leaflet-zoom-hide');
}
},
// private methods that modify map state
- _resetView: function (center, zoom, preserveMapOffset, afterZoomAnim) {
+ // @section Map state change events
+ _resetView: function (center, zoom) {
+ L.DomUtil.setPosition(this._mapPane, new L.Point(0, 0));
- var zoomChanged = (this._zoom !== zoom);
+ var loading = !this._loaded;
+ this._loaded = true;
+ zoom = this._limitZoom(zoom);
- if (!afterZoomAnim) {
- this.fire('movestart');
+ this.fire('viewprereset');
- if (zoomChanged) {
- this.fire('zoomstart');
- }
+ var zoomChanged = this._zoom !== zoom;
+ this
+ ._moveStart(zoomChanged)
+ ._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');
}
+ },
- this._zoom = zoom;
- this._initialCenter = center;
-
- this._initialTopLeftPoint = this._getNewTopLeftPoint(center);
-
- if (!preserveMapOffset) {
- L.DomUtil.setPosition(this._mapPane, new L.Point(0, 0));
- } else {
- this._initialTopLeftPoint._add(this._getMapPanePos());
+ _moveStart: function (zoomChanged) {
+ // @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');
}
+ return this.fire('movestart');
+ },
- this._tileLayersToLoad = this._tileLayersNum;
-
- var loading = !this._loaded;
- this._loaded = true;
+ _move: function (center, zoom, data) {
+ if (zoom === undefined) {
+ zoom = this._zoom;
+ }
+ var zoomChanged = this._zoom !== zoom;
- this.fire('viewreset', {hard: !preserveMapOffset});
+ this._zoom = zoom;
+ this._lastCenter = center;
+ this._pixelOrigin = this._getNewPixelOrigin(center);
- if (loading) {
- this.fire('load');
- this.eachLayer(this._layerAdd, this);
+ // @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);
}
- this.fire('move');
+ // @event move: Event
+ // Fired repeatedly during any movement of the map, including pan and
+ // fly animations.
+ return this.fire('move', data);
+ },
- if (zoomChanged || afterZoomAnim) {
+ _moveEnd: function (zoomChanged) {
+ // @event zoomend: Event
+ // Fired when the map has changed, after any animations.
+ if (zoomChanged) {
this.fire('zoomend');
}
- this.fire('moveend', {hard: !preserveMapOffset});
+ // @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 () {
+ L.Util.cancelAnimFrame(this._flyToFrame);
+ if (this._panAnim) {
+ this._panAnim.stop();
+ }
+ return this;
},
_rawPanBy: function (offset) {
@@ -2098,36 +3140,10 @@ L.Map = L.Class.extend({
return this.getMaxZoom() - this.getMinZoom();
},
- _updateZoomLevels: function () {
- var i,
- minZoom = Infinity,
- maxZoom = -Infinity,
- oldZoomSpan = this._getZoomSpan();
-
- for (i in this._zoomBoundLayers) {
- 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);
- }
- }
-
- if (i === undefined) { // we have no tilelayers
- this._layersMaxZoom = this._layersMinZoom = undefined;
- } else {
- this._layersMaxZoom = maxZoom;
- this._layersMinZoom = minZoom;
- }
-
- if (oldZoomSpan !== this._getZoomSpan()) {
- this.fire('zoomlevelschange');
- }
- },
-
_panInsideMaxBounds: function () {
- this.panInsideBounds(this.options.maxBounds);
+ if (!this._enforcingBounds) {
+ this.panInsideBounds(this.options.maxBounds);
+ }
},
_checkIfLoaded: function () {
@@ -2136,101 +3152,186 @@ L.Map = L.Class.extend({
}
},
- // map events
+ // DOM event handling
- _initEvents: function (onOff) {
+ // @section Interaction events
+ _initEvents: function (remove) {
if (!L.DomEvent) { return; }
- onOff = onOff || 'on';
-
- L.DomEvent[onOff](this._container, 'click', this._onMouseClick, this);
-
- var events = ['dblclick', 'mousedown', 'mouseup', 'mouseenter',
- 'mouseleave', 'mousemove', 'contextmenu'],
- i, len;
-
- for (i = 0, len = events.length; i < len; i++) {
- L.DomEvent[onOff](this._container, events[i], this._fireMouseEvent, this);
- }
+ this._targets = {};
+ this._targets[L.stamp(this._container)] = this;
+
+ var onOff = remove ? '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 while the map is focused.
+ L.DomEvent[onOff](this._container, 'click dblclick mousedown mouseup ' +
+ 'mouseover mouseout mousemove contextmenu keypress', this._handleDOMEvent, this);
if (this.options.trackResize) {
L.DomEvent[onOff](window, 'resize', this._onResize, this);
}
+
+ if (L.Browser.any3d && this.options.transform3DLimit) {
+ this[onOff]('moveend', this._onMoveEnd);
+ }
},
_onResize: function () {
L.Util.cancelAnimFrame(this._resizeRequest);
this._resizeRequest = L.Util.requestAnimFrame(
- function () { this.invalidateSize({debounceMoveend: true}); }, this, false, this._container);
+ 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());
+ }
},
- _onMouseClick: function (e) {
- if (!this._loaded || (!e._simulated &&
- ((this.dragging && this.dragging.moved()) ||
- (this.boxZoom && this.boxZoom.moved()))) ||
- L.DomEvent._skipped(e)) { return; }
+ _findEventTargets: function (e, type) {
+ var targets = [],
+ target,
+ isHover = type === 'mouseout' || type === 'mouseover',
+ src = e.target || e.srcElement,
+ dragging = false;
- this.fire('preclick');
- this._fireMouseEvent(e);
+ while (src) {
+ target = this._targets[L.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 && !L.DomEvent._isExternalTarget(src, e)) { break; }
+ targets.push(target);
+ if (isHover) { break; }
+ }
+ if (src === this._container) { break; }
+ src = src.parentNode;
+ }
+ if (!targets.length && !dragging && !isHover && L.DomEvent._isExternalTarget(src, e)) {
+ targets = [this];
+ }
+ return targets;
},
- _fireMouseEvent: function (e) {
+ _handleDOMEvent: function (e) {
if (!this._loaded || L.DomEvent._skipped(e)) { return; }
- var type = e.type;
+ var type = e.type === 'keypress' && e.keyCode === 13 ? 'click' : e.type;
+
+ if (type === 'mousedown') {
+ // prevents outline when clicking on keyboard-focusable element
+ L.DomUtil.preventOutline(e.target || e.srcElement);
+ }
- type = (type === 'mouseenter' ? 'mouseover' : (type === 'mouseleave' ? 'mouseout' : type));
+ this._fireDOMEvent(e, type);
+ },
- if (!this.hasEventListeners(type)) { return; }
+ _fireDOMEvent: function (e, type, targets) {
- if (type === 'contextmenu') {
- L.DomEvent.preventDefault(e);
+ 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 = L.Util.extend({}, e);
+ synth.type = 'preclick';
+ this._fireDOMEvent(synth, synth.type, targets);
}
- var containerPoint = this.mouseEventToContainerPoint(e),
- layerPoint = this.containerPointToLayerPoint(containerPoint),
- latlng = this.layerPointToLatLng(layerPoint);
+ if (e._stopped) { return; }
- this.fire(type, {
- latlng: latlng,
- layerPoint: layerPoint,
- containerPoint: containerPoint,
+ // 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)) {
+ L.DomEvent.preventDefault(e);
+ }
+
+ var data = {
originalEvent: e
- });
- },
+ };
+
+ if (e.type !== 'keypress') {
+ var isMarker = target instanceof L.Marker;
+ 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);
+ }
- _onTileLayerLoad: function () {
- this._tileLayersToLoad--;
- if (this._tileLayersNum && !this._tileLayersToLoad) {
- this.fire('tilelayersload');
+ for (var i = 0; i < targets.length; i++) {
+ targets[i].fire(type, data, true);
+ if (data.originalEvent._stopped ||
+ (targets[i].options.nonBubblingEvents && L.Util.indexOf(targets[i].options.nonBubblingEvents, 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, this);
+ callback.call(context || this, {target: this});
} else {
this.on('load', callback, context);
}
return this;
},
- _layerAdd: function (layer) {
- layer.onAdd(this);
- this.fire('layeradd', {layer: layer});
- },
-
// private methods for getting map state
_getMapPanePos: function () {
- return L.DomUtil.getPosition(this._mapPane);
+ return L.DomUtil.getPosition(this._mapPane) || new L.Point(0, 0);
},
_moved: function () {
@@ -2238,19 +3339,21 @@ L.Map = L.Class.extend({
return pos && !pos.equals([0, 0]);
},
- _getTopLeftPoint: function () {
- return this.getPixelOrigin().subtract(this._getMapPanePos());
+ _getTopLeftPoint: function (center, zoom) {
+ var pixelOrigin = center && zoom !== undefined ?
+ this._getNewPixelOrigin(center, zoom) :
+ this.getPixelOrigin();
+ return pixelOrigin.subtract(this._getMapPanePos());
},
- _getNewTopLeftPoint: function (center, zoom) {
+ _getNewPixelOrigin: function (center, zoom) {
var viewHalf = this.getSize()._divideBy(2);
- // TODO round on display, not calculation to increase precision?
- return this.project(center, zoom)._subtract(viewHalf)._round();
+ return this.project(center, zoom)._subtract(viewHalf)._add(this._getMapPanePos())._round();
},
- _latLngToNewLayerPoint: function (latlng, newZoom, newCenter) {
- var topLeft = this._getNewTopLeftPoint(newCenter, newZoom).add(this._getMapPanePos());
- return this.project(latlng, newZoom)._subtract(topLeft);
+ _latLngToNewLayerPoint: function (latlng, zoom, center) {
+ var topLeft = this._getNewPixelOrigin(center, zoom);
+ return this.project(latlng, zoom)._subtract(topLeft);
},
// layer point of the current center
@@ -2273,6 +3376,13 @@ L.Map = L.Class.extend({
viewBounds = new L.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);
},
@@ -2288,11 +3398,15 @@ L.Map = L.Class.extend({
// returns offset needed for pxBounds to get inside maxBounds at a specified zoom
_getBoundsOffset: function (pxBounds, maxBounds, zoom) {
- var nwOffset = this.project(maxBounds.getNorthWest(), zoom).subtract(pxBounds.min),
- seOffset = this.project(maxBounds.getSouthEast(), zoom).subtract(pxBounds.max),
+ var projectedMaxBounds = L.bounds(
+ 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(nwOffset.x, -seOffset.x),
- dy = this._rebound(nwOffset.y, -seOffset.y);
+ dx = this._rebound(minOffset.x, -maxOffset.x),
+ dy = this._rebound(minOffset.y, -maxOffset.y);
return new L.Point(dx, dy);
},
@@ -2305,1941 +3419,3589 @@ L.Map = L.Class.extend({
_limitZoom: function (zoom) {
var min = this.getMinZoom(),
- max = this.getMaxZoom();
-
+ max = this.getMaxZoom(),
+ snap = L.Browser.any3d ? this.options.zoomSnap : 1;
+ if (snap) {
+ zoom = Math.round(zoom / snap) * snap;
+ }
return Math.max(min, Math.min(max, zoom));
}
});
+// @section
+
+// @factory L.map(id: String, options?: Map options)
+// Instantiates a map object given the DOM ID of a `
` element
+// and optionally an object literal with `Map options`.
+//
+// @alternative
+// @factory L.map(el: HTMLElement, options?: Map options)
+// Instantiates a map object given an instance of a `
` HTML element
+// and optionally an object literal with `Map options`.
L.map = function (id, options) {
return new L.Map(id, options);
};
-/*
- * Mercator projection that takes into account that the Earth is not a perfect sphere.
- * Less popular than spherical mercator; used by projections like EPSG:3395.
- */
-
-L.Projection.Mercator = {
- MAX_LATITUDE: 85.0840591556,
-
- R_MINOR: 6356752.314245179,
- R_MAJOR: 6378137,
-
- project: function (latlng) { // (LatLng) -> Point
- var d = L.LatLng.DEG_TO_RAD,
- max = this.MAX_LATITUDE,
- lat = Math.max(Math.min(max, latlng.lat), -max),
- r = this.R_MAJOR,
- r2 = this.R_MINOR,
- x = latlng.lng * d * r,
- y = lat * d,
- tmp = r2 / r,
- eccent = Math.sqrt(1.0 - tmp * tmp),
- con = eccent * Math.sin(y);
-
- con = Math.pow((1 - con) / (1 + con), eccent * 0.5);
-
- var ts = Math.tan(0.5 * ((Math.PI * 0.5) - y)) / con;
- y = -r * Math.log(ts);
-
- return new L.Point(x, y);
- },
-
- unproject: function (point) { // (Point, Boolean) -> LatLng
- var d = L.LatLng.RAD_TO_DEG,
- r = this.R_MAJOR,
- r2 = this.R_MINOR,
- lng = point.x * d / r,
- tmp = r2 / r,
- eccent = Math.sqrt(1 - (tmp * tmp)),
- ts = Math.exp(- point.y / r),
- phi = (Math.PI / 2) - 2 * Math.atan(ts),
- numIter = 15,
- tol = 1e-7,
- i = numIter,
- dphi = 0.1,
- con;
-
- while ((Math.abs(dphi) > tol) && (--i > 0)) {
- con = eccent * Math.sin(phi);
- dphi = (Math.PI / 2) - 2 * Math.atan(ts *
- Math.pow((1.0 - con) / (1.0 + con), 0.5 * eccent)) - phi;
- phi += dphi;
- }
-
- return new L.LatLng(phi * d, lng);
- }
-};
-
-L.CRS.EPSG3395 = L.extend({}, L.CRS, {
- code: 'EPSG:3395',
+/*
+ * @class Layer
+ * @inherits Evented
+ * @aka L.Layer
+ * @aka ILayer
+ *
+ * A set of methods from the Layer base class that all Leaflet layers use.
+ * Inherits all methods, options and events from `L.Evented`.
+ *
+ * @example
+ *
+ * ```js
+ * var layer = L.Marker(latlng).addTo(map);
+ * layer.addTo(map);
+ * layer.remove();
+ * ```
+ *
+ * @event add: Event
+ * Fired after the layer is added to a map
+ *
+ * @event remove: Event
+ * Fired after the layer is removed from a map
+ */
- projection: L.Projection.Mercator,
- transformation: (function () {
- var m = L.Projection.Mercator,
- r = m.R_MAJOR,
- scale = 0.5 / (Math.PI * r);
+L.Layer = L.Evented.extend({
- return new L.Transformation(scale, 0.5, -scale, 0.5);
- }())
-});
+ // Classes extending `L.Layer` will inherit the following options:
+ options: {
+ // @option pane: String = 'overlayPane'
+ // By default the layer will be added to the map's [overlay pane](#map-overlaypane). Overriding this option will cause the layer to be placed on another pane by default.
+ pane: 'overlayPane',
+ nonBubblingEvents: [] // Array of events that should not be bubbled to DOM parents (like the map)
+ },
+
+ /* @section
+ * Classes extending `L.Layer` will inherit the following methods:
+ *
+ * @method addTo(map: Map): this
+ * Adds the layer to the given map
+ */
+ addTo: function (map) {
+ map.addLayer(this);
+ return this;
+ },
+ // @method remove: this
+ // Removes the layer from the map it is currently active on.
+ remove: function () {
+ return this.removeFrom(this._map || this._mapToAdd);
+ },
-/*
- * L.TileLayer is used for standard xyz-numbered tile layers.
- */
+ // @method removeFrom(map: Map): this
+ // Removes the layer from the given map
+ removeFrom: function (obj) {
+ if (obj) {
+ obj.removeLayer(this);
+ }
+ return this;
+ },
-L.TileLayer = L.Class.extend({
- includes: L.Mixin.Events,
+ // @method getPane(name? : String): HTMLElement
+ // Returns the `HTMLElement` representing the named pane on the map. If `name` is omitted, returns the pane for this layer.
+ getPane: function (name) {
+ return this._map.getPane(name ? (this.options[name] || name) : this.options.pane);
+ },
- options: {
- minZoom: 0,
- maxZoom: 18,
- tileSize: 256,
- subdomains: 'abc',
- errorTileUrl: '',
- attribution: '',
- zoomOffset: 0,
- opacity: 1,
- /*
- maxNativeZoom: null,
- zIndex: null,
- tms: false,
- continuousWorld: false,
- noWrap: false,
- zoomReverse: false,
- detectRetina: false,
- reuseTiles: false,
- bounds: false,
- */
- unloadInvisibleTiles: L.Browser.mobile,
- updateWhenIdle: L.Browser.mobile
+ addInteractiveTarget: function (targetEl) {
+ this._map._targets[L.stamp(targetEl)] = this;
+ return this;
},
- initialize: function (url, options) {
- options = L.setOptions(this, options);
+ removeInteractiveTarget: function (targetEl) {
+ delete this._map._targets[L.stamp(targetEl)];
+ return this;
+ },
- // detecting retina displays, adjusting tileSize and zoom levels
- if (options.detectRetina && L.Browser.retina && options.maxZoom > 0) {
+ _layerAdd: function (e) {
+ var map = e.target;
- options.tileSize = Math.floor(options.tileSize / 2);
- options.zoomOffset++;
+ // check in case layer gets added and then removed before the map is ready
+ if (!map.hasLayer(this)) { return; }
- if (options.minZoom > 0) {
- options.minZoom--;
- }
- this.options.maxZoom--;
- }
+ this._map = map;
+ this._zoomAnimated = map._zoomAnimated;
- if (options.bounds) {
- options.bounds = L.latLngBounds(options.bounds);
+ if (this.getEvents) {
+ var events = this.getEvents();
+ map.on(events, this);
+ this.once('remove', function () {
+ map.off(events, this);
+ }, this);
}
- this._url = url;
-
- var subdomains = this.options.subdomains;
+ this.onAdd(map);
- if (typeof subdomains === 'string') {
- this.options.subdomains = subdomains.split('');
+ if (this.getAttribution && this._map.attributionControl) {
+ this._map.attributionControl.addAttribution(this.getAttribution());
}
- },
- onAdd: function (map) {
- this._map = map;
- this._animated = map._zoomAnimated;
+ this.fire('add');
+ map.fire('layeradd', {layer: this});
+ }
+});
- // create a container div for tiles
- this._initContainer();
+/* @section Extension methods
+ * @uninheritable
+ *
+ * Every layer should extend from `L.Layer` and (re-)implement the following methods.
+ *
+ * @method onAdd(map: Map): this
+ * Should contain code that creates DOM elements for the layer, adds them to `map panes` where they should belong and puts listeners on relevant map events. Called on [`map.addLayer(layer)`](#map-addlayer).
+ *
+ * @method onRemove(map: Map): this
+ * Should contain all clean up code that removes the layer's elements from the DOM and removes listeners previously added in [`onAdd`](#layer-onadd). Called on [`map.removeLayer(layer)`](#map-removelayer).
+ *
+ * @method getEvents(): Object
+ * This optional method should return an object like `{ viewreset: this._reset }` for [`addEventListener`](#evented-addeventlistener). The event handlers in this object will be automatically added and removed from the map with your layer.
+ *
+ * @method getAttribution(): String
+ * This optional method should return a string containing HTML to be shown on the `Attribution control` whenever the layer is visible.
+ *
+ * @method beforeAdd(map: Map): this
+ * Optional method. Called on [`map.addLayer(layer)`](#map-addlayer), before the layer is added to the map, before events are initialized, without waiting until the map is in a usable state. Use for early initialization only.
+ */
- // set up events
- map.on({
- 'viewreset': this._reset,
- 'moveend': this._update
- }, this);
- if (this._animated) {
- map.on({
- 'zoomanim': this._animateZoom,
- 'zoomend': this._endZoomAnim
- }, this);
- }
+/* @namespace Map
+ * @section Layer events
+ *
+ * @event layeradd: LayerEvent
+ * Fired when a new layer is added to the map.
+ *
+ * @event layerremove: LayerEvent
+ * Fired when some layer is removed from the map
+ *
+ * @section Methods for Layers and Controls
+ */
+L.Map.include({
+ // @method addLayer(layer: Layer): this
+ // Adds the given layer to the map
+ addLayer: function (layer) {
+ var id = L.stamp(layer);
+ if (this._layers[id]) { return this; }
+ this._layers[id] = layer;
- if (!this.options.updateWhenIdle) {
- this._limitedUpdate = L.Util.limitExecByInterval(this._update, 150, this);
- map.on('move', this._limitedUpdate, this);
+ layer._mapToAdd = this;
+
+ if (layer.beforeAdd) {
+ layer.beforeAdd(this);
}
- this._reset();
- this._update();
- },
+ this.whenReady(layer._layerAdd, layer);
- addTo: function (map) {
- map.addLayer(this);
return this;
},
- onRemove: function (map) {
- this._container.parentNode.removeChild(this._container);
+ // @method removeLayer(layer: Layer): this
+ // Removes the given layer from the map.
+ removeLayer: function (layer) {
+ var id = L.stamp(layer);
- map.off({
- 'viewreset': this._reset,
- 'moveend': this._update
- }, this);
+ if (!this._layers[id]) { return this; }
- if (this._animated) {
- map.off({
- 'zoomanim': this._animateZoom,
- 'zoomend': this._endZoomAnim
- }, this);
+ if (this._loaded) {
+ layer.onRemove(this);
}
- if (!this.options.updateWhenIdle) {
- map.off('move', this._limitedUpdate, this);
+ if (layer.getAttribution && this.attributionControl) {
+ this.attributionControl.removeAttribution(layer.getAttribution());
}
- this._container = null;
- this._map = null;
- },
-
- bringToFront: function () {
- var pane = this._map._panes.tilePane;
+ delete this._layers[id];
- if (this._container) {
- pane.appendChild(this._container);
- this._setAutoZIndex(pane, Math.max);
+ if (this._loaded) {
+ this.fire('layerremove', {layer: layer});
+ layer.fire('remove');
}
+ layer._map = layer._mapToAdd = null;
+
return this;
},
- bringToBack: function () {
- var pane = this._map._panes.tilePane;
+ // @method hasLayer(layer: Layer): Boolean
+ // Returns `true` if the given layer is currently added to the map
+ hasLayer: function (layer) {
+ return !!layer && (L.stamp(layer) in this._layers);
+ },
- if (this._container) {
- pane.insertBefore(this._container, pane.firstChild);
- this._setAutoZIndex(pane, Math.min);
+ /* @method eachLayer(fn: Function, context?: Object): this
+ * Iterates over the layers of the map, optionally specifying context of the iterator function.
+ * ```
+ * map.eachLayer(function(layer){
+ * layer.bindPopup('Hello');
+ * });
+ * ```
+ */
+ eachLayer: function (method, context) {
+ for (var i in this._layers) {
+ method.call(context, this._layers[i]);
}
-
return this;
},
- getAttribution: function () {
- return this.options.attribution;
- },
+ _addLayers: function (layers) {
+ layers = layers ? (L.Util.isArray(layers) ? layers : [layers]) : [];
- getContainer: function () {
- return this._container;
+ for (var i = 0, len = layers.length; i < len; i++) {
+ this.addLayer(layers[i]);
+ }
},
- setOpacity: function (opacity) {
- this.options.opacity = opacity;
-
- if (this._map) {
- this._updateOpacity();
+ _addZoomLimit: function (layer) {
+ if (isNaN(layer.options.maxZoom) || !isNaN(layer.options.minZoom)) {
+ this._zoomBoundLayers[L.stamp(layer)] = layer;
+ this._updateZoomLevels();
}
-
- return this;
},
- setZIndex: function (zIndex) {
- this.options.zIndex = zIndex;
- this._updateZIndex();
+ _removeZoomLimit: function (layer) {
+ var id = L.stamp(layer);
- return this;
+ if (this._zoomBoundLayers[id]) {
+ delete this._zoomBoundLayers[id];
+ this._updateZoomLevels();
+ }
},
- setUrl: function (url, noRedraw) {
- this._url = url;
+ _updateZoomLevels: function () {
+ var minZoom = Infinity,
+ maxZoom = -Infinity,
+ oldZoomSpan = this._getZoomSpan();
- if (!noRedraw) {
- this.redraw();
+ for (var i in this._zoomBoundLayers) {
+ var options = this._zoomBoundLayers[i].options;
+
+ minZoom = options.minZoom === undefined ? minZoom : Math.min(minZoom, options.minZoom);
+ maxZoom = options.maxZoom === undefined ? maxZoom : Math.max(maxZoom, options.maxZoom);
}
- return this;
- },
+ this._layersMaxZoom = maxZoom === -Infinity ? undefined : maxZoom;
+ this._layersMinZoom = minZoom === Infinity ? undefined : minZoom;
- redraw: function () {
- if (this._map) {
- this._reset({hard: true});
- this._update();
+ // @section Map state change events
+ // @event zoomlevelschange: Event
+ // Fired when the number of zoomlevels on the map is changed due
+ // to adding or removing a layer.
+ if (oldZoomSpan !== this._getZoomSpan()) {
+ this.fire('zoomlevelschange');
}
- return this;
- },
+ }
+});
+
- _updateZIndex: function () {
- if (this._container && this.options.zIndex !== undefined) {
- this._container.style.zIndex = this.options.zIndex;
- }
- },
- _setAutoZIndex: function (pane, compare) {
+/*
+ * @namespace Projection
+ * @projection L.Projection.Mercator
+ *
+ * Elliptical Mercator projection â more complex than Spherical Mercator. Takes into account that Earth is a geoid, not a perfect sphere. Used by the EPSG:3395 CRS.
+ */
- var layers = pane.children,
- edgeZIndex = -compare(Infinity, -Infinity), // -Infinity for max, Infinity for min
- zIndex, i, len;
+L.Projection.Mercator = {
+ R: 6378137,
+ R_MINOR: 6356752.314245179,
- for (i = 0, len = layers.length; i < len; i++) {
+ bounds: L.bounds([-20037508.34279, -15496570.73972], [20037508.34279, 18764656.23138]),
- if (layers[i] !== this._container) {
- zIndex = parseInt(layers[i].style.zIndex, 10);
+ project: function (latlng) {
+ var d = Math.PI / 180,
+ r = this.R,
+ y = latlng.lat * d,
+ tmp = this.R_MINOR / r,
+ e = Math.sqrt(1 - tmp * tmp),
+ con = e * Math.sin(y);
- if (!isNaN(zIndex)) {
- edgeZIndex = compare(edgeZIndex, zIndex);
- }
- }
- }
+ var ts = Math.tan(Math.PI / 4 - y / 2) / Math.pow((1 - con) / (1 + con), e / 2);
+ y = -r * Math.log(Math.max(ts, 1E-10));
- this.options.zIndex = this._container.style.zIndex =
- (isFinite(edgeZIndex) ? edgeZIndex : 0) + compare(1, -1);
+ return new L.Point(latlng.lng * d * r, y);
},
- _updateOpacity: function () {
- var i,
- tiles = this._tiles;
-
- if (L.Browser.ielt9) {
- for (i in tiles) {
- L.DomUtil.setOpacity(tiles[i], this.options.opacity);
- }
- } else {
- L.DomUtil.setOpacity(this._container, this.options.opacity);
+ unproject: function (point) {
+ var d = 180 / Math.PI,
+ r = this.R,
+ tmp = this.R_MINOR / r,
+ e = Math.sqrt(1 - tmp * tmp),
+ ts = Math.exp(-point.y / r),
+ phi = Math.PI / 2 - 2 * Math.atan(ts);
+
+ for (var i = 0, dphi = 0.1, con; i < 15 && Math.abs(dphi) > 1e-7; i++) {
+ con = e * Math.sin(phi);
+ con = Math.pow((1 - con) / (1 + con), e / 2);
+ dphi = Math.PI / 2 - 2 * Math.atan(ts * con) - phi;
+ phi += dphi;
}
- },
- _initContainer: function () {
- var tilePane = this._map._panes.tilePane;
+ return new L.LatLng(phi * d, point.x * d / r);
+ }
+};
- if (!this._container) {
- this._container = L.DomUtil.create('div', 'leaflet-layer');
- this._updateZIndex();
- if (this._animated) {
- var className = 'leaflet-tile-container';
+/*
+ * @namespace CRS
+ * @crs L.CRS.EPSG3395
+ *
+ * Rarely used by some commercial tile providers. Uses Elliptical Mercator projection.
+ */
- this._bgBuffer = L.DomUtil.create('div', className, this._container);
- this._tileContainer = L.DomUtil.create('div', className, this._container);
+L.CRS.EPSG3395 = L.extend({}, L.CRS.Earth, {
+ code: 'EPSG:3395',
+ projection: L.Projection.Mercator,
- } else {
- this._tileContainer = this._container;
- }
+ transformation: (function () {
+ var scale = 0.5 / (Math.PI * L.Projection.Mercator.R);
+ return new L.Transformation(scale, 0.5, -scale, 0.5);
+ }())
+});
- tilePane.appendChild(this._container);
- if (this.options.opacity < 1) {
- this._updateOpacity();
- }
- }
- },
- _reset: function (e) {
- for (var key in this._tiles) {
- this.fire('tileunload', {tile: this._tiles[key]});
- }
+/*
+ * @class GridLayer
+ * @inherits Layer
+ * @aka L.GridLayer
+ *
+ * Generic class for handling a tiled grid of HTML elements. This is the base class for all tile layers and replaces `TileLayer.Canvas`.
+ * GridLayer can be extended to create a tiled grid of HTML elements like `