2 Leaflet, a JavaScript library for mobile-friendly interactive maps. http://leafletjs.com
3 (c) 2010-2013, Vladimir Agafonkin
4 (c) 2010-2011, CloudMade
6 (function (window, document, undefined) {
12 // define Leaflet for Node module pattern loaders, including Browserify
13 if (typeof module === 'object' && typeof module.exports === 'object') {
16 // define Leaflet as an AMD module
17 } else if (typeof define === 'function' && define.amd) {
21 // define Leaflet as a global L variable, saving the original L to restore later if needed
23 L.noConflict = function () {
32 * L.Util contains various utility functions used throughout Leaflet code.
36 extend: function (dest) { // (Object[, Object, ...]) ->
37 var sources = Array.prototype.slice.call(arguments, 1),
40 for (j = 0, len = sources.length; j < len; j++) {
41 src = sources[j] || {};
43 if (src.hasOwnProperty(i)) {
51 bind: function (fn, obj) { // (Function, Object) -> Function
52 var args = arguments.length > 2 ? Array.prototype.slice.call(arguments, 2) : null;
54 return fn.apply(obj, args || arguments);
61 return function (obj) {
62 obj[key] = obj[key] || ++lastId;
67 invokeEach: function (obj, method, context) {
70 if (typeof obj === 'object') {
71 args = Array.prototype.slice.call(arguments, 3);
74 method.apply(context, [i, obj[i]].concat(args));
82 limitExecByInterval: function (fn, time, context) {
83 var lock, execOnUnlock;
85 return function wrapperFn() {
95 setTimeout(function () {
99 wrapperFn.apply(context, args);
100 execOnUnlock = false;
104 fn.apply(context, args);
108 falseFn: function () {
112 formatNum: function (num, digits) {
113 var pow = Math.pow(10, digits || 5);
114 return Math.round(num * pow) / pow;
117 trim: function (str) {
118 return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, '');
121 splitWords: function (str) {
122 return L.Util.trim(str).split(/\s+/);
125 setOptions: function (obj, options) {
126 obj.options = L.extend({}, obj.options, options);
130 getParamString: function (obj, existingUrl, uppercase) {
133 params.push(encodeURIComponent(uppercase ? i.toUpperCase() : i) + '=' + encodeURIComponent(obj[i]));
135 return ((!existingUrl || existingUrl.indexOf('?') === -1) ? '?' : '&') + params.join('&');
137 template: function (str, data) {
138 return str.replace(/\{ *([\w_]+) *\}/g, function (str, key) {
139 var value = data[key];
140 if (value === undefined) {
141 throw new Error('No value provided for variable ' + str);
142 } else if (typeof value === 'function') {
149 isArray: Array.isArray || function (obj) {
150 return (Object.prototype.toString.call(obj) === '[object Array]');
153 emptyImageUrl: 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='
158 // inspired by http://paulirish.com/2011/requestanimationframe-for-smart-animating/
160 function getPrefixed(name) {
162 prefixes = ['webkit', 'moz', 'o', 'ms'];
164 for (i = 0; i < prefixes.length && !fn; i++) {
165 fn = window[prefixes[i] + name];
173 function timeoutDefer(fn) {
174 var time = +new Date(),
175 timeToCall = Math.max(0, 16 - (time - lastTime));
177 lastTime = time + timeToCall;
178 return window.setTimeout(fn, timeToCall);
181 var requestFn = window.requestAnimationFrame ||
182 getPrefixed('RequestAnimationFrame') || timeoutDefer;
184 var cancelFn = window.cancelAnimationFrame ||
185 getPrefixed('CancelAnimationFrame') ||
186 getPrefixed('CancelRequestAnimationFrame') ||
187 function (id) { window.clearTimeout(id); };
190 L.Util.requestAnimFrame = function (fn, context, immediate, element) {
191 fn = L.bind(fn, context);
193 if (immediate && requestFn === timeoutDefer) {
196 return requestFn.call(window, fn, element);
200 L.Util.cancelAnimFrame = function (id) {
202 cancelFn.call(window, id);
208 // shortcuts for most used utility functions
209 L.extend = L.Util.extend;
210 L.bind = L.Util.bind;
211 L.stamp = L.Util.stamp;
212 L.setOptions = L.Util.setOptions;
216 * L.Class powers the OOP facilities of the library.
217 * Thanks to John Resig and Dean Edwards for inspiration!
220 L.Class = function () {};
222 L.Class.extend = function (props) {
224 // extended class with the new prototype
225 var NewClass = function () {
227 // call the constructor
228 if (this.initialize) {
229 this.initialize.apply(this, arguments);
232 // call all constructor hooks
233 if (this._initHooks) {
234 this.callInitHooks();
238 // instantiate class without calling constructor
239 var F = function () {};
240 F.prototype = this.prototype;
243 proto.constructor = NewClass;
245 NewClass.prototype = proto;
247 //inherit parent's statics
248 for (var i in this) {
249 if (this.hasOwnProperty(i) && i !== 'prototype') {
250 NewClass[i] = this[i];
254 // mix static properties into the class
256 L.extend(NewClass, props.statics);
257 delete props.statics;
260 // mix includes into the prototype
261 if (props.includes) {
262 L.Util.extend.apply(null, [proto].concat(props.includes));
263 delete props.includes;
267 if (props.options && proto.options) {
268 props.options = L.extend({}, proto.options, props.options);
271 // mix given properties into the prototype
272 L.extend(proto, props);
274 proto._initHooks = [];
277 // jshint camelcase: false
278 NewClass.__super__ = parent.prototype;
280 // add method for calling all hooks
281 proto.callInitHooks = function () {
283 if (this._initHooksCalled) { return; }
285 if (parent.prototype.callInitHooks) {
286 parent.prototype.callInitHooks.call(this);
289 this._initHooksCalled = true;
291 for (var i = 0, len = proto._initHooks.length; i < len; i++) {
292 proto._initHooks[i].call(this);
300 // method for adding properties to prototype
301 L.Class.include = function (props) {
302 L.extend(this.prototype, props);
305 // merge new default options to the Class
306 L.Class.mergeOptions = function (options) {
307 L.extend(this.prototype.options, options);
310 // add a constructor hook
311 L.Class.addInitHook = function (fn) { // (Function) || (String, args...)
312 var args = Array.prototype.slice.call(arguments, 1);
314 var init = typeof fn === 'function' ? fn : function () {
315 this[fn].apply(this, args);
318 this.prototype._initHooks = this.prototype._initHooks || [];
319 this.prototype._initHooks.push(init);
324 * L.Mixin.Events is used to add custom events functionality to Leaflet classes.
327 var eventsKey = '_leaflet_events';
333 addEventListener: function (types, fn, context) { // (String, Function[, Object]) or (Object[, Object])
335 // types can be a map of types/handlers
336 if (L.Util.invokeEach(types, this.addEventListener, this, fn, context)) { return this; }
338 var events = this[eventsKey] = this[eventsKey] || {},
339 contextId = context && context !== this && L.stamp(context),
340 i, len, event, type, indexKey, indexLenKey, typeIndex;
342 // types can be a string of space-separated words
343 types = L.Util.splitWords(types);
345 for (i = 0, len = types.length; i < len; i++) {
348 context: context || this
353 // store listeners of a particular context in a separate hash (if it has an id)
354 // gives a major performance boost when removing thousands of map layers
356 indexKey = type + '_idx';
357 indexLenKey = indexKey + '_len';
359 typeIndex = events[indexKey] = events[indexKey] || {};
361 if (!typeIndex[contextId]) {
362 typeIndex[contextId] = [];
364 // keep track of the number of keys in the index to quickly check if it's empty
365 events[indexLenKey] = (events[indexLenKey] || 0) + 1;
368 typeIndex[contextId].push(event);
372 events[type] = events[type] || [];
373 events[type].push(event);
380 hasEventListeners: function (type) { // (String) -> Boolean
381 var events = this[eventsKey];
382 return !!events && ((type in events && events[type].length > 0) ||
383 (type + '_idx' in events && events[type + '_idx_len'] > 0));
386 removeEventListener: function (types, fn, context) { // ([String, Function, Object]) or (Object[, Object])
388 if (!this[eventsKey]) {
393 return this.clearAllEventListeners();
396 if (L.Util.invokeEach(types, this.removeEventListener, this, fn, context)) { return this; }
398 var events = this[eventsKey],
399 contextId = context && context !== this && L.stamp(context),
400 i, len, type, listeners, j, indexKey, indexLenKey, typeIndex, removed;
402 types = L.Util.splitWords(types);
404 for (i = 0, len = types.length; i < len; i++) {
406 indexKey = type + '_idx';
407 indexLenKey = indexKey + '_len';
409 typeIndex = events[indexKey];
412 // clear all listeners for a type if function isn't specified
414 delete events[indexKey];
415 delete events[indexLenKey];
418 listeners = contextId && typeIndex ? typeIndex[contextId] : events[type];
421 for (j = listeners.length - 1; j >= 0; j--) {
422 if ((listeners[j].action === fn) && (!context || (listeners[j].context === context))) {
423 removed = listeners.splice(j, 1);
424 // set the old action to a no-op, because it is possible
425 // that the listener is being iterated over as part of a dispatch
426 removed[0].action = L.Util.falseFn;
430 if (context && typeIndex && (listeners.length === 0)) {
431 delete typeIndex[contextId];
432 events[indexLenKey]--;
441 clearAllEventListeners: function () {
442 delete this[eventsKey];
446 fireEvent: function (type, data) { // (String[, Object])
447 if (!this.hasEventListeners(type)) {
451 var event = L.Util.extend({}, data, { type: type, target: this });
453 var events = this[eventsKey],
454 listeners, i, len, typeIndex, contextId;
457 // make sure adding/removing listeners inside other listeners won't cause infinite loop
458 listeners = events[type].slice();
460 for (i = 0, len = listeners.length; i < len; i++) {
461 listeners[i].action.call(listeners[i].context, event);
465 // fire event for the context-indexed listeners as well
466 typeIndex = events[type + '_idx'];
468 for (contextId in typeIndex) {
469 listeners = typeIndex[contextId].slice();
472 for (i = 0, len = listeners.length; i < len; i++) {
473 listeners[i].action.call(listeners[i].context, event);
481 addOneTimeEventListener: function (types, fn, context) {
483 if (L.Util.invokeEach(types, this.addOneTimeEventListener, this, fn, context)) { return this; }
485 var handler = L.bind(function () {
487 .removeEventListener(types, fn, context)
488 .removeEventListener(types, handler, context);
492 .addEventListener(types, fn, context)
493 .addEventListener(types, handler, context);
497 L.Mixin.Events.on = L.Mixin.Events.addEventListener;
498 L.Mixin.Events.off = L.Mixin.Events.removeEventListener;
499 L.Mixin.Events.once = L.Mixin.Events.addOneTimeEventListener;
500 L.Mixin.Events.fire = L.Mixin.Events.fireEvent;
504 * L.Browser handles different browser and feature detections for internal Leaflet use.
509 var ie = 'ActiveXObject' in window,
510 ielt9 = ie && !document.addEventListener,
512 // terrible browser detection to work around Safari / iOS / Android browser bugs
513 ua = navigator.userAgent.toLowerCase(),
514 webkit = ua.indexOf('webkit') !== -1,
515 chrome = ua.indexOf('chrome') !== -1,
516 phantomjs = ua.indexOf('phantom') !== -1,
517 android = ua.indexOf('android') !== -1,
518 android23 = ua.search('android [23]') !== -1,
519 gecko = ua.indexOf('gecko') !== -1,
521 mobile = typeof orientation !== undefined + '',
522 msPointer = window.navigator && window.navigator.msPointerEnabled &&
523 window.navigator.msMaxTouchPoints && !window.PointerEvent,
524 pointer = (window.PointerEvent && window.navigator.pointerEnabled && window.navigator.maxTouchPoints) ||
526 retina = ('devicePixelRatio' in window && window.devicePixelRatio > 1) ||
527 ('matchMedia' in window && window.matchMedia('(min-resolution:144dpi)') &&
528 window.matchMedia('(min-resolution:144dpi)').matches),
530 doc = document.documentElement,
531 ie3d = ie && ('transition' in doc.style),
532 webkit3d = ('WebKitCSSMatrix' in window) && ('m11' in new window.WebKitCSSMatrix()) && !android23,
533 gecko3d = 'MozPerspective' in doc.style,
534 opera3d = 'OTransition' in doc.style,
535 any3d = !window.L_DISABLE_3D && (ie3d || webkit3d || gecko3d || opera3d) && !phantomjs;
538 // PhantomJS has 'ontouchstart' in document.documentElement, but doesn't actually support touch.
539 // https://github.com/Leaflet/Leaflet/pull/1434#issuecomment-13843151
541 var touch = !window.L_NO_TOUCH && !phantomjs && (function () {
543 var startName = 'ontouchstart';
545 // IE10+ (We simulate these into touch* events in L.DomEvent and L.DomEvent.Pointer) or WebKit, etc.
546 if (pointer || (startName in doc)) {
551 var div = document.createElement('div'),
554 if (!div.setAttribute) {
557 div.setAttribute(startName, 'return;');
559 if (typeof div[startName] === 'function') {
563 div.removeAttribute(startName);
574 gecko: gecko && !webkit && !window.opera && !ie,
577 android23: android23,
588 mobileWebkit: mobile && webkit,
589 mobileWebkit3d: mobile && webkit3d,
590 mobileOpera: mobile && window.opera,
593 msPointer: msPointer,
603 * L.Point represents a point with x and y coordinates.
606 L.Point = function (/*Number*/ x, /*Number*/ y, /*Boolean*/ round) {
607 this.x = (round ? Math.round(x) : x);
608 this.y = (round ? Math.round(y) : y);
611 L.Point.prototype = {
614 return new L.Point(this.x, this.y);
617 // non-destructive, returns a new point
618 add: function (point) {
619 return this.clone()._add(L.point(point));
622 // destructive, used directly for performance in situations where it's safe to modify existing point
623 _add: function (point) {
629 subtract: function (point) {
630 return this.clone()._subtract(L.point(point));
633 _subtract: function (point) {
639 divideBy: function (num) {
640 return this.clone()._divideBy(num);
643 _divideBy: function (num) {
649 multiplyBy: function (num) {
650 return this.clone()._multiplyBy(num);
653 _multiplyBy: function (num) {
660 return this.clone()._round();
663 _round: function () {
664 this.x = Math.round(this.x);
665 this.y = Math.round(this.y);
670 return this.clone()._floor();
673 _floor: function () {
674 this.x = Math.floor(this.x);
675 this.y = Math.floor(this.y);
679 distanceTo: function (point) {
680 point = L.point(point);
682 var x = point.x - this.x,
683 y = point.y - this.y;
685 return Math.sqrt(x * x + y * y);
688 equals: function (point) {
689 point = L.point(point);
691 return point.x === this.x &&
695 contains: function (point) {
696 point = L.point(point);
698 return Math.abs(point.x) <= Math.abs(this.x) &&
699 Math.abs(point.y) <= Math.abs(this.y);
702 toString: function () {
704 L.Util.formatNum(this.x) + ', ' +
705 L.Util.formatNum(this.y) + ')';
709 L.point = function (x, y, round) {
710 if (x instanceof L.Point) {
713 if (L.Util.isArray(x)) {
714 return new L.Point(x[0], x[1]);
716 if (x === undefined || x === null) {
719 return new L.Point(x, y, round);
724 * L.Bounds represents a rectangular area on the screen in pixel coordinates.
727 L.Bounds = function (a, b) { //(Point, Point) or Point[]
730 var points = b ? [a, b] : a;
732 for (var i = 0, len = points.length; i < len; i++) {
733 this.extend(points[i]);
737 L.Bounds.prototype = {
738 // extend the bounds to contain the given point
739 extend: function (point) { // (Point)
740 point = L.point(point);
742 if (!this.min && !this.max) {
743 this.min = point.clone();
744 this.max = point.clone();
746 this.min.x = Math.min(point.x, this.min.x);
747 this.max.x = Math.max(point.x, this.max.x);
748 this.min.y = Math.min(point.y, this.min.y);
749 this.max.y = Math.max(point.y, this.max.y);
754 getCenter: function (round) { // (Boolean) -> Point
756 (this.min.x + this.max.x) / 2,
757 (this.min.y + this.max.y) / 2, round);
760 getBottomLeft: function () { // -> Point
761 return new L.Point(this.min.x, this.max.y);
764 getTopRight: function () { // -> Point
765 return new L.Point(this.max.x, this.min.y);
768 getSize: function () {
769 return this.max.subtract(this.min);
772 contains: function (obj) { // (Bounds) or (Point) -> Boolean
775 if (typeof obj[0] === 'number' || obj instanceof L.Point) {
781 if (obj instanceof L.Bounds) {
788 return (min.x >= this.min.x) &&
789 (max.x <= this.max.x) &&
790 (min.y >= this.min.y) &&
791 (max.y <= this.max.y);
794 intersects: function (bounds) { // (Bounds) -> Boolean
795 bounds = L.bounds(bounds);
801 xIntersects = (max2.x >= min.x) && (min2.x <= max.x),
802 yIntersects = (max2.y >= min.y) && (min2.y <= max.y);
804 return xIntersects && yIntersects;
807 isValid: function () {
808 return !!(this.min && this.max);
812 L.bounds = function (a, b) { // (Bounds) or (Point, Point) or (Point[])
813 if (!a || a instanceof L.Bounds) {
816 return new L.Bounds(a, b);
821 * L.Transformation is an utility class to perform simple point transformations through a 2d-matrix.
824 L.Transformation = function (a, b, c, d) {
831 L.Transformation.prototype = {
832 transform: function (point, scale) { // (Point, Number) -> Point
833 return this._transform(point.clone(), scale);
836 // destructive transform (faster)
837 _transform: function (point, scale) {
839 point.x = scale * (this._a * point.x + this._b);
840 point.y = scale * (this._c * point.y + this._d);
844 untransform: function (point, scale) {
847 (point.x / scale - this._b) / this._a,
848 (point.y / scale - this._d) / this._c);
854 * L.DomUtil contains various utility functions for working with DOM.
859 return (typeof id === 'string' ? document.getElementById(id) : id);
862 getStyle: function (el, style) {
864 var value = el.style[style];
866 if (!value && el.currentStyle) {
867 value = el.currentStyle[style];
870 if ((!value || value === 'auto') && document.defaultView) {
871 var css = document.defaultView.getComputedStyle(el, null);
872 value = css ? css[style] : null;
875 return value === 'auto' ? null : value;
878 getViewportOffset: function (element) {
883 docBody = document.body,
884 docEl = document.documentElement,
888 top += el.offsetTop || 0;
889 left += el.offsetLeft || 0;
892 top += parseInt(L.DomUtil.getStyle(el, 'borderTopWidth'), 10) || 0;
893 left += parseInt(L.DomUtil.getStyle(el, 'borderLeftWidth'), 10) || 0;
895 pos = L.DomUtil.getStyle(el, 'position');
897 if (el.offsetParent === docBody && pos === 'absolute') { break; }
899 if (pos === 'fixed') {
900 top += docBody.scrollTop || docEl.scrollTop || 0;
901 left += docBody.scrollLeft || docEl.scrollLeft || 0;
905 if (pos === 'relative' && !el.offsetLeft) {
906 var width = L.DomUtil.getStyle(el, 'width'),
907 maxWidth = L.DomUtil.getStyle(el, 'max-width'),
908 r = el.getBoundingClientRect();
910 if (width !== 'none' || maxWidth !== 'none') {
911 left += r.left + el.clientLeft;
914 //calculate full y offset since we're breaking out of the loop
915 top += r.top + (docBody.scrollTop || docEl.scrollTop || 0);
920 el = el.offsetParent;
927 if (el === docBody) { break; }
929 top -= el.scrollTop || 0;
930 left -= el.scrollLeft || 0;
935 return new L.Point(left, top);
938 documentIsLtr: function () {
939 if (!L.DomUtil._docIsLtrCached) {
940 L.DomUtil._docIsLtrCached = true;
941 L.DomUtil._docIsLtr = L.DomUtil.getStyle(document.body, 'direction') === 'ltr';
943 return L.DomUtil._docIsLtr;
946 create: function (tagName, className, container) {
948 var el = document.createElement(tagName);
949 el.className = className;
952 container.appendChild(el);
958 hasClass: function (el, name) {
959 if (el.classList !== undefined) {
960 return el.classList.contains(name);
962 var className = L.DomUtil._getClass(el);
963 return className.length > 0 && new RegExp('(^|\\s)' + name + '(\\s|$)').test(className);
966 addClass: function (el, name) {
967 if (el.classList !== undefined) {
968 var classes = L.Util.splitWords(name);
969 for (var i = 0, len = classes.length; i < len; i++) {
970 el.classList.add(classes[i]);
972 } else if (!L.DomUtil.hasClass(el, name)) {
973 var className = L.DomUtil._getClass(el);
974 L.DomUtil._setClass(el, (className ? className + ' ' : '') + name);
978 removeClass: function (el, name) {
979 if (el.classList !== undefined) {
980 el.classList.remove(name);
982 L.DomUtil._setClass(el, L.Util.trim((' ' + L.DomUtil._getClass(el) + ' ').replace(' ' + name + ' ', ' ')));
986 _setClass: function (el, name) {
987 if (el.className.baseVal === undefined) {
990 // in case of SVG element
991 el.className.baseVal = name;
995 _getClass: function (el) {
996 return el.className.baseVal === undefined ? el.className : el.className.baseVal;
999 setOpacity: function (el, value) {
1001 if ('opacity' in el.style) {
1002 el.style.opacity = value;
1004 } else if ('filter' in el.style) {
1007 filterName = 'DXImageTransform.Microsoft.Alpha';
1009 // filters collection throws an error if we try to retrieve a filter that doesn't exist
1011 filter = el.filters.item(filterName);
1013 // don't set opacity to 1 if we haven't already set an opacity,
1014 // it isn't needed and breaks transparent pngs.
1015 if (value === 1) { return; }
1018 value = Math.round(value * 100);
1021 filter.Enabled = (value !== 100);
1022 filter.Opacity = value;
1024 el.style.filter += ' progid:' + filterName + '(opacity=' + value + ')';
1029 testProp: function (props) {
1031 var style = document.documentElement.style;
1033 for (var i = 0; i < props.length; i++) {
1034 if (props[i] in style) {
1041 getTranslateString: function (point) {
1042 // on WebKit browsers (Chrome/Safari/iOS Safari/Android) using translate3d instead of translate
1043 // makes animation smoother as it ensures HW accel is used. Firefox 13 doesn't care
1044 // (same speed either way), Opera 12 doesn't support translate3d
1046 var is3d = L.Browser.webkit3d,
1047 open = 'translate' + (is3d ? '3d' : '') + '(',
1048 close = (is3d ? ',0' : '') + ')';
1050 return open + point.x + 'px,' + point.y + 'px' + close;
1053 getScaleString: function (scale, origin) {
1055 var preTranslateStr = L.DomUtil.getTranslateString(origin.add(origin.multiplyBy(-1 * scale))),
1056 scaleStr = ' scale(' + scale + ') ';
1058 return preTranslateStr + scaleStr;
1061 setPosition: function (el, point, disable3D) { // (HTMLElement, Point[, Boolean])
1063 // jshint camelcase: false
1064 el._leaflet_pos = point;
1066 if (!disable3D && L.Browser.any3d) {
1067 el.style[L.DomUtil.TRANSFORM] = L.DomUtil.getTranslateString(point);
1069 el.style.left = point.x + 'px';
1070 el.style.top = point.y + 'px';
1074 getPosition: function (el) {
1075 // this method is only used for elements previously positioned using setPosition,
1076 // so it's safe to cache the position for performance
1078 // jshint camelcase: false
1079 return el._leaflet_pos;
1084 // prefix style property names
1086 L.DomUtil.TRANSFORM = L.DomUtil.testProp(
1087 ['transform', 'WebkitTransform', 'OTransform', 'MozTransform', 'msTransform']);
1089 // webkitTransition comes first because some browser versions that drop vendor prefix don't do
1090 // the same for the transitionend event, in particular the Android 4.1 stock browser
1092 L.DomUtil.TRANSITION = L.DomUtil.testProp(
1093 ['webkitTransition', 'transition', 'OTransition', 'MozTransition', 'msTransition']);
1095 L.DomUtil.TRANSITION_END =
1096 L.DomUtil.TRANSITION === 'webkitTransition' || L.DomUtil.TRANSITION === 'OTransition' ?
1097 L.DomUtil.TRANSITION + 'End' : 'transitionend';
1100 if ('onselectstart' in document) {
1101 L.extend(L.DomUtil, {
1102 disableTextSelection: function () {
1103 L.DomEvent.on(window, 'selectstart', L.DomEvent.preventDefault);
1106 enableTextSelection: function () {
1107 L.DomEvent.off(window, 'selectstart', L.DomEvent.preventDefault);
1111 var userSelectProperty = L.DomUtil.testProp(
1112 ['userSelect', 'WebkitUserSelect', 'OUserSelect', 'MozUserSelect', 'msUserSelect']);
1114 L.extend(L.DomUtil, {
1115 disableTextSelection: function () {
1116 if (userSelectProperty) {
1117 var style = document.documentElement.style;
1118 this._userSelect = style[userSelectProperty];
1119 style[userSelectProperty] = 'none';
1123 enableTextSelection: function () {
1124 if (userSelectProperty) {
1125 document.documentElement.style[userSelectProperty] = this._userSelect;
1126 delete this._userSelect;
1132 L.extend(L.DomUtil, {
1133 disableImageDrag: function () {
1134 L.DomEvent.on(window, 'dragstart', L.DomEvent.preventDefault);
1137 enableImageDrag: function () {
1138 L.DomEvent.off(window, 'dragstart', L.DomEvent.preventDefault);
1145 * L.LatLng represents a geographical point with latitude and longitude coordinates.
1148 L.LatLng = function (lat, lng, alt) { // (Number, Number, Number)
1149 lat = parseFloat(lat);
1150 lng = parseFloat(lng);
1152 if (isNaN(lat) || isNaN(lng)) {
1153 throw new Error('Invalid LatLng object: (' + lat + ', ' + lng + ')');
1159 if (alt !== undefined) {
1160 this.alt = parseFloat(alt);
1164 L.extend(L.LatLng, {
1165 DEG_TO_RAD: Math.PI / 180,
1166 RAD_TO_DEG: 180 / Math.PI,
1167 MAX_MARGIN: 1.0E-9 // max margin of error for the "equals" check
1170 L.LatLng.prototype = {
1171 equals: function (obj) { // (LatLng) -> Boolean
1172 if (!obj) { return false; }
1174 obj = L.latLng(obj);
1176 var margin = Math.max(
1177 Math.abs(this.lat - obj.lat),
1178 Math.abs(this.lng - obj.lng));
1180 return margin <= L.LatLng.MAX_MARGIN;
1183 toString: function (precision) { // (Number) -> String
1185 L.Util.formatNum(this.lat, precision) + ', ' +
1186 L.Util.formatNum(this.lng, precision) + ')';
1189 // Haversine distance formula, see http://en.wikipedia.org/wiki/Haversine_formula
1190 // TODO move to projection code, LatLng shouldn't know about Earth
1191 distanceTo: function (other) { // (LatLng) -> Number
1192 other = L.latLng(other);
1194 var R = 6378137, // earth radius in meters
1195 d2r = L.LatLng.DEG_TO_RAD,
1196 dLat = (other.lat - this.lat) * d2r,
1197 dLon = (other.lng - this.lng) * d2r,
1198 lat1 = this.lat * d2r,
1199 lat2 = other.lat * d2r,
1200 sin1 = Math.sin(dLat / 2),
1201 sin2 = Math.sin(dLon / 2);
1203 var a = sin1 * sin1 + sin2 * sin2 * Math.cos(lat1) * Math.cos(lat2);
1205 return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
1208 wrap: function (a, b) { // (Number, Number) -> LatLng
1214 lng = (lng + b) % (b - a) + (lng < a || lng === b ? b : a);
1216 return new L.LatLng(this.lat, lng);
1220 L.latLng = function (a, b) { // (LatLng) or ([Number, Number]) or (Number, Number)
1221 if (a instanceof L.LatLng) {
1224 if (L.Util.isArray(a)) {
1225 if (typeof a[0] === 'number' || typeof a[0] === 'string') {
1226 return new L.LatLng(a[0], a[1], a[2]);
1231 if (a === undefined || a === null) {
1234 if (typeof a === 'object' && 'lat' in a) {
1235 return new L.LatLng(a.lat, 'lng' in a ? a.lng : a.lon);
1237 if (b === undefined) {
1240 return new L.LatLng(a, b);
1246 * L.LatLngBounds represents a rectangular area on the map in geographical coordinates.
1249 L.LatLngBounds = function (southWest, northEast) { // (LatLng, LatLng) or (LatLng[])
1250 if (!southWest) { return; }
1252 var latlngs = northEast ? [southWest, northEast] : southWest;
1254 for (var i = 0, len = latlngs.length; i < len; i++) {
1255 this.extend(latlngs[i]);
1259 L.LatLngBounds.prototype = {
1260 // extend the bounds to contain the given point or bounds
1261 extend: function (obj) { // (LatLng) or (LatLngBounds)
1262 if (!obj) { return this; }
1264 var latLng = L.latLng(obj);
1265 if (latLng !== null) {
1268 obj = L.latLngBounds(obj);
1271 if (obj instanceof L.LatLng) {
1272 if (!this._southWest && !this._northEast) {
1273 this._southWest = new L.LatLng(obj.lat, obj.lng);
1274 this._northEast = new L.LatLng(obj.lat, obj.lng);
1276 this._southWest.lat = Math.min(obj.lat, this._southWest.lat);
1277 this._southWest.lng = Math.min(obj.lng, this._southWest.lng);
1279 this._northEast.lat = Math.max(obj.lat, this._northEast.lat);
1280 this._northEast.lng = Math.max(obj.lng, this._northEast.lng);
1282 } else if (obj instanceof L.LatLngBounds) {
1283 this.extend(obj._southWest);
1284 this.extend(obj._northEast);
1289 // extend the bounds by a percentage
1290 pad: function (bufferRatio) { // (Number) -> LatLngBounds
1291 var sw = this._southWest,
1292 ne = this._northEast,
1293 heightBuffer = Math.abs(sw.lat - ne.lat) * bufferRatio,
1294 widthBuffer = Math.abs(sw.lng - ne.lng) * bufferRatio;
1296 return new L.LatLngBounds(
1297 new L.LatLng(sw.lat - heightBuffer, sw.lng - widthBuffer),
1298 new L.LatLng(ne.lat + heightBuffer, ne.lng + widthBuffer));
1301 getCenter: function () { // -> LatLng
1302 return new L.LatLng(
1303 (this._southWest.lat + this._northEast.lat) / 2,
1304 (this._southWest.lng + this._northEast.lng) / 2);
1307 getSouthWest: function () {
1308 return this._southWest;
1311 getNorthEast: function () {
1312 return this._northEast;
1315 getNorthWest: function () {
1316 return new L.LatLng(this.getNorth(), this.getWest());
1319 getSouthEast: function () {
1320 return new L.LatLng(this.getSouth(), this.getEast());
1323 getWest: function () {
1324 return this._southWest.lng;
1327 getSouth: function () {
1328 return this._southWest.lat;
1331 getEast: function () {
1332 return this._northEast.lng;
1335 getNorth: function () {
1336 return this._northEast.lat;
1339 contains: function (obj) { // (LatLngBounds) or (LatLng) -> Boolean
1340 if (typeof obj[0] === 'number' || obj instanceof L.LatLng) {
1341 obj = L.latLng(obj);
1343 obj = L.latLngBounds(obj);
1346 var sw = this._southWest,
1347 ne = this._northEast,
1350 if (obj instanceof L.LatLngBounds) {
1351 sw2 = obj.getSouthWest();
1352 ne2 = obj.getNorthEast();
1357 return (sw2.lat >= sw.lat) && (ne2.lat <= ne.lat) &&
1358 (sw2.lng >= sw.lng) && (ne2.lng <= ne.lng);
1361 intersects: function (bounds) { // (LatLngBounds)
1362 bounds = L.latLngBounds(bounds);
1364 var sw = this._southWest,
1365 ne = this._northEast,
1366 sw2 = bounds.getSouthWest(),
1367 ne2 = bounds.getNorthEast(),
1369 latIntersects = (ne2.lat >= sw.lat) && (sw2.lat <= ne.lat),
1370 lngIntersects = (ne2.lng >= sw.lng) && (sw2.lng <= ne.lng);
1372 return latIntersects && lngIntersects;
1375 toBBoxString: function () {
1376 return [this.getWest(), this.getSouth(), this.getEast(), this.getNorth()].join(',');
1379 equals: function (bounds) { // (LatLngBounds)
1380 if (!bounds) { return false; }
1382 bounds = L.latLngBounds(bounds);
1384 return this._southWest.equals(bounds.getSouthWest()) &&
1385 this._northEast.equals(bounds.getNorthEast());
1388 isValid: function () {
1389 return !!(this._southWest && this._northEast);
1393 //TODO International date line?
1395 L.latLngBounds = function (a, b) { // (LatLngBounds) or (LatLng, LatLng)
1396 if (!a || a instanceof L.LatLngBounds) {
1399 return new L.LatLngBounds(a, b);
1404 * L.Projection contains various geographical projections used by CRS classes.
1411 * Spherical Mercator is the most popular map projection, used by EPSG:3857 CRS used by default.
1414 L.Projection.SphericalMercator = {
1415 MAX_LATITUDE: 85.0511287798,
1417 project: function (latlng) { // (LatLng) -> Point
1418 var d = L.LatLng.DEG_TO_RAD,
1419 max = this.MAX_LATITUDE,
1420 lat = Math.max(Math.min(max, latlng.lat), -max),
1424 y = Math.log(Math.tan((Math.PI / 4) + (y / 2)));
1426 return new L.Point(x, y);
1429 unproject: function (point) { // (Point, Boolean) -> LatLng
1430 var d = L.LatLng.RAD_TO_DEG,
1432 lat = (2 * Math.atan(Math.exp(point.y)) - (Math.PI / 2)) * d;
1434 return new L.LatLng(lat, lng);
1440 * Simple equirectangular (Plate Carree) projection, used by CRS like EPSG:4326 and Simple.
1443 L.Projection.LonLat = {
1444 project: function (latlng) {
1445 return new L.Point(latlng.lng, latlng.lat);
1448 unproject: function (point) {
1449 return new L.LatLng(point.y, point.x);
1455 * L.CRS is a base object for all defined CRS (Coordinate Reference Systems) in Leaflet.
1459 latLngToPoint: function (latlng, zoom) { // (LatLng, Number) -> Point
1460 var projectedPoint = this.projection.project(latlng),
1461 scale = this.scale(zoom);
1463 return this.transformation._transform(projectedPoint, scale);
1466 pointToLatLng: function (point, zoom) { // (Point, Number[, Boolean]) -> LatLng
1467 var scale = this.scale(zoom),
1468 untransformedPoint = this.transformation.untransform(point, scale);
1470 return this.projection.unproject(untransformedPoint);
1473 project: function (latlng) {
1474 return this.projection.project(latlng);
1477 scale: function (zoom) {
1478 return 256 * Math.pow(2, zoom);
1481 getSize: function (zoom) {
1482 var s = this.scale(zoom);
1483 return L.point(s, s);
1489 * A simple CRS that can be used for flat non-Earth maps like panoramas or game maps.
1492 L.CRS.Simple = L.extend({}, L.CRS, {
1493 projection: L.Projection.LonLat,
1494 transformation: new L.Transformation(1, 0, -1, 0),
1496 scale: function (zoom) {
1497 return Math.pow(2, zoom);
1503 * L.CRS.EPSG3857 (Spherical Mercator) is the most common CRS for web mapping
1504 * and is used by Leaflet by default.
1507 L.CRS.EPSG3857 = L.extend({}, L.CRS, {
1510 projection: L.Projection.SphericalMercator,
1511 transformation: new L.Transformation(0.5 / Math.PI, 0.5, -0.5 / Math.PI, 0.5),
1513 project: function (latlng) { // (LatLng) -> Point
1514 var projectedPoint = this.projection.project(latlng),
1515 earthRadius = 6378137;
1516 return projectedPoint.multiplyBy(earthRadius);
1520 L.CRS.EPSG900913 = L.extend({}, L.CRS.EPSG3857, {
1526 * L.CRS.EPSG4326 is a CRS popular among advanced GIS specialists.
1529 L.CRS.EPSG4326 = L.extend({}, L.CRS, {
1532 projection: L.Projection.LonLat,
1533 transformation: new L.Transformation(1 / 360, 0.5, -1 / 360, 0.5)
1538 * L.Map is the central class of the API - it is used to create a map.
1541 L.Map = L.Class.extend({
1543 includes: L.Mixin.Events,
1546 crs: L.CRS.EPSG3857,
1554 fadeAnimation: L.DomUtil.TRANSITION && !L.Browser.android23,
1556 markerZoomAnimation: L.DomUtil.TRANSITION && L.Browser.any3d
1559 initialize: function (id, options) { // (HTMLElement or String, Object)
1560 options = L.setOptions(this, options);
1563 this._initContainer(id);
1566 // hack for https://github.com/Leaflet/Leaflet/issues/1980
1567 this._onResize = L.bind(this._onResize, this);
1571 if (options.maxBounds) {
1572 this.setMaxBounds(options.maxBounds);
1575 if (options.center && options.zoom !== undefined) {
1576 this.setView(L.latLng(options.center), options.zoom, {reset: true});
1579 this._handlers = [];
1582 this._zoomBoundLayers = {};
1583 this._tileLayersNum = 0;
1585 this.callInitHooks();
1587 this._addLayers(options.layers);
1591 // public methods that modify map state
1593 // replaced by animation-powered implementation in Map.PanAnimation.js
1594 setView: function (center, zoom) {
1595 zoom = zoom === undefined ? this.getZoom() : zoom;
1596 this._resetView(L.latLng(center), this._limitZoom(zoom));
1600 setZoom: function (zoom, options) {
1601 if (!this._loaded) {
1602 this._zoom = this._limitZoom(zoom);
1605 return this.setView(this.getCenter(), zoom, {zoom: options});
1608 zoomIn: function (delta, options) {
1609 return this.setZoom(this._zoom + (delta || 1), options);
1612 zoomOut: function (delta, options) {
1613 return this.setZoom(this._zoom - (delta || 1), options);
1616 setZoomAround: function (latlng, zoom, options) {
1617 var scale = this.getZoomScale(zoom),
1618 viewHalf = this.getSize().divideBy(2),
1619 containerPoint = latlng instanceof L.Point ? latlng : this.latLngToContainerPoint(latlng),
1621 centerOffset = containerPoint.subtract(viewHalf).multiplyBy(1 - 1 / scale),
1622 newCenter = this.containerPointToLatLng(viewHalf.add(centerOffset));
1624 return this.setView(newCenter, zoom, {zoom: options});
1627 fitBounds: function (bounds, options) {
1629 options = options || {};
1630 bounds = bounds.getBounds ? bounds.getBounds() : L.latLngBounds(bounds);
1632 var paddingTL = L.point(options.paddingTopLeft || options.padding || [0, 0]),
1633 paddingBR = L.point(options.paddingBottomRight || options.padding || [0, 0]),
1635 zoom = this.getBoundsZoom(bounds, false, paddingTL.add(paddingBR)),
1636 paddingOffset = paddingBR.subtract(paddingTL).divideBy(2),
1638 swPoint = this.project(bounds.getSouthWest(), zoom),
1639 nePoint = this.project(bounds.getNorthEast(), zoom),
1640 center = this.unproject(swPoint.add(nePoint).divideBy(2).add(paddingOffset), zoom);
1642 zoom = options && options.maxZoom ? Math.min(options.maxZoom, zoom) : zoom;
1644 return this.setView(center, zoom, options);
1647 fitWorld: function (options) {
1648 return this.fitBounds([[-90, -180], [90, 180]], options);
1651 panTo: function (center, options) { // (LatLng)
1652 return this.setView(center, this._zoom, {pan: options});
1655 panBy: function (offset) { // (Point)
1656 // replaced with animated panBy in Map.PanAnimation.js
1657 this.fire('movestart');
1659 this._rawPanBy(L.point(offset));
1662 return this.fire('moveend');
1665 setMaxBounds: function (bounds) {
1666 bounds = L.latLngBounds(bounds);
1668 this.options.maxBounds = bounds;
1671 return this.off('moveend', this._panInsideMaxBounds, this);
1675 this._panInsideMaxBounds();
1678 return this.on('moveend', this._panInsideMaxBounds, this);
1681 panInsideBounds: function (bounds, options) {
1682 var center = this.getCenter(),
1683 newCenter = this._limitCenter(center, this._zoom, bounds);
1685 if (center.equals(newCenter)) { return this; }
1687 return this.panTo(newCenter, options);
1690 addLayer: function (layer) {
1691 // TODO method is too big, refactor
1693 var id = L.stamp(layer);
1695 if (this._layers[id]) { return this; }
1697 this._layers[id] = layer;
1699 // TODO getMaxZoom, getMinZoom in ILayer (instead of options)
1700 if (layer.options && (!isNaN(layer.options.maxZoom) || !isNaN(layer.options.minZoom))) {
1701 this._zoomBoundLayers[id] = layer;
1702 this._updateZoomLevels();
1705 // TODO looks ugly, refactor!!!
1706 if (this.options.zoomAnimation && L.TileLayer && (layer instanceof L.TileLayer)) {
1707 this._tileLayersNum++;
1708 this._tileLayersToLoad++;
1709 layer.on('load', this._onTileLayerLoad, this);
1713 this._layerAdd(layer);
1719 removeLayer: function (layer) {
1720 var id = L.stamp(layer);
1722 if (!this._layers[id]) { return this; }
1725 layer.onRemove(this);
1728 delete this._layers[id];
1731 this.fire('layerremove', {layer: layer});
1734 if (this._zoomBoundLayers[id]) {
1735 delete this._zoomBoundLayers[id];
1736 this._updateZoomLevels();
1739 // TODO looks ugly, refactor
1740 if (this.options.zoomAnimation && L.TileLayer && (layer instanceof L.TileLayer)) {
1741 this._tileLayersNum--;
1742 this._tileLayersToLoad--;
1743 layer.off('load', this._onTileLayerLoad, this);
1749 hasLayer: function (layer) {
1750 if (!layer) { return false; }
1752 return (L.stamp(layer) in this._layers);
1755 eachLayer: function (method, context) {
1756 for (var i in this._layers) {
1757 method.call(context, this._layers[i]);
1762 invalidateSize: function (options) {
1763 if (!this._loaded) { return this; }
1765 options = L.extend({
1768 }, options === true ? {animate: true} : options);
1770 var oldSize = this.getSize();
1771 this._sizeChanged = true;
1772 this._initialCenter = null;
1774 var newSize = this.getSize(),
1775 oldCenter = oldSize.divideBy(2).round(),
1776 newCenter = newSize.divideBy(2).round(),
1777 offset = oldCenter.subtract(newCenter);
1779 if (!offset.x && !offset.y) { return this; }
1781 if (options.animate && options.pan) {
1786 this._rawPanBy(offset);
1791 if (options.debounceMoveend) {
1792 clearTimeout(this._sizeTimer);
1793 this._sizeTimer = setTimeout(L.bind(this.fire, this, 'moveend'), 200);
1795 this.fire('moveend');
1799 return this.fire('resize', {
1805 // TODO handler.addTo
1806 addHandler: function (name, HandlerClass) {
1807 if (!HandlerClass) { return this; }
1809 var handler = this[name] = new HandlerClass(this);
1811 this._handlers.push(handler);
1813 if (this.options[name]) {
1820 remove: function () {
1822 this.fire('unload');
1825 this._initEvents('off');
1828 // throws error in IE6-8
1829 delete this._container._leaflet;
1831 this._container._leaflet = undefined;
1835 if (this._clearControlPos) {
1836 this._clearControlPos();
1839 this._clearHandlers();
1845 // public methods for getting map state
1847 getCenter: function () { // (Boolean) -> LatLng
1848 this._checkIfLoaded();
1850 if (this._initialCenter && !this._moved()) {
1851 return this._initialCenter;
1853 return this.layerPointToLatLng(this._getCenterLayerPoint());
1856 getZoom: function () {
1860 getBounds: function () {
1861 var bounds = this.getPixelBounds(),
1862 sw = this.unproject(bounds.getBottomLeft()),
1863 ne = this.unproject(bounds.getTopRight());
1865 return new L.LatLngBounds(sw, ne);
1868 getMinZoom: function () {
1869 return this.options.minZoom === undefined ?
1870 (this._layersMinZoom === undefined ? 0 : this._layersMinZoom) :
1871 this.options.minZoom;
1874 getMaxZoom: function () {
1875 return this.options.maxZoom === undefined ?
1876 (this._layersMaxZoom === undefined ? Infinity : this._layersMaxZoom) :
1877 this.options.maxZoom;
1880 getBoundsZoom: function (bounds, inside, padding) { // (LatLngBounds[, Boolean, Point]) -> Number
1881 bounds = L.latLngBounds(bounds);
1883 var zoom = this.getMinZoom() - (inside ? 1 : 0),
1884 maxZoom = this.getMaxZoom(),
1885 size = this.getSize(),
1887 nw = bounds.getNorthWest(),
1888 se = bounds.getSouthEast(),
1890 zoomNotFound = true,
1893 padding = L.point(padding || [0, 0]);
1897 boundsSize = this.project(se, zoom).subtract(this.project(nw, zoom)).add(padding);
1898 zoomNotFound = !inside ? size.contains(boundsSize) : boundsSize.x < size.x || boundsSize.y < size.y;
1900 } while (zoomNotFound && zoom <= maxZoom);
1902 if (zoomNotFound && inside) {
1906 return inside ? zoom : zoom - 1;
1909 getSize: function () {
1910 if (!this._size || this._sizeChanged) {
1911 this._size = new L.Point(
1912 this._container.clientWidth,
1913 this._container.clientHeight);
1915 this._sizeChanged = false;
1917 return this._size.clone();
1920 getPixelBounds: function () {
1921 var topLeftPoint = this._getTopLeftPoint();
1922 return new L.Bounds(topLeftPoint, topLeftPoint.add(this.getSize()));
1925 getPixelOrigin: function () {
1926 this._checkIfLoaded();
1927 return this._initialTopLeftPoint;
1930 getPanes: function () {
1934 getContainer: function () {
1935 return this._container;
1939 // TODO replace with universal implementation after refactoring projections
1941 getZoomScale: function (toZoom) {
1942 var crs = this.options.crs;
1943 return crs.scale(toZoom) / crs.scale(this._zoom);
1946 getScaleZoom: function (scale) {
1947 return this._zoom + (Math.log(scale) / Math.LN2);
1951 // conversion methods
1953 project: function (latlng, zoom) { // (LatLng[, Number]) -> Point
1954 zoom = zoom === undefined ? this._zoom : zoom;
1955 return this.options.crs.latLngToPoint(L.latLng(latlng), zoom);
1958 unproject: function (point, zoom) { // (Point[, Number]) -> LatLng
1959 zoom = zoom === undefined ? this._zoom : zoom;
1960 return this.options.crs.pointToLatLng(L.point(point), zoom);
1963 layerPointToLatLng: function (point) { // (Point)
1964 var projectedPoint = L.point(point).add(this.getPixelOrigin());
1965 return this.unproject(projectedPoint);
1968 latLngToLayerPoint: function (latlng) { // (LatLng)
1969 var projectedPoint = this.project(L.latLng(latlng))._round();
1970 return projectedPoint._subtract(this.getPixelOrigin());
1973 containerPointToLayerPoint: function (point) { // (Point)
1974 return L.point(point).subtract(this._getMapPanePos());
1977 layerPointToContainerPoint: function (point) { // (Point)
1978 return L.point(point).add(this._getMapPanePos());
1981 containerPointToLatLng: function (point) {
1982 var layerPoint = this.containerPointToLayerPoint(L.point(point));
1983 return this.layerPointToLatLng(layerPoint);
1986 latLngToContainerPoint: function (latlng) {
1987 return this.layerPointToContainerPoint(this.latLngToLayerPoint(L.latLng(latlng)));
1990 mouseEventToContainerPoint: function (e) { // (MouseEvent)
1991 return L.DomEvent.getMousePosition(e, this._container);
1994 mouseEventToLayerPoint: function (e) { // (MouseEvent)
1995 return this.containerPointToLayerPoint(this.mouseEventToContainerPoint(e));
1998 mouseEventToLatLng: function (e) { // (MouseEvent)
1999 return this.layerPointToLatLng(this.mouseEventToLayerPoint(e));
2003 // map initialization methods
2005 _initContainer: function (id) {
2006 var container = this._container = L.DomUtil.get(id);
2009 throw new Error('Map container not found.');
2010 } else if (container._leaflet) {
2011 throw new Error('Map container is already initialized.');
2014 container._leaflet = true;
2017 _initLayout: function () {
2018 var container = this._container;
2020 L.DomUtil.addClass(container, 'leaflet-container' +
2021 (L.Browser.touch ? ' leaflet-touch' : '') +
2022 (L.Browser.retina ? ' leaflet-retina' : '') +
2023 (L.Browser.ielt9 ? ' leaflet-oldie' : '') +
2024 (this.options.fadeAnimation ? ' leaflet-fade-anim' : ''));
2026 var position = L.DomUtil.getStyle(container, 'position');
2028 if (position !== 'absolute' && position !== 'relative' && position !== 'fixed') {
2029 container.style.position = 'relative';
2034 if (this._initControlPos) {
2035 this._initControlPos();
2039 _initPanes: function () {
2040 var panes = this._panes = {};
2042 this._mapPane = panes.mapPane = this._createPane('leaflet-map-pane', this._container);
2044 this._tilePane = panes.tilePane = this._createPane('leaflet-tile-pane', this._mapPane);
2045 panes.objectsPane = this._createPane('leaflet-objects-pane', this._mapPane);
2046 panes.shadowPane = this._createPane('leaflet-shadow-pane');
2047 panes.overlayPane = this._createPane('leaflet-overlay-pane');
2048 panes.markerPane = this._createPane('leaflet-marker-pane');
2049 panes.popupPane = this._createPane('leaflet-popup-pane');
2051 var zoomHide = ' leaflet-zoom-hide';
2053 if (!this.options.markerZoomAnimation) {
2054 L.DomUtil.addClass(panes.markerPane, zoomHide);
2055 L.DomUtil.addClass(panes.shadowPane, zoomHide);
2056 L.DomUtil.addClass(panes.popupPane, zoomHide);
2060 _createPane: function (className, container) {
2061 return L.DomUtil.create('div', className, container || this._panes.objectsPane);
2064 _clearPanes: function () {
2065 this._container.removeChild(this._mapPane);
2068 _addLayers: function (layers) {
2069 layers = layers ? (L.Util.isArray(layers) ? layers : [layers]) : [];
2071 for (var i = 0, len = layers.length; i < len; i++) {
2072 this.addLayer(layers[i]);
2077 // private methods that modify map state
2079 _resetView: function (center, zoom, preserveMapOffset, afterZoomAnim) {
2081 var zoomChanged = (this._zoom !== zoom);
2083 if (!afterZoomAnim) {
2084 this.fire('movestart');
2087 this.fire('zoomstart');
2092 this._initialCenter = center;
2094 this._initialTopLeftPoint = this._getNewTopLeftPoint(center);
2096 if (!preserveMapOffset) {
2097 L.DomUtil.setPosition(this._mapPane, new L.Point(0, 0));
2099 this._initialTopLeftPoint._add(this._getMapPanePos());
2102 this._tileLayersToLoad = this._tileLayersNum;
2104 var loading = !this._loaded;
2105 this._loaded = true;
2107 this.fire('viewreset', {hard: !preserveMapOffset});
2111 this.eachLayer(this._layerAdd, this);
2116 if (zoomChanged || afterZoomAnim) {
2117 this.fire('zoomend');
2120 this.fire('moveend', {hard: !preserveMapOffset});
2123 _rawPanBy: function (offset) {
2124 L.DomUtil.setPosition(this._mapPane, this._getMapPanePos().subtract(offset));
2127 _getZoomSpan: function () {
2128 return this.getMaxZoom() - this.getMinZoom();
2131 _updateZoomLevels: function () {
2134 maxZoom = -Infinity,
2135 oldZoomSpan = this._getZoomSpan();
2137 for (i in this._zoomBoundLayers) {
2138 var layer = this._zoomBoundLayers[i];
2139 if (!isNaN(layer.options.minZoom)) {
2140 minZoom = Math.min(minZoom, layer.options.minZoom);
2142 if (!isNaN(layer.options.maxZoom)) {
2143 maxZoom = Math.max(maxZoom, layer.options.maxZoom);
2147 if (i === undefined) { // we have no tilelayers
2148 this._layersMaxZoom = this._layersMinZoom = undefined;
2150 this._layersMaxZoom = maxZoom;
2151 this._layersMinZoom = minZoom;
2154 if (oldZoomSpan !== this._getZoomSpan()) {
2155 this.fire('zoomlevelschange');
2159 _panInsideMaxBounds: function () {
2160 this.panInsideBounds(this.options.maxBounds);
2163 _checkIfLoaded: function () {
2164 if (!this._loaded) {
2165 throw new Error('Set map center and zoom first.');
2171 _initEvents: function (onOff) {
2172 if (!L.DomEvent) { return; }
2174 onOff = onOff || 'on';
2176 L.DomEvent[onOff](this._container, 'click', this._onMouseClick, this);
2178 var events = ['dblclick', 'mousedown', 'mouseup', 'mouseenter',
2179 'mouseleave', 'mousemove', 'contextmenu'],
2182 for (i = 0, len = events.length; i < len; i++) {
2183 L.DomEvent[onOff](this._container, events[i], this._fireMouseEvent, this);
2186 if (this.options.trackResize) {
2187 L.DomEvent[onOff](window, 'resize', this._onResize, this);
2191 _onResize: function () {
2192 L.Util.cancelAnimFrame(this._resizeRequest);
2193 this._resizeRequest = L.Util.requestAnimFrame(
2194 function () { this.invalidateSize({debounceMoveend: true}); }, this, false, this._container);
2197 _onMouseClick: function (e) {
2198 if (!this._loaded || (!e._simulated &&
2199 ((this.dragging && this.dragging.moved()) ||
2200 (this.boxZoom && this.boxZoom.moved()))) ||
2201 L.DomEvent._skipped(e)) { return; }
2203 this.fire('preclick');
2204 this._fireMouseEvent(e);
2207 _fireMouseEvent: function (e) {
2208 if (!this._loaded || L.DomEvent._skipped(e)) { return; }
2212 type = (type === 'mouseenter' ? 'mouseover' : (type === 'mouseleave' ? 'mouseout' : type));
2214 if (!this.hasEventListeners(type)) { return; }
2216 if (type === 'contextmenu') {
2217 L.DomEvent.preventDefault(e);
2220 var containerPoint = this.mouseEventToContainerPoint(e),
2221 layerPoint = this.containerPointToLayerPoint(containerPoint),
2222 latlng = this.layerPointToLatLng(layerPoint);
2226 layerPoint: layerPoint,
2227 containerPoint: containerPoint,
2232 _onTileLayerLoad: function () {
2233 this._tileLayersToLoad--;
2234 if (this._tileLayersNum && !this._tileLayersToLoad) {
2235 this.fire('tilelayersload');
2239 _clearHandlers: function () {
2240 for (var i = 0, len = this._handlers.length; i < len; i++) {
2241 this._handlers[i].disable();
2245 whenReady: function (callback, context) {
2247 callback.call(context || this, this);
2249 this.on('load', callback, context);
2254 _layerAdd: function (layer) {
2256 this.fire('layeradd', {layer: layer});
2260 // private methods for getting map state
2262 _getMapPanePos: function () {
2263 return L.DomUtil.getPosition(this._mapPane);
2266 _moved: function () {
2267 var pos = this._getMapPanePos();
2268 return pos && !pos.equals([0, 0]);
2271 _getTopLeftPoint: function () {
2272 return this.getPixelOrigin().subtract(this._getMapPanePos());
2275 _getNewTopLeftPoint: function (center, zoom) {
2276 var viewHalf = this.getSize()._divideBy(2);
2277 // TODO round on display, not calculation to increase precision?
2278 return this.project(center, zoom)._subtract(viewHalf)._round();
2281 _latLngToNewLayerPoint: function (latlng, newZoom, newCenter) {
2282 var topLeft = this._getNewTopLeftPoint(newCenter, newZoom).add(this._getMapPanePos());
2283 return this.project(latlng, newZoom)._subtract(topLeft);
2286 // layer point of the current center
2287 _getCenterLayerPoint: function () {
2288 return this.containerPointToLayerPoint(this.getSize()._divideBy(2));
2291 // offset of the specified place to the current center in pixels
2292 _getCenterOffset: function (latlng) {
2293 return this.latLngToLayerPoint(latlng).subtract(this._getCenterLayerPoint());
2296 // adjust center for view to get inside bounds
2297 _limitCenter: function (center, zoom, bounds) {
2299 if (!bounds) { return center; }
2301 var centerPoint = this.project(center, zoom),
2302 viewHalf = this.getSize().divideBy(2),
2303 viewBounds = new L.Bounds(centerPoint.subtract(viewHalf), centerPoint.add(viewHalf)),
2304 offset = this._getBoundsOffset(viewBounds, bounds, zoom);
2306 return this.unproject(centerPoint.add(offset), zoom);
2309 // adjust offset for view to get inside bounds
2310 _limitOffset: function (offset, bounds) {
2311 if (!bounds) { return offset; }
2313 var viewBounds = this.getPixelBounds(),
2314 newBounds = new L.Bounds(viewBounds.min.add(offset), viewBounds.max.add(offset));
2316 return offset.add(this._getBoundsOffset(newBounds, bounds));
2319 // returns offset needed for pxBounds to get inside maxBounds at a specified zoom
2320 _getBoundsOffset: function (pxBounds, maxBounds, zoom) {
2321 var nwOffset = this.project(maxBounds.getNorthWest(), zoom).subtract(pxBounds.min),
2322 seOffset = this.project(maxBounds.getSouthEast(), zoom).subtract(pxBounds.max),
2324 dx = this._rebound(nwOffset.x, -seOffset.x),
2325 dy = this._rebound(nwOffset.y, -seOffset.y);
2327 return new L.Point(dx, dy);
2330 _rebound: function (left, right) {
2331 return left + right > 0 ?
2332 Math.round(left - right) / 2 :
2333 Math.max(0, Math.ceil(left)) - Math.max(0, Math.floor(right));
2336 _limitZoom: function (zoom) {
2337 var min = this.getMinZoom(),
2338 max = this.getMaxZoom();
2340 return Math.max(min, Math.min(max, zoom));
2344 L.map = function (id, options) {
2345 return new L.Map(id, options);
2350 * Mercator projection that takes into account that the Earth is not a perfect sphere.
2351 * Less popular than spherical mercator; used by projections like EPSG:3395.
2354 L.Projection.Mercator = {
2355 MAX_LATITUDE: 85.0840591556,
2357 R_MINOR: 6356752.314245179,
2360 project: function (latlng) { // (LatLng) -> Point
2361 var d = L.LatLng.DEG_TO_RAD,
2362 max = this.MAX_LATITUDE,
2363 lat = Math.max(Math.min(max, latlng.lat), -max),
2366 x = latlng.lng * d * r,
2369 eccent = Math.sqrt(1.0 - tmp * tmp),
2370 con = eccent * Math.sin(y);
2372 con = Math.pow((1 - con) / (1 + con), eccent * 0.5);
2374 var ts = Math.tan(0.5 * ((Math.PI * 0.5) - y)) / con;
2375 y = -r * Math.log(ts);
2377 return new L.Point(x, y);
2380 unproject: function (point) { // (Point, Boolean) -> LatLng
2381 var d = L.LatLng.RAD_TO_DEG,
2384 lng = point.x * d / r,
2386 eccent = Math.sqrt(1 - (tmp * tmp)),
2387 ts = Math.exp(- point.y / r),
2388 phi = (Math.PI / 2) - 2 * Math.atan(ts),
2395 while ((Math.abs(dphi) > tol) && (--i > 0)) {
2396 con = eccent * Math.sin(phi);
2397 dphi = (Math.PI / 2) - 2 * Math.atan(ts *
2398 Math.pow((1.0 - con) / (1.0 + con), 0.5 * eccent)) - phi;
2402 return new L.LatLng(phi * d, lng);
2408 L.CRS.EPSG3395 = L.extend({}, L.CRS, {
2411 projection: L.Projection.Mercator,
2413 transformation: (function () {
2414 var m = L.Projection.Mercator,
2416 scale = 0.5 / (Math.PI * r);
2418 return new L.Transformation(scale, 0.5, -scale, 0.5);
2424 * L.TileLayer is used for standard xyz-numbered tile layers.
2427 L.TileLayer = L.Class.extend({
2428 includes: L.Mixin.Events,
2440 maxNativeZoom: null,
2443 continuousWorld: false,
2446 detectRetina: false,
2450 unloadInvisibleTiles: L.Browser.mobile,
2451 updateWhenIdle: L.Browser.mobile
2454 initialize: function (url, options) {
2455 options = L.setOptions(this, options);
2457 // detecting retina displays, adjusting tileSize and zoom levels
2458 if (options.detectRetina && L.Browser.retina && options.maxZoom > 0) {
2460 options.tileSize = Math.floor(options.tileSize / 2);
2461 options.zoomOffset++;
2463 if (options.minZoom > 0) {
2466 this.options.maxZoom--;
2469 if (options.bounds) {
2470 options.bounds = L.latLngBounds(options.bounds);
2475 var subdomains = this.options.subdomains;
2477 if (typeof subdomains === 'string') {
2478 this.options.subdomains = subdomains.split('');
2482 onAdd: function (map) {
2484 this._animated = map._zoomAnimated;
2486 // create a container div for tiles
2487 this._initContainer();
2491 'viewreset': this._reset,
2492 'moveend': this._update
2495 if (this._animated) {
2497 'zoomanim': this._animateZoom,
2498 'zoomend': this._endZoomAnim
2502 if (!this.options.updateWhenIdle) {
2503 this._limitedUpdate = L.Util.limitExecByInterval(this._update, 150, this);
2504 map.on('move', this._limitedUpdate, this);
2511 addTo: function (map) {
2516 onRemove: function (map) {
2517 this._container.parentNode.removeChild(this._container);
2520 'viewreset': this._reset,
2521 'moveend': this._update
2524 if (this._animated) {
2526 'zoomanim': this._animateZoom,
2527 'zoomend': this._endZoomAnim
2531 if (!this.options.updateWhenIdle) {
2532 map.off('move', this._limitedUpdate, this);
2535 this._container = null;
2539 bringToFront: function () {
2540 var pane = this._map._panes.tilePane;
2542 if (this._container) {
2543 pane.appendChild(this._container);
2544 this._setAutoZIndex(pane, Math.max);
2550 bringToBack: function () {
2551 var pane = this._map._panes.tilePane;
2553 if (this._container) {
2554 pane.insertBefore(this._container, pane.firstChild);
2555 this._setAutoZIndex(pane, Math.min);
2561 getAttribution: function () {
2562 return this.options.attribution;
2565 getContainer: function () {
2566 return this._container;
2569 setOpacity: function (opacity) {
2570 this.options.opacity = opacity;
2573 this._updateOpacity();
2579 setZIndex: function (zIndex) {
2580 this.options.zIndex = zIndex;
2581 this._updateZIndex();
2586 setUrl: function (url, noRedraw) {
2596 redraw: function () {
2598 this._reset({hard: true});
2604 _updateZIndex: function () {
2605 if (this._container && this.options.zIndex !== undefined) {
2606 this._container.style.zIndex = this.options.zIndex;
2610 _setAutoZIndex: function (pane, compare) {
2612 var layers = pane.children,
2613 edgeZIndex = -compare(Infinity, -Infinity), // -Infinity for max, Infinity for min
2616 for (i = 0, len = layers.length; i < len; i++) {
2618 if (layers[i] !== this._container) {
2619 zIndex = parseInt(layers[i].style.zIndex, 10);
2621 if (!isNaN(zIndex)) {
2622 edgeZIndex = compare(edgeZIndex, zIndex);
2627 this.options.zIndex = this._container.style.zIndex =
2628 (isFinite(edgeZIndex) ? edgeZIndex : 0) + compare(1, -1);
2631 _updateOpacity: function () {
2633 tiles = this._tiles;
2635 if (L.Browser.ielt9) {
2637 L.DomUtil.setOpacity(tiles[i], this.options.opacity);
2640 L.DomUtil.setOpacity(this._container, this.options.opacity);
2644 _initContainer: function () {
2645 var tilePane = this._map._panes.tilePane;
2647 if (!this._container) {
2648 this._container = L.DomUtil.create('div', 'leaflet-layer');
2650 this._updateZIndex();
2652 if (this._animated) {
2653 var className = 'leaflet-tile-container';
2655 this._bgBuffer = L.DomUtil.create('div', className, this._container);
2656 this._tileContainer = L.DomUtil.create('div', className, this._container);
2659 this._tileContainer = this._container;
2662 tilePane.appendChild(this._container);
2664 if (this.options.opacity < 1) {
2665 this._updateOpacity();
2670 _reset: function (e) {
2671 for (var key in this._tiles) {
2672 this.fire('tileunload', {tile: this._tiles[key]});
2676 this._tilesToLoad = 0;
2678 if (this.options.reuseTiles) {
2679 this._unusedTiles = [];
2682 this._tileContainer.innerHTML = '';
2684 if (this._animated && e && e.hard) {
2685 this._clearBgBuffer();
2688 this._initContainer();
2691 _getTileSize: function () {
2692 var map = this._map,
2693 zoom = map.getZoom() + this.options.zoomOffset,
2694 zoomN = this.options.maxNativeZoom,
2695 tileSize = this.options.tileSize;
2697 if (zoomN && zoom > zoomN) {
2698 tileSize = Math.round(map.getZoomScale(zoom) / map.getZoomScale(zoomN) * tileSize);
2704 _update: function () {
2706 if (!this._map) { return; }
2708 var map = this._map,
2709 bounds = map.getPixelBounds(),
2710 zoom = map.getZoom(),
2711 tileSize = this._getTileSize();
2713 if (zoom > this.options.maxZoom || zoom < this.options.minZoom) {
2717 var tileBounds = L.bounds(
2718 bounds.min.divideBy(tileSize)._floor(),
2719 bounds.max.divideBy(tileSize)._floor());
2721 this._addTilesFromCenterOut(tileBounds);
2723 if (this.options.unloadInvisibleTiles || this.options.reuseTiles) {
2724 this._removeOtherTiles(tileBounds);
2728 _addTilesFromCenterOut: function (bounds) {
2730 center = bounds.getCenter();
2734 for (j = bounds.min.y; j <= bounds.max.y; j++) {
2735 for (i = bounds.min.x; i <= bounds.max.x; i++) {
2736 point = new L.Point(i, j);
2738 if (this._tileShouldBeLoaded(point)) {
2744 var tilesToLoad = queue.length;
2746 if (tilesToLoad === 0) { return; }
2748 // load tiles in order of their distance to center
2749 queue.sort(function (a, b) {
2750 return a.distanceTo(center) - b.distanceTo(center);
2753 var fragment = document.createDocumentFragment();
2755 // if its the first batch of tiles to load
2756 if (!this._tilesToLoad) {
2757 this.fire('loading');
2760 this._tilesToLoad += tilesToLoad;
2762 for (i = 0; i < tilesToLoad; i++) {
2763 this._addTile(queue[i], fragment);
2766 this._tileContainer.appendChild(fragment);
2769 _tileShouldBeLoaded: function (tilePoint) {
2770 if ((tilePoint.x + ':' + tilePoint.y) in this._tiles) {
2771 return false; // already loaded
2774 var options = this.options;
2776 if (!options.continuousWorld) {
2777 var limit = this._getWrapTileNum();
2779 // don't load if exceeds world bounds
2780 if ((options.noWrap && (tilePoint.x < 0 || tilePoint.x >= limit.x)) ||
2781 tilePoint.y < 0 || tilePoint.y >= limit.y) { return false; }
2784 if (options.bounds) {
2785 var tileSize = options.tileSize,
2786 nwPoint = tilePoint.multiplyBy(tileSize),
2787 sePoint = nwPoint.add([tileSize, tileSize]),
2788 nw = this._map.unproject(nwPoint),
2789 se = this._map.unproject(sePoint);
2791 // TODO temporary hack, will be removed after refactoring projections
2792 // https://github.com/Leaflet/Leaflet/issues/1618
2793 if (!options.continuousWorld && !options.noWrap) {
2798 if (!options.bounds.intersects([nw, se])) { return false; }
2804 _removeOtherTiles: function (bounds) {
2805 var kArr, x, y, key;
2807 for (key in this._tiles) {
2808 kArr = key.split(':');
2809 x = parseInt(kArr[0], 10);
2810 y = parseInt(kArr[1], 10);
2812 // remove tile if it's out of bounds
2813 if (x < bounds.min.x || x > bounds.max.x || y < bounds.min.y || y > bounds.max.y) {
2814 this._removeTile(key);
2819 _removeTile: function (key) {
2820 var tile = this._tiles[key];
2822 this.fire('tileunload', {tile: tile, url: tile.src});
2824 if (this.options.reuseTiles) {
2825 L.DomUtil.removeClass(tile, 'leaflet-tile-loaded');
2826 this._unusedTiles.push(tile);
2828 } else if (tile.parentNode === this._tileContainer) {
2829 this._tileContainer.removeChild(tile);
2832 // for https://github.com/CloudMade/Leaflet/issues/137
2833 if (!L.Browser.android) {
2835 tile.src = L.Util.emptyImageUrl;
2838 delete this._tiles[key];
2841 _addTile: function (tilePoint, container) {
2842 var tilePos = this._getTilePos(tilePoint);
2844 // get unused tile - or create a new tile
2845 var tile = this._getTile();
2848 Chrome 20 layouts much faster with top/left (verify with timeline, frames)
2849 Android 4 browser has display issues with top/left and requires transform instead
2850 (other browsers don't currently care) - see debug/hacks/jitter.html for an example
2852 L.DomUtil.setPosition(tile, tilePos, L.Browser.chrome);
2854 this._tiles[tilePoint.x + ':' + tilePoint.y] = tile;
2856 this._loadTile(tile, tilePoint);
2858 if (tile.parentNode !== this._tileContainer) {
2859 container.appendChild(tile);
2863 _getZoomForUrl: function () {
2865 var options = this.options,
2866 zoom = this._map.getZoom();
2868 if (options.zoomReverse) {
2869 zoom = options.maxZoom - zoom;
2872 zoom += options.zoomOffset;
2874 return options.maxNativeZoom ? Math.min(zoom, options.maxNativeZoom) : zoom;
2877 _getTilePos: function (tilePoint) {
2878 var origin = this._map.getPixelOrigin(),
2879 tileSize = this._getTileSize();
2881 return tilePoint.multiplyBy(tileSize).subtract(origin);
2884 // image-specific code (override to implement e.g. Canvas or SVG tile layer)
2886 getTileUrl: function (tilePoint) {
2887 return L.Util.template(this._url, L.extend({
2888 s: this._getSubdomain(tilePoint),
2895 _getWrapTileNum: function () {
2896 var crs = this._map.options.crs,
2897 size = crs.getSize(this._map.getZoom());
2898 return size.divideBy(this._getTileSize())._floor();
2901 _adjustTilePoint: function (tilePoint) {
2903 var limit = this._getWrapTileNum();
2905 // wrap tile coordinates
2906 if (!this.options.continuousWorld && !this.options.noWrap) {
2907 tilePoint.x = ((tilePoint.x % limit.x) + limit.x) % limit.x;
2910 if (this.options.tms) {
2911 tilePoint.y = limit.y - tilePoint.y - 1;
2914 tilePoint.z = this._getZoomForUrl();
2917 _getSubdomain: function (tilePoint) {
2918 var index = Math.abs(tilePoint.x + tilePoint.y) % this.options.subdomains.length;
2919 return this.options.subdomains[index];
2922 _getTile: function () {
2923 if (this.options.reuseTiles && this._unusedTiles.length > 0) {
2924 var tile = this._unusedTiles.pop();
2925 this._resetTile(tile);
2928 return this._createTile();
2931 // Override if data stored on a tile needs to be cleaned up before reuse
2932 _resetTile: function (/*tile*/) {},
2934 _createTile: function () {
2935 var tile = L.DomUtil.create('img', 'leaflet-tile');
2936 tile.style.width = tile.style.height = this._getTileSize() + 'px';
2937 tile.galleryimg = 'no';
2939 tile.onselectstart = tile.onmousemove = L.Util.falseFn;
2941 if (L.Browser.ielt9 && this.options.opacity !== undefined) {
2942 L.DomUtil.setOpacity(tile, this.options.opacity);
2944 // without this hack, tiles disappear after zoom on Chrome for Android
2945 // https://github.com/Leaflet/Leaflet/issues/2078
2946 if (L.Browser.mobileWebkit3d) {
2947 tile.style.WebkitBackfaceVisibility = 'hidden';
2952 _loadTile: function (tile, tilePoint) {
2954 tile.onload = this._tileOnLoad;
2955 tile.onerror = this._tileOnError;
2957 this._adjustTilePoint(tilePoint);
2958 tile.src = this.getTileUrl(tilePoint);
2960 this.fire('tileloadstart', {
2966 _tileLoaded: function () {
2967 this._tilesToLoad--;
2969 if (this._animated) {
2970 L.DomUtil.addClass(this._tileContainer, 'leaflet-zoom-animated');
2973 if (!this._tilesToLoad) {
2976 if (this._animated) {
2977 // clear scaled tiles after all new tiles are loaded (for performance)
2978 clearTimeout(this._clearBgBufferTimer);
2979 this._clearBgBufferTimer = setTimeout(L.bind(this._clearBgBuffer, this), 500);
2984 _tileOnLoad: function () {
2985 var layer = this._layer;
2987 //Only if we are loading an actual image
2988 if (this.src !== L.Util.emptyImageUrl) {
2989 L.DomUtil.addClass(this, 'leaflet-tile-loaded');
2991 layer.fire('tileload', {
2997 layer._tileLoaded();
3000 _tileOnError: function () {
3001 var layer = this._layer;
3003 layer.fire('tileerror', {
3008 var newUrl = layer.options.errorTileUrl;
3013 layer._tileLoaded();
3017 L.tileLayer = function (url, options) {
3018 return new L.TileLayer(url, options);
3023 * L.TileLayer.WMS is used for putting WMS tile layers on the map.
3026 L.TileLayer.WMS = L.TileLayer.extend({
3034 format: 'image/jpeg',
3038 initialize: function (url, options) { // (String, Object)
3042 var wmsParams = L.extend({}, this.defaultWmsParams),
3043 tileSize = options.tileSize || this.options.tileSize;
3045 if (options.detectRetina && L.Browser.retina) {
3046 wmsParams.width = wmsParams.height = tileSize * 2;
3048 wmsParams.width = wmsParams.height = tileSize;
3051 for (var i in options) {
3052 // all keys that are not TileLayer options go to WMS params
3053 if (!this.options.hasOwnProperty(i) && i !== 'crs') {
3054 wmsParams[i] = options[i];
3058 this.wmsParams = wmsParams;
3060 L.setOptions(this, options);
3063 onAdd: function (map) {
3065 this._crs = this.options.crs || map.options.crs;
3067 this._wmsVersion = parseFloat(this.wmsParams.version);
3069 var projectionKey = this._wmsVersion >= 1.3 ? 'crs' : 'srs';
3070 this.wmsParams[projectionKey] = this._crs.code;
3072 L.TileLayer.prototype.onAdd.call(this, map);
3075 getTileUrl: function (tilePoint) { // (Point, Number) -> String
3077 var map = this._map,
3078 tileSize = this.options.tileSize,
3080 nwPoint = tilePoint.multiplyBy(tileSize),
3081 sePoint = nwPoint.add([tileSize, tileSize]),
3083 nw = this._crs.project(map.unproject(nwPoint, tilePoint.z)),
3084 se = this._crs.project(map.unproject(sePoint, tilePoint.z)),
3085 bbox = this._wmsVersion >= 1.3 && this._crs === L.CRS.EPSG4326 ?
3086 [se.y, nw.x, nw.y, se.x].join(',') :
3087 [nw.x, se.y, se.x, nw.y].join(','),
3089 url = L.Util.template(this._url, {s: this._getSubdomain(tilePoint)});
3091 return url + L.Util.getParamString(this.wmsParams, url, true) + '&BBOX=' + bbox;
3094 setParams: function (params, noRedraw) {
3096 L.extend(this.wmsParams, params);
3106 L.tileLayer.wms = function (url, options) {
3107 return new L.TileLayer.WMS(url, options);
3112 * L.TileLayer.Canvas is a class that you can use as a base for creating
3113 * dynamically drawn Canvas-based tile layers.
3116 L.TileLayer.Canvas = L.TileLayer.extend({
3121 initialize: function (options) {
3122 L.setOptions(this, options);
3125 redraw: function () {
3127 this._reset({hard: true});
3131 for (var i in this._tiles) {
3132 this._redrawTile(this._tiles[i]);
3137 _redrawTile: function (tile) {
3138 this.drawTile(tile, tile._tilePoint, this._map._zoom);
3141 _createTile: function () {
3142 var tile = L.DomUtil.create('canvas', 'leaflet-tile');
3143 tile.width = tile.height = this.options.tileSize;
3144 tile.onselectstart = tile.onmousemove = L.Util.falseFn;
3148 _loadTile: function (tile, tilePoint) {
3150 tile._tilePoint = tilePoint;
3152 this._redrawTile(tile);
3154 if (!this.options.async) {
3155 this.tileDrawn(tile);
3159 drawTile: function (/*tile, tilePoint*/) {
3160 // override with rendering code
3163 tileDrawn: function (tile) {
3164 this._tileOnLoad.call(tile);
3169 L.tileLayer.canvas = function (options) {
3170 return new L.TileLayer.Canvas(options);
3175 * L.ImageOverlay is used to overlay images over the map (to specific geographical bounds).
3178 L.ImageOverlay = L.Class.extend({
3179 includes: L.Mixin.Events,
3185 initialize: function (url, bounds, options) { // (String, LatLngBounds, Object)
3187 this._bounds = L.latLngBounds(bounds);
3189 L.setOptions(this, options);
3192 onAdd: function (map) {
3199 map._panes.overlayPane.appendChild(this._image);
3201 map.on('viewreset', this._reset, this);
3203 if (map.options.zoomAnimation && L.Browser.any3d) {
3204 map.on('zoomanim', this._animateZoom, this);
3210 onRemove: function (map) {
3211 map.getPanes().overlayPane.removeChild(this._image);
3213 map.off('viewreset', this._reset, this);
3215 if (map.options.zoomAnimation) {
3216 map.off('zoomanim', this._animateZoom, this);
3220 addTo: function (map) {
3225 setOpacity: function (opacity) {
3226 this.options.opacity = opacity;
3227 this._updateOpacity();
3231 // TODO remove bringToFront/bringToBack duplication from TileLayer/Path
3232 bringToFront: function () {
3234 this._map._panes.overlayPane.appendChild(this._image);
3239 bringToBack: function () {
3240 var pane = this._map._panes.overlayPane;
3242 pane.insertBefore(this._image, pane.firstChild);
3247 setUrl: function (url) {
3249 this._image.src = this._url;
3252 getAttribution: function () {
3253 return this.options.attribution;
3256 _initImage: function () {
3257 this._image = L.DomUtil.create('img', 'leaflet-image-layer');
3259 if (this._map.options.zoomAnimation && L.Browser.any3d) {
3260 L.DomUtil.addClass(this._image, 'leaflet-zoom-animated');
3262 L.DomUtil.addClass(this._image, 'leaflet-zoom-hide');
3265 this._updateOpacity();
3267 //TODO createImage util method to remove duplication
3268 L.extend(this._image, {
3270 onselectstart: L.Util.falseFn,
3271 onmousemove: L.Util.falseFn,
3272 onload: L.bind(this._onImageLoad, this),
3277 _animateZoom: function (e) {
3278 var map = this._map,
3279 image = this._image,
3280 scale = map.getZoomScale(e.zoom),
3281 nw = this._bounds.getNorthWest(),
3282 se = this._bounds.getSouthEast(),
3284 topLeft = map._latLngToNewLayerPoint(nw, e.zoom, e.center),
3285 size = map._latLngToNewLayerPoint(se, e.zoom, e.center)._subtract(topLeft),
3286 origin = topLeft._add(size._multiplyBy((1 / 2) * (1 - 1 / scale)));
3288 image.style[L.DomUtil.TRANSFORM] =
3289 L.DomUtil.getTranslateString(origin) + ' scale(' + scale + ') ';
3292 _reset: function () {
3293 var image = this._image,
3294 topLeft = this._map.latLngToLayerPoint(this._bounds.getNorthWest()),
3295 size = this._map.latLngToLayerPoint(this._bounds.getSouthEast())._subtract(topLeft);
3297 L.DomUtil.setPosition(image, topLeft);
3299 image.style.width = size.x + 'px';
3300 image.style.height = size.y + 'px';
3303 _onImageLoad: function () {
3307 _updateOpacity: function () {
3308 L.DomUtil.setOpacity(this._image, this.options.opacity);
3312 L.imageOverlay = function (url, bounds, options) {
3313 return new L.ImageOverlay(url, bounds, options);
3318 * L.Icon is an image-based icon class that you can use with L.Marker for custom markers.
3321 L.Icon = L.Class.extend({
3324 iconUrl: (String) (required)
3325 iconRetinaUrl: (String) (optional, used for retina devices if detected)
3326 iconSize: (Point) (can be set through CSS)
3327 iconAnchor: (Point) (centered by default, can be set in CSS with negative margins)
3328 popupAnchor: (Point) (if not specified, popup opens in the anchor point)
3329 shadowUrl: (String) (no shadow by default)
3330 shadowRetinaUrl: (String) (optional, used for retina devices if detected)
3332 shadowAnchor: (Point)
3337 initialize: function (options) {
3338 L.setOptions(this, options);
3341 createIcon: function (oldIcon) {
3342 return this._createIcon('icon', oldIcon);
3345 createShadow: function (oldIcon) {
3346 return this._createIcon('shadow', oldIcon);
3349 _createIcon: function (name, oldIcon) {
3350 var src = this._getIconUrl(name);
3353 if (name === 'icon') {
3354 throw new Error('iconUrl not set in Icon options (see the docs).');
3360 if (!oldIcon || oldIcon.tagName !== 'IMG') {
3361 img = this._createImg(src);
3363 img = this._createImg(src, oldIcon);
3365 this._setIconStyles(img, name);
3370 _setIconStyles: function (img, name) {
3371 var options = this.options,
3372 size = L.point(options[name + 'Size']),
3375 if (name === 'shadow') {
3376 anchor = L.point(options.shadowAnchor || options.iconAnchor);
3378 anchor = L.point(options.iconAnchor);
3381 if (!anchor && size) {
3382 anchor = size.divideBy(2, true);
3385 img.className = 'leaflet-marker-' + name + ' ' + options.className;
3388 img.style.marginLeft = (-anchor.x) + 'px';
3389 img.style.marginTop = (-anchor.y) + 'px';
3393 img.style.width = size.x + 'px';
3394 img.style.height = size.y + 'px';
3398 _createImg: function (src, el) {
3399 el = el || document.createElement('img');
3404 _getIconUrl: function (name) {
3405 if (L.Browser.retina && this.options[name + 'RetinaUrl']) {
3406 return this.options[name + 'RetinaUrl'];
3408 return this.options[name + 'Url'];
3412 L.icon = function (options) {
3413 return new L.Icon(options);
3418 * L.Icon.Default is the blue marker icon used by default in Leaflet.
3421 L.Icon.Default = L.Icon.extend({
3425 iconAnchor: [12, 41],
3426 popupAnchor: [1, -34],
3428 shadowSize: [41, 41]
3431 _getIconUrl: function (name) {
3432 var key = name + 'Url';
3434 if (this.options[key]) {
3435 return this.options[key];
3438 if (L.Browser.retina && name === 'icon') {
3442 var path = L.Icon.Default.imagePath;
3445 throw new Error('Couldn\'t autodetect L.Icon.Default.imagePath, set it manually.');
3448 return path + '/marker-' + name + '.png';
3452 L.Icon.Default.imagePath = (function () {
3453 var scripts = document.getElementsByTagName('script'),
3454 leafletRe = /[\/^]leaflet[\-\._]?([\w\-\._]*)\.js\??/;
3456 var i, len, src, matches, path;
3458 for (i = 0, len = scripts.length; i < len; i++) {
3459 src = scripts[i].src;
3460 matches = src.match(leafletRe);
3463 path = src.split(leafletRe)[0];
3464 return (path ? path + '/' : '') + 'images';
3471 * L.Marker is used to display clickable/draggable icons on the map.
3474 L.Marker = L.Class.extend({
3476 includes: L.Mixin.Events,
3479 icon: new L.Icon.Default(),
3491 initialize: function (latlng, options) {
3492 L.setOptions(this, options);
3493 this._latlng = L.latLng(latlng);
3496 onAdd: function (map) {
3499 map.on('viewreset', this.update, this);
3505 if (map.options.zoomAnimation && map.options.markerZoomAnimation) {
3506 map.on('zoomanim', this._animateZoom, this);
3510 addTo: function (map) {
3515 onRemove: function (map) {
3516 if (this.dragging) {
3517 this.dragging.disable();
3521 this._removeShadow();
3523 this.fire('remove');
3526 'viewreset': this.update,
3527 'zoomanim': this._animateZoom
3533 getLatLng: function () {
3534 return this._latlng;
3537 setLatLng: function (latlng) {
3538 this._latlng = L.latLng(latlng);
3542 return this.fire('move', { latlng: this._latlng });
3545 setZIndexOffset: function (offset) {
3546 this.options.zIndexOffset = offset;
3552 setIcon: function (icon) {
3554 this.options.icon = icon;
3562 this.bindPopup(this._popup);
3568 update: function () {
3570 var pos = this._map.latLngToLayerPoint(this._latlng).round();
3577 _initIcon: function () {
3578 var options = this.options,
3580 animation = (map.options.zoomAnimation && map.options.markerZoomAnimation),
3581 classToAdd = animation ? 'leaflet-zoom-animated' : 'leaflet-zoom-hide';
3583 var icon = options.icon.createIcon(this._icon),
3586 // if we're not reusing the icon, remove the old one and init new one
3587 if (icon !== this._icon) {
3593 if (options.title) {
3594 icon.title = options.title;
3598 icon.alt = options.alt;
3602 L.DomUtil.addClass(icon, classToAdd);
3604 if (options.keyboard) {
3605 icon.tabIndex = '0';
3610 this._initInteraction();
3612 if (options.riseOnHover) {
3614 .on(icon, 'mouseover', this._bringToFront, this)
3615 .on(icon, 'mouseout', this._resetZIndex, this);
3618 var newShadow = options.icon.createShadow(this._shadow),
3621 if (newShadow !== this._shadow) {
3622 this._removeShadow();
3627 L.DomUtil.addClass(newShadow, classToAdd);
3629 this._shadow = newShadow;
3632 if (options.opacity < 1) {
3633 this._updateOpacity();
3637 var panes = this._map._panes;
3640 panes.markerPane.appendChild(this._icon);
3643 if (newShadow && addShadow) {
3644 panes.shadowPane.appendChild(this._shadow);
3648 _removeIcon: function () {
3649 if (this.options.riseOnHover) {
3651 .off(this._icon, 'mouseover', this._bringToFront)
3652 .off(this._icon, 'mouseout', this._resetZIndex);
3655 this._map._panes.markerPane.removeChild(this._icon);
3660 _removeShadow: function () {
3662 this._map._panes.shadowPane.removeChild(this._shadow);
3664 this._shadow = null;
3667 _setPos: function (pos) {
3668 L.DomUtil.setPosition(this._icon, pos);
3671 L.DomUtil.setPosition(this._shadow, pos);
3674 this._zIndex = pos.y + this.options.zIndexOffset;
3676 this._resetZIndex();
3679 _updateZIndex: function (offset) {
3680 this._icon.style.zIndex = this._zIndex + offset;
3683 _animateZoom: function (opt) {
3684 var pos = this._map._latLngToNewLayerPoint(this._latlng, opt.zoom, opt.center).round();
3689 _initInteraction: function () {
3691 if (!this.options.clickable) { return; }
3693 // TODO refactor into something shared with Map/Path/etc. to DRY it up
3695 var icon = this._icon,
3696 events = ['dblclick', 'mousedown', 'mouseover', 'mouseout', 'contextmenu'];
3698 L.DomUtil.addClass(icon, 'leaflet-clickable');
3699 L.DomEvent.on(icon, 'click', this._onMouseClick, this);
3700 L.DomEvent.on(icon, 'keypress', this._onKeyPress, this);
3702 for (var i = 0; i < events.length; i++) {
3703 L.DomEvent.on(icon, events[i], this._fireMouseEvent, this);
3706 if (L.Handler.MarkerDrag) {
3707 this.dragging = new L.Handler.MarkerDrag(this);
3709 if (this.options.draggable) {
3710 this.dragging.enable();
3715 _onMouseClick: function (e) {
3716 var wasDragged = this.dragging && this.dragging.moved();
3718 if (this.hasEventListeners(e.type) || wasDragged) {
3719 L.DomEvent.stopPropagation(e);
3722 if (wasDragged) { return; }
3724 if ((!this.dragging || !this.dragging._enabled) && this._map.dragging && this._map.dragging.moved()) { return; }
3728 latlng: this._latlng
3732 _onKeyPress: function (e) {
3733 if (e.keyCode === 13) {
3734 this.fire('click', {
3736 latlng: this._latlng
3741 _fireMouseEvent: function (e) {
3745 latlng: this._latlng
3748 // TODO proper custom event propagation
3749 // this line will always be called if marker is in a FeatureGroup
3750 if (e.type === 'contextmenu' && this.hasEventListeners(e.type)) {
3751 L.DomEvent.preventDefault(e);
3753 if (e.type !== 'mousedown') {
3754 L.DomEvent.stopPropagation(e);
3756 L.DomEvent.preventDefault(e);
3760 setOpacity: function (opacity) {
3761 this.options.opacity = opacity;
3763 this._updateOpacity();
3769 _updateOpacity: function () {
3770 L.DomUtil.setOpacity(this._icon, this.options.opacity);
3772 L.DomUtil.setOpacity(this._shadow, this.options.opacity);
3776 _bringToFront: function () {
3777 this._updateZIndex(this.options.riseOffset);
3780 _resetZIndex: function () {
3781 this._updateZIndex(0);
3785 L.marker = function (latlng, options) {
3786 return new L.Marker(latlng, options);
3791 * L.DivIcon is a lightweight HTML-based icon class (as opposed to the image-based L.Icon)
3792 * to use with L.Marker.
3795 L.DivIcon = L.Icon.extend({
3797 iconSize: [12, 12], // also can be set through CSS
3800 popupAnchor: (Point)
3804 className: 'leaflet-div-icon',
3808 createIcon: function (oldIcon) {
3809 var div = (oldIcon && oldIcon.tagName === 'DIV') ? oldIcon : document.createElement('div'),
3810 options = this.options;
3812 if (options.html !== false) {
3813 div.innerHTML = options.html;
3818 if (options.bgPos) {
3819 div.style.backgroundPosition =
3820 (-options.bgPos.x) + 'px ' + (-options.bgPos.y) + 'px';
3823 this._setIconStyles(div, 'icon');
3827 createShadow: function () {
3832 L.divIcon = function (options) {
3833 return new L.DivIcon(options);
3838 * L.Popup is used for displaying popups on the map.
3841 L.Map.mergeOptions({
3842 closePopupOnClick: true
3845 L.Popup = L.Class.extend({
3846 includes: L.Mixin.Events,
3855 autoPanPadding: [5, 5],
3856 // autoPanPaddingTopLeft: null,
3857 // autoPanPaddingBottomRight: null,
3863 initialize: function (options, source) {
3864 L.setOptions(this, options);
3866 this._source = source;
3867 this._animated = L.Browser.any3d && this.options.zoomAnimation;
3868 this._isOpen = false;
3871 onAdd: function (map) {
3874 if (!this._container) {
3878 var animFade = map.options.fadeAnimation;
3881 L.DomUtil.setOpacity(this._container, 0);
3883 map._panes.popupPane.appendChild(this._container);
3885 map.on(this._getEvents(), this);
3890 L.DomUtil.setOpacity(this._container, 1);
3895 map.fire('popupopen', {popup: this});
3898 this._source.fire('popupopen', {popup: this});
3902 addTo: function (map) {
3907 openOn: function (map) {
3908 map.openPopup(this);
3912 onRemove: function (map) {
3913 map._panes.popupPane.removeChild(this._container);
3915 L.Util.falseFn(this._container.offsetWidth); // force reflow
3917 map.off(this._getEvents(), this);
3919 if (map.options.fadeAnimation) {
3920 L.DomUtil.setOpacity(this._container, 0);
3927 map.fire('popupclose', {popup: this});
3930 this._source.fire('popupclose', {popup: this});
3934 getLatLng: function () {
3935 return this._latlng;
3938 setLatLng: function (latlng) {
3939 this._latlng = L.latLng(latlng);
3941 this._updatePosition();
3947 getContent: function () {
3948 return this._content;
3951 setContent: function (content) {
3952 this._content = content;
3957 update: function () {
3958 if (!this._map) { return; }
3960 this._container.style.visibility = 'hidden';
3962 this._updateContent();
3963 this._updateLayout();
3964 this._updatePosition();
3966 this._container.style.visibility = '';
3971 _getEvents: function () {
3973 viewreset: this._updatePosition
3976 if (this._animated) {
3977 events.zoomanim = this._zoomAnimation;
3979 if ('closeOnClick' in this.options ? this.options.closeOnClick : this._map.options.closePopupOnClick) {
3980 events.preclick = this._close;
3982 if (this.options.keepInView) {
3983 events.moveend = this._adjustPan;
3989 _close: function () {
3991 this._map.closePopup(this);
3995 _initLayout: function () {
3996 var prefix = 'leaflet-popup',
3997 containerClass = prefix + ' ' + this.options.className + ' leaflet-zoom-' +
3998 (this._animated ? 'animated' : 'hide'),
3999 container = this._container = L.DomUtil.create('div', containerClass),
4002 if (this.options.closeButton) {
4003 closeButton = this._closeButton =
4004 L.DomUtil.create('a', prefix + '-close-button', container);
4005 closeButton.href = '#close';
4006 closeButton.innerHTML = '×';
4007 L.DomEvent.disableClickPropagation(closeButton);
4009 L.DomEvent.on(closeButton, 'click', this._onCloseButtonClick, this);
4012 var wrapper = this._wrapper =
4013 L.DomUtil.create('div', prefix + '-content-wrapper', container);
4014 L.DomEvent.disableClickPropagation(wrapper);
4016 this._contentNode = L.DomUtil.create('div', prefix + '-content', wrapper);
4018 L.DomEvent.disableScrollPropagation(this._contentNode);
4019 L.DomEvent.on(wrapper, 'contextmenu', L.DomEvent.stopPropagation);
4021 this._tipContainer = L.DomUtil.create('div', prefix + '-tip-container', container);
4022 this._tip = L.DomUtil.create('div', prefix + '-tip', this._tipContainer);
4025 _updateContent: function () {
4026 if (!this._content) { return; }
4028 if (typeof this._content === 'string') {
4029 this._contentNode.innerHTML = this._content;
4031 while (this._contentNode.hasChildNodes()) {
4032 this._contentNode.removeChild(this._contentNode.firstChild);
4034 this._contentNode.appendChild(this._content);
4036 this.fire('contentupdate');
4039 _updateLayout: function () {
4040 var container = this._contentNode,
4041 style = container.style;
4044 style.whiteSpace = 'nowrap';
4046 var width = container.offsetWidth;
4047 width = Math.min(width, this.options.maxWidth);
4048 width = Math.max(width, this.options.minWidth);
4050 style.width = (width + 1) + 'px';
4051 style.whiteSpace = '';
4055 var height = container.offsetHeight,
4056 maxHeight = this.options.maxHeight,
4057 scrolledClass = 'leaflet-popup-scrolled';
4059 if (maxHeight && height > maxHeight) {
4060 style.height = maxHeight + 'px';
4061 L.DomUtil.addClass(container, scrolledClass);
4063 L.DomUtil.removeClass(container, scrolledClass);
4066 this._containerWidth = this._container.offsetWidth;
4069 _updatePosition: function () {
4070 if (!this._map) { return; }
4072 var pos = this._map.latLngToLayerPoint(this._latlng),
4073 animated = this._animated,
4074 offset = L.point(this.options.offset);
4077 L.DomUtil.setPosition(this._container, pos);
4080 this._containerBottom = -offset.y - (animated ? 0 : pos.y);
4081 this._containerLeft = -Math.round(this._containerWidth / 2) + offset.x + (animated ? 0 : pos.x);
4083 // bottom position the popup in case the height of the popup changes (images loading etc)
4084 this._container.style.bottom = this._containerBottom + 'px';
4085 this._container.style.left = this._containerLeft + 'px';
4088 _zoomAnimation: function (opt) {
4089 var pos = this._map._latLngToNewLayerPoint(this._latlng, opt.zoom, opt.center);
4091 L.DomUtil.setPosition(this._container, pos);
4094 _adjustPan: function () {
4095 if (!this.options.autoPan) { return; }
4097 var map = this._map,
4098 containerHeight = this._container.offsetHeight,
4099 containerWidth = this._containerWidth,
4101 layerPos = new L.Point(this._containerLeft, -containerHeight - this._containerBottom);
4103 if (this._animated) {
4104 layerPos._add(L.DomUtil.getPosition(this._container));
4107 var containerPos = map.layerPointToContainerPoint(layerPos),
4108 padding = L.point(this.options.autoPanPadding),
4109 paddingTL = L.point(this.options.autoPanPaddingTopLeft || padding),
4110 paddingBR = L.point(this.options.autoPanPaddingBottomRight || padding),
4111 size = map.getSize(),
4115 if (containerPos.x + containerWidth + paddingBR.x > size.x) { // right
4116 dx = containerPos.x + containerWidth - size.x + paddingBR.x;
4118 if (containerPos.x - dx - paddingTL.x < 0) { // left
4119 dx = containerPos.x - paddingTL.x;
4121 if (containerPos.y + containerHeight + paddingBR.y > size.y) { // bottom
4122 dy = containerPos.y + containerHeight - size.y + paddingBR.y;
4124 if (containerPos.y - dy - paddingTL.y < 0) { // top
4125 dy = containerPos.y - paddingTL.y;
4130 .fire('autopanstart')
4135 _onCloseButtonClick: function (e) {
4141 L.popup = function (options, source) {
4142 return new L.Popup(options, source);
4147 openPopup: function (popup, latlng, options) { // (Popup) or (String || HTMLElement, LatLng[, Object])
4150 if (!(popup instanceof L.Popup)) {
4151 var content = popup;
4153 popup = new L.Popup(options)
4155 .setContent(content);
4157 popup._isOpen = true;
4159 this._popup = popup;
4160 return this.addLayer(popup);
4163 closePopup: function (popup) {
4164 if (!popup || popup === this._popup) {
4165 popup = this._popup;
4169 this.removeLayer(popup);
4170 popup._isOpen = false;
4178 * Popup extension to L.Marker, adding popup-related methods.
4182 openPopup: function () {
4183 if (this._popup && this._map && !this._map.hasLayer(this._popup)) {
4184 this._popup.setLatLng(this._latlng);
4185 this._map.openPopup(this._popup);
4191 closePopup: function () {
4193 this._popup._close();
4198 togglePopup: function () {
4200 if (this._popup._isOpen) {
4209 bindPopup: function (content, options) {
4210 var anchor = L.point(this.options.icon.options.popupAnchor || [0, 0]);
4212 anchor = anchor.add(L.Popup.prototype.options.offset);
4214 if (options && options.offset) {
4215 anchor = anchor.add(options.offset);
4218 options = L.extend({offset: anchor}, options);
4220 if (!this._popupHandlersAdded) {
4222 .on('click', this.togglePopup, this)
4223 .on('remove', this.closePopup, this)
4224 .on('move', this._movePopup, this);
4225 this._popupHandlersAdded = true;
4228 if (content instanceof L.Popup) {
4229 L.setOptions(content, options);
4230 this._popup = content;
4232 this._popup = new L.Popup(options, this)
4233 .setContent(content);
4239 setPopupContent: function (content) {
4241 this._popup.setContent(content);
4246 unbindPopup: function () {
4250 .off('click', this.togglePopup, this)
4251 .off('remove', this.closePopup, this)
4252 .off('move', this._movePopup, this);
4253 this._popupHandlersAdded = false;
4258 getPopup: function () {
4262 _movePopup: function (e) {
4263 this._popup.setLatLng(e.latlng);
4269 * L.LayerGroup is a class to combine several layers into one so that
4270 * you can manipulate the group (e.g. add/remove it) as one layer.
4273 L.LayerGroup = L.Class.extend({
4274 initialize: function (layers) {
4280 for (i = 0, len = layers.length; i < len; i++) {
4281 this.addLayer(layers[i]);
4286 addLayer: function (layer) {
4287 var id = this.getLayerId(layer);
4289 this._layers[id] = layer;
4292 this._map.addLayer(layer);
4298 removeLayer: function (layer) {
4299 var id = layer in this._layers ? layer : this.getLayerId(layer);
4301 if (this._map && this._layers[id]) {
4302 this._map.removeLayer(this._layers[id]);
4305 delete this._layers[id];
4310 hasLayer: function (layer) {
4311 if (!layer) { return false; }
4313 return (layer in this._layers || this.getLayerId(layer) in this._layers);
4316 clearLayers: function () {
4317 this.eachLayer(this.removeLayer, this);
4321 invoke: function (methodName) {
4322 var args = Array.prototype.slice.call(arguments, 1),
4325 for (i in this._layers) {
4326 layer = this._layers[i];
4328 if (layer[methodName]) {
4329 layer[methodName].apply(layer, args);
4336 onAdd: function (map) {
4338 this.eachLayer(map.addLayer, map);
4341 onRemove: function (map) {
4342 this.eachLayer(map.removeLayer, map);
4346 addTo: function (map) {
4351 eachLayer: function (method, context) {
4352 for (var i in this._layers) {
4353 method.call(context, this._layers[i]);
4358 getLayer: function (id) {
4359 return this._layers[id];
4362 getLayers: function () {
4365 for (var i in this._layers) {
4366 layers.push(this._layers[i]);
4371 setZIndex: function (zIndex) {
4372 return this.invoke('setZIndex', zIndex);
4375 getLayerId: function (layer) {
4376 return L.stamp(layer);
4380 L.layerGroup = function (layers) {
4381 return new L.LayerGroup(layers);
4386 * L.FeatureGroup extends L.LayerGroup by introducing mouse events and additional methods
4387 * shared between a group of interactive layers (like vectors or markers).
4390 L.FeatureGroup = L.LayerGroup.extend({
4391 includes: L.Mixin.Events,
4394 EVENTS: 'click dblclick mouseover mouseout mousemove contextmenu popupopen popupclose'
4397 addLayer: function (layer) {
4398 if (this.hasLayer(layer)) {
4402 if ('on' in layer) {
4403 layer.on(L.FeatureGroup.EVENTS, this._propagateEvent, this);
4406 L.LayerGroup.prototype.addLayer.call(this, layer);
4408 if (this._popupContent && layer.bindPopup) {
4409 layer.bindPopup(this._popupContent, this._popupOptions);
4412 return this.fire('layeradd', {layer: layer});
4415 removeLayer: function (layer) {
4416 if (!this.hasLayer(layer)) {
4419 if (layer in this._layers) {
4420 layer = this._layers[layer];
4423 layer.off(L.FeatureGroup.EVENTS, this._propagateEvent, this);
4425 L.LayerGroup.prototype.removeLayer.call(this, layer);
4427 if (this._popupContent) {
4428 this.invoke('unbindPopup');
4431 return this.fire('layerremove', {layer: layer});
4434 bindPopup: function (content, options) {
4435 this._popupContent = content;
4436 this._popupOptions = options;
4437 return this.invoke('bindPopup', content, options);
4440 openPopup: function (latlng) {
4441 // open popup on the first layer
4442 for (var id in this._layers) {
4443 this._layers[id].openPopup(latlng);
4449 setStyle: function (style) {
4450 return this.invoke('setStyle', style);
4453 bringToFront: function () {
4454 return this.invoke('bringToFront');
4457 bringToBack: function () {
4458 return this.invoke('bringToBack');
4461 getBounds: function () {
4462 var bounds = new L.LatLngBounds();
4464 this.eachLayer(function (layer) {
4465 bounds.extend(layer instanceof L.Marker ? layer.getLatLng() : layer.getBounds());
4471 _propagateEvent: function (e) {
4476 this.fire(e.type, e);
4480 L.featureGroup = function (layers) {
4481 return new L.FeatureGroup(layers);
4486 * L.Path is a base class for rendering vector paths on a map. Inherited by Polyline, Circle, etc.
4489 L.Path = L.Class.extend({
4490 includes: [L.Mixin.Events],
4493 // how much to extend the clip area around the map view
4494 // (relative to its size, e.g. 0.5 is half the screen in each direction)
4495 // set it so that SVG element doesn't exceed 1280px (vectors flicker on dragend if it is)
4496 CLIP_PADDING: (function () {
4497 var max = L.Browser.mobile ? 1280 : 2000,
4498 target = (max / Math.max(window.outerWidth, window.outerHeight) - 1) / 2;
4499 return Math.max(0, Math.min(0.5, target));
4513 fillColor: null, //same as color by default
4519 initialize: function (options) {
4520 L.setOptions(this, options);
4523 onAdd: function (map) {
4526 if (!this._container) {
4527 this._initElements();
4531 this.projectLatlngs();
4534 if (this._container) {
4535 this._map._pathRoot.appendChild(this._container);
4541 'viewreset': this.projectLatlngs,
4542 'moveend': this._updatePath
4546 addTo: function (map) {
4551 onRemove: function (map) {
4552 map._pathRoot.removeChild(this._container);
4554 // Need to fire remove event before we set _map to null as the event hooks might need the object
4555 this.fire('remove');
4558 if (L.Browser.vml) {
4559 this._container = null;
4560 this._stroke = null;
4565 'viewreset': this.projectLatlngs,
4566 'moveend': this._updatePath
4570 projectLatlngs: function () {
4571 // do all projection stuff here
4574 setStyle: function (style) {
4575 L.setOptions(this, style);
4577 if (this._container) {
4578 this._updateStyle();
4584 redraw: function () {
4586 this.projectLatlngs();
4594 _updatePathViewport: function () {
4595 var p = L.Path.CLIP_PADDING,
4596 size = this.getSize(),
4597 panePos = L.DomUtil.getPosition(this._mapPane),
4598 min = panePos.multiplyBy(-1)._subtract(size.multiplyBy(p)._round()),
4599 max = min.add(size.multiplyBy(1 + p * 2)._round());
4601 this._pathViewport = new L.Bounds(min, max);
4607 * Extends L.Path with SVG-specific rendering code.
4610 L.Path.SVG_NS = 'http://www.w3.org/2000/svg';
4612 L.Browser.svg = !!(document.createElementNS && document.createElementNS(L.Path.SVG_NS, 'svg').createSVGRect);
4614 L.Path = L.Path.extend({
4619 bringToFront: function () {
4620 var root = this._map._pathRoot,
4621 path = this._container;
4623 if (path && root.lastChild !== path) {
4624 root.appendChild(path);
4629 bringToBack: function () {
4630 var root = this._map._pathRoot,
4631 path = this._container,
4632 first = root.firstChild;
4634 if (path && first !== path) {
4635 root.insertBefore(path, first);
4640 getPathString: function () {
4641 // form path string here
4644 _createElement: function (name) {
4645 return document.createElementNS(L.Path.SVG_NS, name);
4648 _initElements: function () {
4649 this._map._initPathRoot();
4654 _initPath: function () {
4655 this._container = this._createElement('g');
4657 this._path = this._createElement('path');
4659 if (this.options.className) {
4660 L.DomUtil.addClass(this._path, this.options.className);
4663 this._container.appendChild(this._path);
4666 _initStyle: function () {
4667 if (this.options.stroke) {
4668 this._path.setAttribute('stroke-linejoin', 'round');
4669 this._path.setAttribute('stroke-linecap', 'round');
4671 if (this.options.fill) {
4672 this._path.setAttribute('fill-rule', 'evenodd');
4674 if (this.options.pointerEvents) {
4675 this._path.setAttribute('pointer-events', this.options.pointerEvents);
4677 if (!this.options.clickable && !this.options.pointerEvents) {
4678 this._path.setAttribute('pointer-events', 'none');
4680 this._updateStyle();
4683 _updateStyle: function () {
4684 if (this.options.stroke) {
4685 this._path.setAttribute('stroke', this.options.color);
4686 this._path.setAttribute('stroke-opacity', this.options.opacity);
4687 this._path.setAttribute('stroke-width', this.options.weight);
4688 if (this.options.dashArray) {
4689 this._path.setAttribute('stroke-dasharray', this.options.dashArray);
4691 this._path.removeAttribute('stroke-dasharray');
4693 if (this.options.lineCap) {
4694 this._path.setAttribute('stroke-linecap', this.options.lineCap);
4696 if (this.options.lineJoin) {
4697 this._path.setAttribute('stroke-linejoin', this.options.lineJoin);
4700 this._path.setAttribute('stroke', 'none');
4702 if (this.options.fill) {
4703 this._path.setAttribute('fill', this.options.fillColor || this.options.color);
4704 this._path.setAttribute('fill-opacity', this.options.fillOpacity);
4706 this._path.setAttribute('fill', 'none');
4710 _updatePath: function () {
4711 var str = this.getPathString();
4713 // fix webkit empty string parsing bug
4716 this._path.setAttribute('d', str);
4719 // TODO remove duplication with L.Map
4720 _initEvents: function () {
4721 if (this.options.clickable) {
4722 if (L.Browser.svg || !L.Browser.vml) {
4723 L.DomUtil.addClass(this._path, 'leaflet-clickable');
4726 L.DomEvent.on(this._container, 'click', this._onMouseClick, this);
4728 var events = ['dblclick', 'mousedown', 'mouseover',
4729 'mouseout', 'mousemove', 'contextmenu'];
4730 for (var i = 0; i < events.length; i++) {
4731 L.DomEvent.on(this._container, events[i], this._fireMouseEvent, this);
4736 _onMouseClick: function (e) {
4737 if (this._map.dragging && this._map.dragging.moved()) { return; }
4739 this._fireMouseEvent(e);
4742 _fireMouseEvent: function (e) {
4743 if (!this.hasEventListeners(e.type)) { return; }
4745 var map = this._map,
4746 containerPoint = map.mouseEventToContainerPoint(e),
4747 layerPoint = map.containerPointToLayerPoint(containerPoint),
4748 latlng = map.layerPointToLatLng(layerPoint);
4752 layerPoint: layerPoint,
4753 containerPoint: containerPoint,
4757 if (e.type === 'contextmenu') {
4758 L.DomEvent.preventDefault(e);
4760 if (e.type !== 'mousemove') {
4761 L.DomEvent.stopPropagation(e);
4767 _initPathRoot: function () {
4768 if (!this._pathRoot) {
4769 this._pathRoot = L.Path.prototype._createElement('svg');
4770 this._panes.overlayPane.appendChild(this._pathRoot);
4772 if (this.options.zoomAnimation && L.Browser.any3d) {
4773 L.DomUtil.addClass(this._pathRoot, 'leaflet-zoom-animated');
4776 'zoomanim': this._animatePathZoom,
4777 'zoomend': this._endPathZoom
4780 L.DomUtil.addClass(this._pathRoot, 'leaflet-zoom-hide');
4783 this.on('moveend', this._updateSvgViewport);
4784 this._updateSvgViewport();
4788 _animatePathZoom: function (e) {
4789 var scale = this.getZoomScale(e.zoom),
4790 offset = this._getCenterOffset(e.center)._multiplyBy(-scale)._add(this._pathViewport.min);
4792 this._pathRoot.style[L.DomUtil.TRANSFORM] =
4793 L.DomUtil.getTranslateString(offset) + ' scale(' + scale + ') ';
4795 this._pathZooming = true;
4798 _endPathZoom: function () {
4799 this._pathZooming = false;
4802 _updateSvgViewport: function () {
4804 if (this._pathZooming) {
4805 // Do not update SVGs while a zoom animation is going on otherwise the animation will break.
4806 // When the zoom animation ends we will be updated again anyway
4807 // This fixes the case where you do a momentum move and zoom while the move is still ongoing.
4811 this._updatePathViewport();
4813 var vp = this._pathViewport,
4816 width = max.x - min.x,
4817 height = max.y - min.y,
4818 root = this._pathRoot,
4819 pane = this._panes.overlayPane;
4821 // Hack to make flicker on drag end on mobile webkit less irritating
4822 if (L.Browser.mobileWebkit) {
4823 pane.removeChild(root);
4826 L.DomUtil.setPosition(root, min);
4827 root.setAttribute('width', width);
4828 root.setAttribute('height', height);
4829 root.setAttribute('viewBox', [min.x, min.y, width, height].join(' '));
4831 if (L.Browser.mobileWebkit) {
4832 pane.appendChild(root);
4839 * Popup extension to L.Path (polylines, polygons, circles), adding popup-related methods.
4844 bindPopup: function (content, options) {
4846 if (content instanceof L.Popup) {
4847 this._popup = content;
4849 if (!this._popup || options) {
4850 this._popup = new L.Popup(options, this);
4852 this._popup.setContent(content);
4855 if (!this._popupHandlersAdded) {
4857 .on('click', this._openPopup, this)
4858 .on('remove', this.closePopup, this);
4860 this._popupHandlersAdded = true;
4866 unbindPopup: function () {
4870 .off('click', this._openPopup)
4871 .off('remove', this.closePopup);
4873 this._popupHandlersAdded = false;
4878 openPopup: function (latlng) {
4881 // open the popup from one of the path's points if not specified
4882 latlng = latlng || this._latlng ||
4883 this._latlngs[Math.floor(this._latlngs.length / 2)];
4885 this._openPopup({latlng: latlng});
4891 closePopup: function () {
4893 this._popup._close();
4898 _openPopup: function (e) {
4899 this._popup.setLatLng(e.latlng);
4900 this._map.openPopup(this._popup);
4906 * Vector rendering for IE6-8 through VML.
4907 * Thanks to Dmitry Baranovsky and his Raphael library for inspiration!
4910 L.Browser.vml = !L.Browser.svg && (function () {
4912 var div = document.createElement('div');
4913 div.innerHTML = '<v:shape adj="1"/>';
4915 var shape = div.firstChild;
4916 shape.style.behavior = 'url(#default#VML)';
4918 return shape && (typeof shape.adj === 'object');
4925 L.Path = L.Browser.svg || !L.Browser.vml ? L.Path : L.Path.extend({
4931 _createElement: (function () {
4933 document.namespaces.add('lvml', 'urn:schemas-microsoft-com:vml');
4934 return function (name) {
4935 return document.createElement('<lvml:' + name + ' class="lvml">');
4938 return function (name) {
4939 return document.createElement(
4940 '<' + name + ' xmlns="urn:schemas-microsoft.com:vml" class="lvml">');
4945 _initPath: function () {
4946 var container = this._container = this._createElement('shape');
4948 L.DomUtil.addClass(container, 'leaflet-vml-shape' +
4949 (this.options.className ? ' ' + this.options.className : ''));
4951 if (this.options.clickable) {
4952 L.DomUtil.addClass(container, 'leaflet-clickable');
4955 container.coordsize = '1 1';
4957 this._path = this._createElement('path');
4958 container.appendChild(this._path);
4960 this._map._pathRoot.appendChild(container);
4963 _initStyle: function () {
4964 this._updateStyle();
4967 _updateStyle: function () {
4968 var stroke = this._stroke,
4970 options = this.options,
4971 container = this._container;
4973 container.stroked = options.stroke;
4974 container.filled = options.fill;
4976 if (options.stroke) {
4978 stroke = this._stroke = this._createElement('stroke');
4979 stroke.endcap = 'round';
4980 container.appendChild(stroke);
4982 stroke.weight = options.weight + 'px';
4983 stroke.color = options.color;
4984 stroke.opacity = options.opacity;
4986 if (options.dashArray) {
4987 stroke.dashStyle = L.Util.isArray(options.dashArray) ?
4988 options.dashArray.join(' ') :
4989 options.dashArray.replace(/( *, *)/g, ' ');
4991 stroke.dashStyle = '';
4993 if (options.lineCap) {
4994 stroke.endcap = options.lineCap.replace('butt', 'flat');
4996 if (options.lineJoin) {
4997 stroke.joinstyle = options.lineJoin;
5000 } else if (stroke) {
5001 container.removeChild(stroke);
5002 this._stroke = null;
5007 fill = this._fill = this._createElement('fill');
5008 container.appendChild(fill);
5010 fill.color = options.fillColor || options.color;
5011 fill.opacity = options.fillOpacity;
5014 container.removeChild(fill);
5019 _updatePath: function () {
5020 var style = this._container.style;
5022 style.display = 'none';
5023 this._path.v = this.getPathString() + ' '; // the space fixes IE empty path string bug
5028 L.Map.include(L.Browser.svg || !L.Browser.vml ? {} : {
5029 _initPathRoot: function () {
5030 if (this._pathRoot) { return; }
5032 var root = this._pathRoot = document.createElement('div');
5033 root.className = 'leaflet-vml-container';
5034 this._panes.overlayPane.appendChild(root);
5036 this.on('moveend', this._updatePathViewport);
5037 this._updatePathViewport();
5043 * Vector rendering for all browsers that support canvas.
5046 L.Browser.canvas = (function () {
5047 return !!document.createElement('canvas').getContext;
5050 L.Path = (L.Path.SVG && !window.L_PREFER_CANVAS) || !L.Browser.canvas ? L.Path : L.Path.extend({
5052 //CLIP_PADDING: 0.02, // not sure if there's a need to set it to a small value
5057 redraw: function () {
5059 this.projectLatlngs();
5060 this._requestUpdate();
5065 setStyle: function (style) {
5066 L.setOptions(this, style);
5069 this._updateStyle();
5070 this._requestUpdate();
5075 onRemove: function (map) {
5077 .off('viewreset', this.projectLatlngs, this)
5078 .off('moveend', this._updatePath, this);
5080 if (this.options.clickable) {
5081 this._map.off('click', this._onClick, this);
5082 this._map.off('mousemove', this._onMouseMove, this);
5085 this._requestUpdate();
5087 this.fire('remove');
5091 _requestUpdate: function () {
5092 if (this._map && !L.Path._updateRequest) {
5093 L.Path._updateRequest = L.Util.requestAnimFrame(this._fireMapMoveEnd, this._map);
5097 _fireMapMoveEnd: function () {
5098 L.Path._updateRequest = null;
5099 this.fire('moveend');
5102 _initElements: function () {
5103 this._map._initPathRoot();
5104 this._ctx = this._map._canvasCtx;
5107 _updateStyle: function () {
5108 var options = this.options;
5110 if (options.stroke) {
5111 this._ctx.lineWidth = options.weight;
5112 this._ctx.strokeStyle = options.color;
5115 this._ctx.fillStyle = options.fillColor || options.color;
5119 _drawPath: function () {
5120 var i, j, len, len2, point, drawMethod;
5122 this._ctx.beginPath();
5124 for (i = 0, len = this._parts.length; i < len; i++) {
5125 for (j = 0, len2 = this._parts[i].length; j < len2; j++) {
5126 point = this._parts[i][j];
5127 drawMethod = (j === 0 ? 'move' : 'line') + 'To';
5129 this._ctx[drawMethod](point.x, point.y);
5131 // TODO refactor ugly hack
5132 if (this instanceof L.Polygon) {
5133 this._ctx.closePath();
5138 _checkIfEmpty: function () {
5139 return !this._parts.length;
5142 _updatePath: function () {
5143 if (this._checkIfEmpty()) { return; }
5145 var ctx = this._ctx,
5146 options = this.options;
5150 this._updateStyle();
5153 ctx.globalAlpha = options.fillOpacity;
5157 if (options.stroke) {
5158 ctx.globalAlpha = options.opacity;
5164 // TODO optimization: 1 fill/stroke for all features with equal style instead of 1 for each feature
5167 _initEvents: function () {
5168 if (this.options.clickable) {
5170 this._map.on('mousemove', this._onMouseMove, this);
5171 this._map.on('click', this._onClick, this);
5175 _onClick: function (e) {
5176 if (this._containsPoint(e.layerPoint)) {
5177 this.fire('click', e);
5181 _onMouseMove: function (e) {
5182 if (!this._map || this._map._animatingZoom) { return; }
5184 // TODO don't do on each move
5185 if (this._containsPoint(e.layerPoint)) {
5186 this._ctx.canvas.style.cursor = 'pointer';
5187 this._mouseInside = true;
5188 this.fire('mouseover', e);
5190 } else if (this._mouseInside) {
5191 this._ctx.canvas.style.cursor = '';
5192 this._mouseInside = false;
5193 this.fire('mouseout', e);
5198 L.Map.include((L.Path.SVG && !window.L_PREFER_CANVAS) || !L.Browser.canvas ? {} : {
5199 _initPathRoot: function () {
5200 var root = this._pathRoot,
5204 root = this._pathRoot = document.createElement('canvas');
5205 root.style.position = 'absolute';
5206 ctx = this._canvasCtx = root.getContext('2d');
5208 ctx.lineCap = 'round';
5209 ctx.lineJoin = 'round';
5211 this._panes.overlayPane.appendChild(root);
5213 if (this.options.zoomAnimation) {
5214 this._pathRoot.className = 'leaflet-zoom-animated';
5215 this.on('zoomanim', this._animatePathZoom);
5216 this.on('zoomend', this._endPathZoom);
5218 this.on('moveend', this._updateCanvasViewport);
5219 this._updateCanvasViewport();
5223 _updateCanvasViewport: function () {
5224 // don't redraw while zooming. See _updateSvgViewport for more details
5225 if (this._pathZooming) { return; }
5226 this._updatePathViewport();
5228 var vp = this._pathViewport,
5230 size = vp.max.subtract(min),
5231 root = this._pathRoot;
5233 //TODO check if this works properly on mobile webkit
5234 L.DomUtil.setPosition(root, min);
5235 root.width = size.x;
5236 root.height = size.y;
5237 root.getContext('2d').translate(-min.x, -min.y);
5243 * L.LineUtil contains different utility functions for line segments
5244 * and polylines (clipping, simplification, distances, etc.)
5247 /*jshint bitwise:false */ // allow bitwise operations for this file
5251 // Simplify polyline with vertex reduction and Douglas-Peucker simplification.
5252 // Improves rendering performance dramatically by lessening the number of points to draw.
5254 simplify: function (/*Point[]*/ points, /*Number*/ tolerance) {
5255 if (!tolerance || !points.length) {
5256 return points.slice();
5259 var sqTolerance = tolerance * tolerance;
5261 // stage 1: vertex reduction
5262 points = this._reducePoints(points, sqTolerance);
5264 // stage 2: Douglas-Peucker simplification
5265 points = this._simplifyDP(points, sqTolerance);
5270 // distance from a point to a segment between two points
5271 pointToSegmentDistance: function (/*Point*/ p, /*Point*/ p1, /*Point*/ p2) {
5272 return Math.sqrt(this._sqClosestPointOnSegment(p, p1, p2, true));
5275 closestPointOnSegment: function (/*Point*/ p, /*Point*/ p1, /*Point*/ p2) {
5276 return this._sqClosestPointOnSegment(p, p1, p2);
5279 // Douglas-Peucker simplification, see http://en.wikipedia.org/wiki/Douglas-Peucker_algorithm
5280 _simplifyDP: function (points, sqTolerance) {
5282 var len = points.length,
5283 ArrayConstructor = typeof Uint8Array !== undefined + '' ? Uint8Array : Array,
5284 markers = new ArrayConstructor(len);
5286 markers[0] = markers[len - 1] = 1;
5288 this._simplifyDPStep(points, markers, sqTolerance, 0, len - 1);
5293 for (i = 0; i < len; i++) {
5295 newPoints.push(points[i]);
5302 _simplifyDPStep: function (points, markers, sqTolerance, first, last) {
5307 for (i = first + 1; i <= last - 1; i++) {
5308 sqDist = this._sqClosestPointOnSegment(points[i], points[first], points[last], true);
5310 if (sqDist > maxSqDist) {
5316 if (maxSqDist > sqTolerance) {
5319 this._simplifyDPStep(points, markers, sqTolerance, first, index);
5320 this._simplifyDPStep(points, markers, sqTolerance, index, last);
5324 // reduce points that are too close to each other to a single point
5325 _reducePoints: function (points, sqTolerance) {
5326 var reducedPoints = [points[0]];
5328 for (var i = 1, prev = 0, len = points.length; i < len; i++) {
5329 if (this._sqDist(points[i], points[prev]) > sqTolerance) {
5330 reducedPoints.push(points[i]);
5334 if (prev < len - 1) {
5335 reducedPoints.push(points[len - 1]);
5337 return reducedPoints;
5340 // Cohen-Sutherland line clipping algorithm.
5341 // Used to avoid rendering parts of a polyline that are not currently visible.
5343 clipSegment: function (a, b, bounds, useLastCode) {
5344 var codeA = useLastCode ? this._lastCode : this._getBitCode(a, bounds),
5345 codeB = this._getBitCode(b, bounds),
5347 codeOut, p, newCode;
5349 // save 2nd code to avoid calculating it on the next segment
5350 this._lastCode = codeB;
5353 // if a,b is inside the clip window (trivial accept)
5354 if (!(codeA | codeB)) {
5356 // if a,b is outside the clip window (trivial reject)
5357 } else if (codeA & codeB) {
5361 codeOut = codeA || codeB;
5362 p = this._getEdgeIntersection(a, b, codeOut, bounds);
5363 newCode = this._getBitCode(p, bounds);
5365 if (codeOut === codeA) {
5376 _getEdgeIntersection: function (a, b, code, bounds) {
5382 if (code & 8) { // top
5383 return new L.Point(a.x + dx * (max.y - a.y) / dy, max.y);
5384 } else if (code & 4) { // bottom
5385 return new L.Point(a.x + dx * (min.y - a.y) / dy, min.y);
5386 } else if (code & 2) { // right
5387 return new L.Point(max.x, a.y + dy * (max.x - a.x) / dx);
5388 } else if (code & 1) { // left
5389 return new L.Point(min.x, a.y + dy * (min.x - a.x) / dx);
5393 _getBitCode: function (/*Point*/ p, bounds) {
5396 if (p.x < bounds.min.x) { // left
5398 } else if (p.x > bounds.max.x) { // right
5401 if (p.y < bounds.min.y) { // bottom
5403 } else if (p.y > bounds.max.y) { // top
5410 // square distance (to avoid unnecessary Math.sqrt calls)
5411 _sqDist: function (p1, p2) {
5412 var dx = p2.x - p1.x,
5414 return dx * dx + dy * dy;
5417 // return closest point on segment or distance to that point
5418 _sqClosestPointOnSegment: function (p, p1, p2, sqDist) {
5423 dot = dx * dx + dy * dy,
5427 t = ((p.x - x) * dx + (p.y - y) * dy) / dot;
5441 return sqDist ? dx * dx + dy * dy : new L.Point(x, y);
5447 * L.Polyline is used to display polylines on a map.
5450 L.Polyline = L.Path.extend({
5451 initialize: function (latlngs, options) {
5452 L.Path.prototype.initialize.call(this, options);
5454 this._latlngs = this._convertLatLngs(latlngs);
5458 // how much to simplify the polyline on each zoom level
5459 // more = better performance and smoother look, less = more accurate
5464 projectLatlngs: function () {
5465 this._originalPoints = [];
5467 for (var i = 0, len = this._latlngs.length; i < len; i++) {
5468 this._originalPoints[i] = this._map.latLngToLayerPoint(this._latlngs[i]);
5472 getPathString: function () {
5473 for (var i = 0, len = this._parts.length, str = ''; i < len; i++) {
5474 str += this._getPathPartStr(this._parts[i]);
5479 getLatLngs: function () {
5480 return this._latlngs;
5483 setLatLngs: function (latlngs) {
5484 this._latlngs = this._convertLatLngs(latlngs);
5485 return this.redraw();
5488 addLatLng: function (latlng) {
5489 this._latlngs.push(L.latLng(latlng));
5490 return this.redraw();
5493 spliceLatLngs: function () { // (Number index, Number howMany)
5494 var removed = [].splice.apply(this._latlngs, arguments);
5495 this._convertLatLngs(this._latlngs, true);
5500 closestLayerPoint: function (p) {
5501 var minDistance = Infinity, parts = this._parts, p1, p2, minPoint = null;
5503 for (var j = 0, jLen = parts.length; j < jLen; j++) {
5504 var points = parts[j];
5505 for (var i = 1, len = points.length; i < len; i++) {
5508 var sqDist = L.LineUtil._sqClosestPointOnSegment(p, p1, p2, true);
5509 if (sqDist < minDistance) {
5510 minDistance = sqDist;
5511 minPoint = L.LineUtil._sqClosestPointOnSegment(p, p1, p2);
5516 minPoint.distance = Math.sqrt(minDistance);
5521 getBounds: function () {
5522 return new L.LatLngBounds(this.getLatLngs());
5525 _convertLatLngs: function (latlngs, overwrite) {
5526 var i, len, target = overwrite ? latlngs : [];
5528 for (i = 0, len = latlngs.length; i < len; i++) {
5529 if (L.Util.isArray(latlngs[i]) && typeof latlngs[i][0] !== 'number') {
5532 target[i] = L.latLng(latlngs[i]);
5537 _initEvents: function () {
5538 L.Path.prototype._initEvents.call(this);
5541 _getPathPartStr: function (points) {
5542 var round = L.Path.VML;
5544 for (var j = 0, len2 = points.length, str = '', p; j < len2; j++) {
5549 str += (j ? 'L' : 'M') + p.x + ' ' + p.y;
5554 _clipPoints: function () {
5555 var points = this._originalPoints,
5556 len = points.length,
5559 if (this.options.noClip) {
5560 this._parts = [points];
5566 var parts = this._parts,
5567 vp = this._map._pathViewport,
5570 for (i = 0, k = 0; i < len - 1; i++) {
5571 segment = lu.clipSegment(points[i], points[i + 1], vp, i);
5576 parts[k] = parts[k] || [];
5577 parts[k].push(segment[0]);
5579 // if segment goes out of screen, or it's the last one, it's the end of the line part
5580 if ((segment[1] !== points[i + 1]) || (i === len - 2)) {
5581 parts[k].push(segment[1]);
5587 // simplify each clipped part of the polyline
5588 _simplifyPoints: function () {
5589 var parts = this._parts,
5592 for (var i = 0, len = parts.length; i < len; i++) {
5593 parts[i] = lu.simplify(parts[i], this.options.smoothFactor);
5597 _updatePath: function () {
5598 if (!this._map) { return; }
5601 this._simplifyPoints();
5603 L.Path.prototype._updatePath.call(this);
5607 L.polyline = function (latlngs, options) {
5608 return new L.Polyline(latlngs, options);
5613 * L.PolyUtil contains utility functions for polygons (clipping, etc.).
5616 /*jshint bitwise:false */ // allow bitwise operations here
5621 * Sutherland-Hodgeman polygon clipping algorithm.
5622 * Used to avoid rendering parts of a polygon that are not currently visible.
5624 L.PolyUtil.clipPolygon = function (points, bounds) {
5626 edges = [1, 4, 2, 8],
5632 for (i = 0, len = points.length; i < len; i++) {
5633 points[i]._code = lu._getBitCode(points[i], bounds);
5636 // for each edge (left, bottom, right, top)
5637 for (k = 0; k < 4; k++) {
5641 for (i = 0, len = points.length, j = len - 1; i < len; j = i++) {
5645 // if a is inside the clip window
5646 if (!(a._code & edge)) {
5647 // if b is outside the clip window (a->b goes out of screen)
5648 if (b._code & edge) {
5649 p = lu._getEdgeIntersection(b, a, edge, bounds);
5650 p._code = lu._getBitCode(p, bounds);
5651 clippedPoints.push(p);
5653 clippedPoints.push(a);
5655 // else if b is inside the clip window (a->b enters the screen)
5656 } else if (!(b._code & edge)) {
5657 p = lu._getEdgeIntersection(b, a, edge, bounds);
5658 p._code = lu._getBitCode(p, bounds);
5659 clippedPoints.push(p);
5662 points = clippedPoints;
5670 * L.Polygon is used to display polygons on a map.
5673 L.Polygon = L.Polyline.extend({
5678 initialize: function (latlngs, options) {
5679 L.Polyline.prototype.initialize.call(this, latlngs, options);
5680 this._initWithHoles(latlngs);
5683 _initWithHoles: function (latlngs) {
5685 if (latlngs && L.Util.isArray(latlngs[0]) && (typeof latlngs[0][0] !== 'number')) {
5686 this._latlngs = this._convertLatLngs(latlngs[0]);
5687 this._holes = latlngs.slice(1);
5689 for (i = 0, len = this._holes.length; i < len; i++) {
5690 hole = this._holes[i] = this._convertLatLngs(this._holes[i]);
5691 if (hole[0].equals(hole[hole.length - 1])) {
5697 // filter out last point if its equal to the first one
5698 latlngs = this._latlngs;
5700 if (latlngs.length >= 2 && latlngs[0].equals(latlngs[latlngs.length - 1])) {
5705 projectLatlngs: function () {
5706 L.Polyline.prototype.projectLatlngs.call(this);
5708 // project polygon holes points
5709 // TODO move this logic to Polyline to get rid of duplication
5710 this._holePoints = [];
5712 if (!this._holes) { return; }
5714 var i, j, len, len2;
5716 for (i = 0, len = this._holes.length; i < len; i++) {
5717 this._holePoints[i] = [];
5719 for (j = 0, len2 = this._holes[i].length; j < len2; j++) {
5720 this._holePoints[i][j] = this._map.latLngToLayerPoint(this._holes[i][j]);
5725 setLatLngs: function (latlngs) {
5726 if (latlngs && L.Util.isArray(latlngs[0]) && (typeof latlngs[0][0] !== 'number')) {
5727 this._initWithHoles(latlngs);
5728 return this.redraw();
5730 return L.Polyline.prototype.setLatLngs.call(this, latlngs);
5734 _clipPoints: function () {
5735 var points = this._originalPoints,
5738 this._parts = [points].concat(this._holePoints);
5740 if (this.options.noClip) { return; }
5742 for (var i = 0, len = this._parts.length; i < len; i++) {
5743 var clipped = L.PolyUtil.clipPolygon(this._parts[i], this._map._pathViewport);
5744 if (clipped.length) {
5745 newParts.push(clipped);
5749 this._parts = newParts;
5752 _getPathPartStr: function (points) {
5753 var str = L.Polyline.prototype._getPathPartStr.call(this, points);
5754 return str + (L.Browser.svg ? 'z' : 'x');
5758 L.polygon = function (latlngs, options) {
5759 return new L.Polygon(latlngs, options);
5764 * Contains L.MultiPolyline and L.MultiPolygon layers.
5768 function createMulti(Klass) {
5770 return L.FeatureGroup.extend({
5772 initialize: function (latlngs, options) {
5774 this._options = options;
5775 this.setLatLngs(latlngs);
5778 setLatLngs: function (latlngs) {
5780 len = latlngs.length;
5782 this.eachLayer(function (layer) {
5784 layer.setLatLngs(latlngs[i++]);
5786 this.removeLayer(layer);
5791 this.addLayer(new Klass(latlngs[i++], this._options));
5797 getLatLngs: function () {
5800 this.eachLayer(function (layer) {
5801 latlngs.push(layer.getLatLngs());
5809 L.MultiPolyline = createMulti(L.Polyline);
5810 L.MultiPolygon = createMulti(L.Polygon);
5812 L.multiPolyline = function (latlngs, options) {
5813 return new L.MultiPolyline(latlngs, options);
5816 L.multiPolygon = function (latlngs, options) {
5817 return new L.MultiPolygon(latlngs, options);
5823 * L.Rectangle extends Polygon and creates a rectangle when passed a LatLngBounds object.
5826 L.Rectangle = L.Polygon.extend({
5827 initialize: function (latLngBounds, options) {
5828 L.Polygon.prototype.initialize.call(this, this._boundsToLatLngs(latLngBounds), options);
5831 setBounds: function (latLngBounds) {
5832 this.setLatLngs(this._boundsToLatLngs(latLngBounds));
5835 _boundsToLatLngs: function (latLngBounds) {
5836 latLngBounds = L.latLngBounds(latLngBounds);
5838 latLngBounds.getSouthWest(),
5839 latLngBounds.getNorthWest(),
5840 latLngBounds.getNorthEast(),
5841 latLngBounds.getSouthEast()
5846 L.rectangle = function (latLngBounds, options) {
5847 return new L.Rectangle(latLngBounds, options);
5852 * L.Circle is a circle overlay (with a certain radius in meters).
5855 L.Circle = L.Path.extend({
5856 initialize: function (latlng, radius, options) {
5857 L.Path.prototype.initialize.call(this, options);
5859 this._latlng = L.latLng(latlng);
5860 this._mRadius = radius;
5867 setLatLng: function (latlng) {
5868 this._latlng = L.latLng(latlng);
5869 return this.redraw();
5872 setRadius: function (radius) {
5873 this._mRadius = radius;
5874 return this.redraw();
5877 projectLatlngs: function () {
5878 var lngRadius = this._getLngRadius(),
5879 latlng = this._latlng,
5880 pointLeft = this._map.latLngToLayerPoint([latlng.lat, latlng.lng - lngRadius]);
5882 this._point = this._map.latLngToLayerPoint(latlng);
5883 this._radius = Math.max(this._point.x - pointLeft.x, 1);
5886 getBounds: function () {
5887 var lngRadius = this._getLngRadius(),
5888 latRadius = (this._mRadius / 40075017) * 360,
5889 latlng = this._latlng;
5891 return new L.LatLngBounds(
5892 [latlng.lat - latRadius, latlng.lng - lngRadius],
5893 [latlng.lat + latRadius, latlng.lng + lngRadius]);
5896 getLatLng: function () {
5897 return this._latlng;
5900 getPathString: function () {
5901 var p = this._point,
5904 if (this._checkIfEmpty()) {
5908 if (L.Browser.svg) {
5909 return 'M' + p.x + ',' + (p.y - r) +
5910 'A' + r + ',' + r + ',0,1,1,' +
5911 (p.x - 0.1) + ',' + (p.y - r) + ' z';
5915 return 'AL ' + p.x + ',' + p.y + ' ' + r + ',' + r + ' 0,' + (65535 * 360);
5919 getRadius: function () {
5920 return this._mRadius;
5923 // TODO Earth hardcoded, move into projection code!
5925 _getLatRadius: function () {
5926 return (this._mRadius / 40075017) * 360;
5929 _getLngRadius: function () {
5930 return this._getLatRadius() / Math.cos(L.LatLng.DEG_TO_RAD * this._latlng.lat);
5933 _checkIfEmpty: function () {
5937 var vp = this._map._pathViewport,
5941 return p.x - r > vp.max.x || p.y - r > vp.max.y ||
5942 p.x + r < vp.min.x || p.y + r < vp.min.y;
5946 L.circle = function (latlng, radius, options) {
5947 return new L.Circle(latlng, radius, options);
5952 * L.CircleMarker is a circle overlay with a permanent pixel radius.
5955 L.CircleMarker = L.Circle.extend({
5961 initialize: function (latlng, options) {
5962 L.Circle.prototype.initialize.call(this, latlng, null, options);
5963 this._radius = this.options.radius;
5966 projectLatlngs: function () {
5967 this._point = this._map.latLngToLayerPoint(this._latlng);
5970 _updateStyle : function () {
5971 L.Circle.prototype._updateStyle.call(this);
5972 this.setRadius(this.options.radius);
5975 setLatLng: function (latlng) {
5976 L.Circle.prototype.setLatLng.call(this, latlng);
5977 if (this._popup && this._popup._isOpen) {
5978 this._popup.setLatLng(latlng);
5983 setRadius: function (radius) {
5984 this.options.radius = this._radius = radius;
5985 return this.redraw();
5988 getRadius: function () {
5989 return this._radius;
5993 L.circleMarker = function (latlng, options) {
5994 return new L.CircleMarker(latlng, options);
5999 * Extends L.Polyline to be able to manually detect clicks on Canvas-rendered polylines.
6002 L.Polyline.include(!L.Path.CANVAS ? {} : {
6003 _containsPoint: function (p, closed) {
6004 var i, j, k, len, len2, dist, part,
6005 w = this.options.weight / 2;
6007 if (L.Browser.touch) {
6008 w += 10; // polyline click tolerance on touch devices
6011 for (i = 0, len = this._parts.length; i < len; i++) {
6012 part = this._parts[i];
6013 for (j = 0, len2 = part.length, k = len2 - 1; j < len2; k = j++) {
6014 if (!closed && (j === 0)) {
6018 dist = L.LineUtil.pointToSegmentDistance(p, part[k], part[j]);
6031 * Extends L.Polygon to be able to manually detect clicks on Canvas-rendered polygons.
6034 L.Polygon.include(!L.Path.CANVAS ? {} : {
6035 _containsPoint: function (p) {
6041 // TODO optimization: check if within bounds first
6043 if (L.Polyline.prototype._containsPoint.call(this, p, true)) {
6044 // click on polygon border
6048 // ray casting algorithm for detecting if point is in polygon
6050 for (i = 0, len = this._parts.length; i < len; i++) {
6051 part = this._parts[i];
6053 for (j = 0, len2 = part.length, k = len2 - 1; j < len2; k = j++) {
6057 if (((p1.y > p.y) !== (p2.y > p.y)) &&
6058 (p.x < (p2.x - p1.x) * (p.y - p1.y) / (p2.y - p1.y) + p1.x)) {
6070 * Extends L.Circle with Canvas-specific code.
6073 L.Circle.include(!L.Path.CANVAS ? {} : {
6074 _drawPath: function () {
6075 var p = this._point;
6076 this._ctx.beginPath();
6077 this._ctx.arc(p.x, p.y, this._radius, 0, Math.PI * 2, false);
6080 _containsPoint: function (p) {
6081 var center = this._point,
6082 w2 = this.options.stroke ? this.options.weight / 2 : 0;
6084 return (p.distanceTo(center) <= this._radius + w2);
6090 * CircleMarker canvas specific drawing parts.
6093 L.CircleMarker.include(!L.Path.CANVAS ? {} : {
6094 _updateStyle: function () {
6095 L.Path.prototype._updateStyle.call(this);
6101 * L.GeoJSON turns any GeoJSON data into a Leaflet layer.
6104 L.GeoJSON = L.FeatureGroup.extend({
6106 initialize: function (geojson, options) {
6107 L.setOptions(this, options);
6112 this.addData(geojson);
6116 addData: function (geojson) {
6117 var features = L.Util.isArray(geojson) ? geojson : geojson.features,
6121 for (i = 0, len = features.length; i < len; i++) {
6122 // Only add this if geometry or geometries are set and not null
6123 feature = features[i];
6124 if (feature.geometries || feature.geometry || feature.features || feature.coordinates) {
6125 this.addData(features[i]);
6131 var options = this.options;
6133 if (options.filter && !options.filter(geojson)) { return; }
6135 var layer = L.GeoJSON.geometryToLayer(geojson, options.pointToLayer, options.coordsToLatLng, options);
6136 layer.feature = L.GeoJSON.asFeature(geojson);
6138 layer.defaultOptions = layer.options;
6139 this.resetStyle(layer);
6141 if (options.onEachFeature) {
6142 options.onEachFeature(geojson, layer);
6145 return this.addLayer(layer);
6148 resetStyle: function (layer) {
6149 var style = this.options.style;
6151 // reset any custom styles
6152 L.Util.extend(layer.options, layer.defaultOptions);
6154 this._setLayerStyle(layer, style);
6158 setStyle: function (style) {
6159 this.eachLayer(function (layer) {
6160 this._setLayerStyle(layer, style);
6164 _setLayerStyle: function (layer, style) {
6165 if (typeof style === 'function') {
6166 style = style(layer.feature);
6168 if (layer.setStyle) {
6169 layer.setStyle(style);
6174 L.extend(L.GeoJSON, {
6175 geometryToLayer: function (geojson, pointToLayer, coordsToLatLng, vectorOptions) {
6176 var geometry = geojson.type === 'Feature' ? geojson.geometry : geojson,
6177 coords = geometry.coordinates,
6179 latlng, latlngs, i, len;
6181 coordsToLatLng = coordsToLatLng || this.coordsToLatLng;
6183 switch (geometry.type) {
6185 latlng = coordsToLatLng(coords);
6186 return pointToLayer ? pointToLayer(geojson, latlng) : new L.Marker(latlng);
6189 for (i = 0, len = coords.length; i < len; i++) {
6190 latlng = coordsToLatLng(coords[i]);
6191 layers.push(pointToLayer ? pointToLayer(geojson, latlng) : new L.Marker(latlng));
6193 return new L.FeatureGroup(layers);
6196 latlngs = this.coordsToLatLngs(coords, 0, coordsToLatLng);
6197 return new L.Polyline(latlngs, vectorOptions);
6200 if (coords.length === 2 && !coords[1].length) {
6201 throw new Error('Invalid GeoJSON object.');
6203 latlngs = this.coordsToLatLngs(coords, 1, coordsToLatLng);
6204 return new L.Polygon(latlngs, vectorOptions);
6206 case 'MultiLineString':
6207 latlngs = this.coordsToLatLngs(coords, 1, coordsToLatLng);
6208 return new L.MultiPolyline(latlngs, vectorOptions);
6210 case 'MultiPolygon':
6211 latlngs = this.coordsToLatLngs(coords, 2, coordsToLatLng);
6212 return new L.MultiPolygon(latlngs, vectorOptions);
6214 case 'GeometryCollection':
6215 for (i = 0, len = geometry.geometries.length; i < len; i++) {
6217 layers.push(this.geometryToLayer({
6218 geometry: geometry.geometries[i],
6220 properties: geojson.properties
6221 }, pointToLayer, coordsToLatLng, vectorOptions));
6223 return new L.FeatureGroup(layers);
6226 throw new Error('Invalid GeoJSON object.');
6230 coordsToLatLng: function (coords) { // (Array[, Boolean]) -> LatLng
6231 return new L.LatLng(coords[1], coords[0], coords[2]);
6234 coordsToLatLngs: function (coords, levelsDeep, coordsToLatLng) { // (Array[, Number, Function]) -> Array
6238 for (i = 0, len = coords.length; i < len; i++) {
6239 latlng = levelsDeep ?
6240 this.coordsToLatLngs(coords[i], levelsDeep - 1, coordsToLatLng) :
6241 (coordsToLatLng || this.coordsToLatLng)(coords[i]);
6243 latlngs.push(latlng);
6249 latLngToCoords: function (latlng) {
6250 var coords = [latlng.lng, latlng.lat];
6252 if (latlng.alt !== undefined) {
6253 coords.push(latlng.alt);
6258 latLngsToCoords: function (latLngs) {
6261 for (var i = 0, len = latLngs.length; i < len; i++) {
6262 coords.push(L.GeoJSON.latLngToCoords(latLngs[i]));
6268 getFeature: function (layer, newGeometry) {
6269 return layer.feature ? L.extend({}, layer.feature, {geometry: newGeometry}) : L.GeoJSON.asFeature(newGeometry);
6272 asFeature: function (geoJSON) {
6273 if (geoJSON.type === 'Feature') {
6285 var PointToGeoJSON = {
6286 toGeoJSON: function () {
6287 return L.GeoJSON.getFeature(this, {
6289 coordinates: L.GeoJSON.latLngToCoords(this.getLatLng())
6294 L.Marker.include(PointToGeoJSON);
6295 L.Circle.include(PointToGeoJSON);
6296 L.CircleMarker.include(PointToGeoJSON);
6298 L.Polyline.include({
6299 toGeoJSON: function () {
6300 return L.GeoJSON.getFeature(this, {
6302 coordinates: L.GeoJSON.latLngsToCoords(this.getLatLngs())
6308 toGeoJSON: function () {
6309 var coords = [L.GeoJSON.latLngsToCoords(this.getLatLngs())],
6312 coords[0].push(coords[0][0]);
6315 for (i = 0, len = this._holes.length; i < len; i++) {
6316 hole = L.GeoJSON.latLngsToCoords(this._holes[i]);
6322 return L.GeoJSON.getFeature(this, {
6330 function multiToGeoJSON(type) {
6331 return function () {
6334 this.eachLayer(function (layer) {
6335 coords.push(layer.toGeoJSON().geometry.coordinates);
6338 return L.GeoJSON.getFeature(this, {
6345 L.MultiPolyline.include({toGeoJSON: multiToGeoJSON('MultiLineString')});
6346 L.MultiPolygon.include({toGeoJSON: multiToGeoJSON('MultiPolygon')});
6348 L.LayerGroup.include({
6349 toGeoJSON: function () {
6351 var geometry = this.feature && this.feature.geometry,
6355 if (geometry && geometry.type === 'MultiPoint') {
6356 return multiToGeoJSON('MultiPoint').call(this);
6359 var isGeometryCollection = geometry && geometry.type === 'GeometryCollection';
6361 this.eachLayer(function (layer) {
6362 if (layer.toGeoJSON) {
6363 json = layer.toGeoJSON();
6364 jsons.push(isGeometryCollection ? json.geometry : L.GeoJSON.asFeature(json));
6368 if (isGeometryCollection) {
6369 return L.GeoJSON.getFeature(this, {
6371 type: 'GeometryCollection'
6376 type: 'FeatureCollection',
6383 L.geoJson = function (geojson, options) {
6384 return new L.GeoJSON(geojson, options);
6389 * L.DomEvent contains functions for working with DOM events.
6393 /* inspired by John Resig, Dean Edwards and YUI addEvent implementations */
6394 addListener: function (obj, type, fn, context) { // (HTMLElement, String, Function[, Object])
6396 var id = L.stamp(fn),
6397 key = '_leaflet_' + type + id,
6398 handler, originalHandler, newType;
6400 if (obj[key]) { return this; }
6402 handler = function (e) {
6403 return fn.call(context || obj, e || L.DomEvent._getEvent());
6406 if (L.Browser.pointer && type.indexOf('touch') === 0) {
6407 return this.addPointerListener(obj, type, handler, id);
6409 if (L.Browser.touch && (type === 'dblclick') && this.addDoubleTapListener) {
6410 this.addDoubleTapListener(obj, handler, id);
6413 if ('addEventListener' in obj) {
6415 if (type === 'mousewheel') {
6416 obj.addEventListener('DOMMouseScroll', handler, false);
6417 obj.addEventListener(type, handler, false);
6419 } else if ((type === 'mouseenter') || (type === 'mouseleave')) {
6421 originalHandler = handler;
6422 newType = (type === 'mouseenter' ? 'mouseover' : 'mouseout');
6424 handler = function (e) {
6425 if (!L.DomEvent._checkMouse(obj, e)) { return; }
6426 return originalHandler(e);
6429 obj.addEventListener(newType, handler, false);
6431 } else if (type === 'click' && L.Browser.android) {
6432 originalHandler = handler;
6433 handler = function (e) {
6434 return L.DomEvent._filterClick(e, originalHandler);
6437 obj.addEventListener(type, handler, false);
6439 obj.addEventListener(type, handler, false);
6442 } else if ('attachEvent' in obj) {
6443 obj.attachEvent('on' + type, handler);
6451 removeListener: function (obj, type, fn) { // (HTMLElement, String, Function)
6453 var id = L.stamp(fn),
6454 key = '_leaflet_' + type + id,
6457 if (!handler) { return this; }
6459 if (L.Browser.pointer && type.indexOf('touch') === 0) {
6460 this.removePointerListener(obj, type, id);
6461 } else if (L.Browser.touch && (type === 'dblclick') && this.removeDoubleTapListener) {
6462 this.removeDoubleTapListener(obj, id);
6464 } else if ('removeEventListener' in obj) {
6466 if (type === 'mousewheel') {
6467 obj.removeEventListener('DOMMouseScroll', handler, false);
6468 obj.removeEventListener(type, handler, false);
6470 } else if ((type === 'mouseenter') || (type === 'mouseleave')) {
6471 obj.removeEventListener((type === 'mouseenter' ? 'mouseover' : 'mouseout'), handler, false);
6473 obj.removeEventListener(type, handler, false);
6475 } else if ('detachEvent' in obj) {
6476 obj.detachEvent('on' + type, handler);
6484 stopPropagation: function (e) {
6486 if (e.stopPropagation) {
6487 e.stopPropagation();
6489 e.cancelBubble = true;
6491 L.DomEvent._skipped(e);
6496 disableScrollPropagation: function (el) {
6497 var stop = L.DomEvent.stopPropagation;
6500 .on(el, 'mousewheel', stop)
6501 .on(el, 'MozMousePixelScroll', stop);
6504 disableClickPropagation: function (el) {
6505 var stop = L.DomEvent.stopPropagation;
6507 for (var i = L.Draggable.START.length - 1; i >= 0; i--) {
6508 L.DomEvent.on(el, L.Draggable.START[i], stop);
6512 .on(el, 'click', L.DomEvent._fakeStop)
6513 .on(el, 'dblclick', stop);
6516 preventDefault: function (e) {
6518 if (e.preventDefault) {
6521 e.returnValue = false;
6526 stop: function (e) {
6529 .stopPropagation(e);
6532 getMousePosition: function (e, container) {
6534 return new L.Point(e.clientX, e.clientY);
6537 var rect = container.getBoundingClientRect();
6540 e.clientX - rect.left - container.clientLeft,
6541 e.clientY - rect.top - container.clientTop);
6544 getWheelDelta: function (e) {
6549 delta = e.wheelDelta / 120;
6552 delta = -e.detail / 3;
6559 _fakeStop: function (e) {
6560 // fakes stopPropagation by setting a special event flag, checked/reset with L.DomEvent._skipped(e)
6561 L.DomEvent._skipEvents[e.type] = true;
6564 _skipped: function (e) {
6565 var skipped = this._skipEvents[e.type];
6566 // reset when checking, as it's only used in map container and propagates outside of the map
6567 this._skipEvents[e.type] = false;
6571 // check if element really left/entered the event target (for mouseenter/mouseleave)
6572 _checkMouse: function (el, e) {
6574 var related = e.relatedTarget;
6576 if (!related) { return true; }
6579 while (related && (related !== el)) {
6580 related = related.parentNode;
6585 return (related !== el);
6588 _getEvent: function () { // evil magic for IE
6589 /*jshint noarg:false */
6590 var e = window.event;
6592 var caller = arguments.callee.caller;
6594 e = caller['arguments'][0];
6595 if (e && window.Event === e.constructor) {
6598 caller = caller.caller;
6604 // this is a horrible workaround for a bug in Android where a single touch triggers two click events
6605 _filterClick: function (e, handler) {
6606 var timeStamp = (e.timeStamp || e.originalEvent.timeStamp),
6607 elapsed = L.DomEvent._lastClick && (timeStamp - L.DomEvent._lastClick);
6609 // are they closer together than 500ms yet more than 100ms?
6610 // Android typically triggers them ~300ms apart while multiple listeners
6611 // on the same event should be triggered far faster;
6612 // or check if click is simulated on the element, and if it is, reject any non-simulated events
6614 if ((elapsed && elapsed > 100 && elapsed < 500) || (e.target._simulatedClick && !e._simulated)) {
6618 L.DomEvent._lastClick = timeStamp;
6624 L.DomEvent.on = L.DomEvent.addListener;
6625 L.DomEvent.off = L.DomEvent.removeListener;
6629 * L.Draggable allows you to add dragging capabilities to any element. Supports mobile devices too.
6632 L.Draggable = L.Class.extend({
6633 includes: L.Mixin.Events,
6636 START: L.Browser.touch ? ['touchstart', 'mousedown'] : ['mousedown'],
6638 mousedown: 'mouseup',
6639 touchstart: 'touchend',
6640 pointerdown: 'touchend',
6641 MSPointerDown: 'touchend'
6644 mousedown: 'mousemove',
6645 touchstart: 'touchmove',
6646 pointerdown: 'touchmove',
6647 MSPointerDown: 'touchmove'
6651 initialize: function (element, dragStartTarget) {
6652 this._element = element;
6653 this._dragStartTarget = dragStartTarget || element;
6656 enable: function () {
6657 if (this._enabled) { return; }
6659 for (var i = L.Draggable.START.length - 1; i >= 0; i--) {
6660 L.DomEvent.on(this._dragStartTarget, L.Draggable.START[i], this._onDown, this);
6663 this._enabled = true;
6666 disable: function () {
6667 if (!this._enabled) { return; }
6669 for (var i = L.Draggable.START.length - 1; i >= 0; i--) {
6670 L.DomEvent.off(this._dragStartTarget, L.Draggable.START[i], this._onDown, this);
6673 this._enabled = false;
6674 this._moved = false;
6677 _onDown: function (e) {
6678 this._moved = false;
6680 if (e.shiftKey || ((e.which !== 1) && (e.button !== 1) && !e.touches)) { return; }
6682 L.DomEvent.stopPropagation(e);
6684 if (L.Draggable._disabled) { return; }
6686 L.DomUtil.disableImageDrag();
6687 L.DomUtil.disableTextSelection();
6689 if (this._moving) { return; }
6691 var first = e.touches ? e.touches[0] : e;
6693 this._startPoint = new L.Point(first.clientX, first.clientY);
6694 this._startPos = this._newPos = L.DomUtil.getPosition(this._element);
6697 .on(document, L.Draggable.MOVE[e.type], this._onMove, this)
6698 .on(document, L.Draggable.END[e.type], this._onUp, this);
6701 _onMove: function (e) {
6702 if (e.touches && e.touches.length > 1) {
6707 var first = (e.touches && e.touches.length === 1 ? e.touches[0] : e),
6708 newPoint = new L.Point(first.clientX, first.clientY),
6709 offset = newPoint.subtract(this._startPoint);
6711 if (!offset.x && !offset.y) { return; }
6712 if (L.Browser.touch && Math.abs(offset.x) + Math.abs(offset.y) < 3) { return; }
6714 L.DomEvent.preventDefault(e);
6717 this.fire('dragstart');
6720 this._startPos = L.DomUtil.getPosition(this._element).subtract(offset);
6722 L.DomUtil.addClass(document.body, 'leaflet-dragging');
6723 this._lastTarget = e.target || e.srcElement;
6724 L.DomUtil.addClass(this._lastTarget, 'leaflet-drag-target');
6727 this._newPos = this._startPos.add(offset);
6728 this._moving = true;
6730 L.Util.cancelAnimFrame(this._animRequest);
6731 this._animRequest = L.Util.requestAnimFrame(this._updatePosition, this, true, this._dragStartTarget);
6734 _updatePosition: function () {
6735 this.fire('predrag');
6736 L.DomUtil.setPosition(this._element, this._newPos);
6740 _onUp: function () {
6741 L.DomUtil.removeClass(document.body, 'leaflet-dragging');
6743 if (this._lastTarget) {
6744 L.DomUtil.removeClass(this._lastTarget, 'leaflet-drag-target');
6745 this._lastTarget = null;
6748 for (var i in L.Draggable.MOVE) {
6750 .off(document, L.Draggable.MOVE[i], this._onMove)
6751 .off(document, L.Draggable.END[i], this._onUp);
6754 L.DomUtil.enableImageDrag();
6755 L.DomUtil.enableTextSelection();
6757 if (this._moved && this._moving) {
6758 // ensure drag is not fired after dragend
6759 L.Util.cancelAnimFrame(this._animRequest);
6761 this.fire('dragend', {
6762 distance: this._newPos.distanceTo(this._startPos)
6766 this._moving = false;
6772 L.Handler is a base class for handler classes that are used internally to inject
6773 interaction features like dragging to classes like Map and Marker.
6776 L.Handler = L.Class.extend({
6777 initialize: function (map) {
6781 enable: function () {
6782 if (this._enabled) { return; }
6784 this._enabled = true;
6788 disable: function () {
6789 if (!this._enabled) { return; }
6791 this._enabled = false;
6795 enabled: function () {
6796 return !!this._enabled;
6802 * L.Handler.MapDrag is used to make the map draggable (with panning inertia), enabled by default.
6805 L.Map.mergeOptions({
6808 inertia: !L.Browser.android23,
6809 inertiaDeceleration: 3400, // px/s^2
6810 inertiaMaxSpeed: Infinity, // px/s
6811 inertiaThreshold: L.Browser.touch ? 32 : 18, // ms
6812 easeLinearity: 0.25,
6814 // TODO refactor, move to CRS
6815 worldCopyJump: false
6818 L.Map.Drag = L.Handler.extend({
6819 addHooks: function () {
6820 if (!this._draggable) {
6821 var map = this._map;
6823 this._draggable = new L.Draggable(map._mapPane, map._container);
6825 this._draggable.on({
6826 'dragstart': this._onDragStart,
6827 'drag': this._onDrag,
6828 'dragend': this._onDragEnd
6831 if (map.options.worldCopyJump) {
6832 this._draggable.on('predrag', this._onPreDrag, this);
6833 map.on('viewreset', this._onViewReset, this);
6835 map.whenReady(this._onViewReset, this);
6838 this._draggable.enable();
6841 removeHooks: function () {
6842 this._draggable.disable();
6845 moved: function () {
6846 return this._draggable && this._draggable._moved;
6849 _onDragStart: function () {
6850 var map = this._map;
6853 map._panAnim.stop();
6860 if (map.options.inertia) {
6861 this._positions = [];
6866 _onDrag: function () {
6867 if (this._map.options.inertia) {
6868 var time = this._lastTime = +new Date(),
6869 pos = this._lastPos = this._draggable._newPos;
6871 this._positions.push(pos);
6872 this._times.push(time);
6874 if (time - this._times[0] > 200) {
6875 this._positions.shift();
6876 this._times.shift();
6885 _onViewReset: function () {
6886 // TODO fix hardcoded Earth values
6887 var pxCenter = this._map.getSize()._divideBy(2),
6888 pxWorldCenter = this._map.latLngToLayerPoint([0, 0]);
6890 this._initialWorldOffset = pxWorldCenter.subtract(pxCenter).x;
6891 this._worldWidth = this._map.project([0, 180]).x;
6894 _onPreDrag: function () {
6895 // TODO refactor to be able to adjust map pane position after zoom
6896 var worldWidth = this._worldWidth,
6897 halfWidth = Math.round(worldWidth / 2),
6898 dx = this._initialWorldOffset,
6899 x = this._draggable._newPos.x,
6900 newX1 = (x - halfWidth + dx) % worldWidth + halfWidth - dx,
6901 newX2 = (x + halfWidth + dx) % worldWidth - halfWidth - dx,
6902 newX = Math.abs(newX1 + dx) < Math.abs(newX2 + dx) ? newX1 : newX2;
6904 this._draggable._newPos.x = newX;
6907 _onDragEnd: function (e) {
6908 var map = this._map,
6909 options = map.options,
6910 delay = +new Date() - this._lastTime,
6912 noInertia = !options.inertia || delay > options.inertiaThreshold || !this._positions[0];
6914 map.fire('dragend', e);
6917 map.fire('moveend');
6921 var direction = this._lastPos.subtract(this._positions[0]),
6922 duration = (this._lastTime + delay - this._times[0]) / 1000,
6923 ease = options.easeLinearity,
6925 speedVector = direction.multiplyBy(ease / duration),
6926 speed = speedVector.distanceTo([0, 0]),
6928 limitedSpeed = Math.min(options.inertiaMaxSpeed, speed),
6929 limitedSpeedVector = speedVector.multiplyBy(limitedSpeed / speed),
6931 decelerationDuration = limitedSpeed / (options.inertiaDeceleration * ease),
6932 offset = limitedSpeedVector.multiplyBy(-decelerationDuration / 2).round();
6934 if (!offset.x || !offset.y) {
6935 map.fire('moveend');
6938 offset = map._limitOffset(offset, map.options.maxBounds);
6940 L.Util.requestAnimFrame(function () {
6942 duration: decelerationDuration,
6943 easeLinearity: ease,
6952 L.Map.addInitHook('addHandler', 'dragging', L.Map.Drag);
6956 * L.Handler.DoubleClickZoom is used to handle double-click zoom on the map, enabled by default.
6959 L.Map.mergeOptions({
6960 doubleClickZoom: true
6963 L.Map.DoubleClickZoom = L.Handler.extend({
6964 addHooks: function () {
6965 this._map.on('dblclick', this._onDoubleClick, this);
6968 removeHooks: function () {
6969 this._map.off('dblclick', this._onDoubleClick, this);
6972 _onDoubleClick: function (e) {
6973 var map = this._map,
6974 zoom = map.getZoom() + (e.originalEvent.shiftKey ? -1 : 1);
6976 if (map.options.doubleClickZoom === 'center') {
6979 map.setZoomAround(e.containerPoint, zoom);
6984 L.Map.addInitHook('addHandler', 'doubleClickZoom', L.Map.DoubleClickZoom);
6988 * L.Handler.ScrollWheelZoom is used by L.Map to enable mouse scroll wheel zoom on the map.
6991 L.Map.mergeOptions({
6992 scrollWheelZoom: true
6995 L.Map.ScrollWheelZoom = L.Handler.extend({
6996 addHooks: function () {
6997 L.DomEvent.on(this._map._container, 'mousewheel', this._onWheelScroll, this);
6998 L.DomEvent.on(this._map._container, 'MozMousePixelScroll', L.DomEvent.preventDefault);
7002 removeHooks: function () {
7003 L.DomEvent.off(this._map._container, 'mousewheel', this._onWheelScroll);
7004 L.DomEvent.off(this._map._container, 'MozMousePixelScroll', L.DomEvent.preventDefault);
7007 _onWheelScroll: function (e) {
7008 var delta = L.DomEvent.getWheelDelta(e);
7010 this._delta += delta;
7011 this._lastMousePos = this._map.mouseEventToContainerPoint(e);
7013 if (!this._startTime) {
7014 this._startTime = +new Date();
7017 var left = Math.max(40 - (+new Date() - this._startTime), 0);
7019 clearTimeout(this._timer);
7020 this._timer = setTimeout(L.bind(this._performZoom, this), left);
7022 L.DomEvent.preventDefault(e);
7023 L.DomEvent.stopPropagation(e);
7026 _performZoom: function () {
7027 var map = this._map,
7028 delta = this._delta,
7029 zoom = map.getZoom();
7031 delta = delta > 0 ? Math.ceil(delta) : Math.floor(delta);
7032 delta = Math.max(Math.min(delta, 4), -4);
7033 delta = map._limitZoom(zoom + delta) - zoom;
7036 this._startTime = null;
7038 if (!delta) { return; }
7040 if (map.options.scrollWheelZoom === 'center') {
7041 map.setZoom(zoom + delta);
7043 map.setZoomAround(this._lastMousePos, zoom + delta);
7048 L.Map.addInitHook('addHandler', 'scrollWheelZoom', L.Map.ScrollWheelZoom);
7052 * Extends the event handling code with double tap support for mobile browsers.
7055 L.extend(L.DomEvent, {
7057 _touchstart: L.Browser.msPointer ? 'MSPointerDown' : L.Browser.pointer ? 'pointerdown' : 'touchstart',
7058 _touchend: L.Browser.msPointer ? 'MSPointerUp' : L.Browser.pointer ? 'pointerup' : 'touchend',
7060 // inspired by Zepto touch code by Thomas Fuchs
7061 addDoubleTapListener: function (obj, handler, id) {
7067 touchstart = this._touchstart,
7068 touchend = this._touchend,
7069 trackedTouches = [];
7071 function onTouchStart(e) {
7074 if (L.Browser.pointer) {
7075 trackedTouches.push(e.pointerId);
7076 count = trackedTouches.length;
7078 count = e.touches.length;
7084 var now = Date.now(),
7085 delta = now - (last || now);
7087 touch = e.touches ? e.touches[0] : e;
7088 doubleTap = (delta > 0 && delta <= delay);
7092 function onTouchEnd(e) {
7093 if (L.Browser.pointer) {
7094 var idx = trackedTouches.indexOf(e.pointerId);
7098 trackedTouches.splice(idx, 1);
7102 if (L.Browser.pointer) {
7103 // work around .type being readonly with MSPointer* events
7107 // jshint forin:false
7108 for (var i in touch) {
7110 if (typeof prop === 'function') {
7111 newTouch[i] = prop.bind(touch);
7118 touch.type = 'dblclick';
7123 obj[pre + touchstart + id] = onTouchStart;
7124 obj[pre + touchend + id] = onTouchEnd;
7126 // on pointer we need to listen on the document, otherwise a drag starting on the map and moving off screen
7127 // will not come through to us, so we will lose track of how many touches are ongoing
7128 var endElement = L.Browser.pointer ? document.documentElement : obj;
7130 obj.addEventListener(touchstart, onTouchStart, false);
7131 endElement.addEventListener(touchend, onTouchEnd, false);
7133 if (L.Browser.pointer) {
7134 endElement.addEventListener(L.DomEvent.POINTER_CANCEL, onTouchEnd, false);
7140 removeDoubleTapListener: function (obj, id) {
7141 var pre = '_leaflet_';
7143 obj.removeEventListener(this._touchstart, obj[pre + this._touchstart + id], false);
7144 (L.Browser.pointer ? document.documentElement : obj).removeEventListener(
7145 this._touchend, obj[pre + this._touchend + id], false);
7147 if (L.Browser.pointer) {
7148 document.documentElement.removeEventListener(L.DomEvent.POINTER_CANCEL, obj[pre + this._touchend + id],
7158 * Extends L.DomEvent to provide touch support for Internet Explorer and Windows-based devices.
7161 L.extend(L.DomEvent, {
7164 POINTER_DOWN: L.Browser.msPointer ? 'MSPointerDown' : 'pointerdown',
7165 POINTER_MOVE: L.Browser.msPointer ? 'MSPointerMove' : 'pointermove',
7166 POINTER_UP: L.Browser.msPointer ? 'MSPointerUp' : 'pointerup',
7167 POINTER_CANCEL: L.Browser.msPointer ? 'MSPointerCancel' : 'pointercancel',
7170 _pointerDocumentListener: false,
7172 // Provides a touch events wrapper for (ms)pointer events.
7173 // Based on changes by veproza https://github.com/CloudMade/Leaflet/pull/1019
7174 //ref http://www.w3.org/TR/pointerevents/ https://www.w3.org/Bugs/Public/show_bug.cgi?id=22890
7176 addPointerListener: function (obj, type, handler, id) {
7180 return this.addPointerListenerStart(obj, type, handler, id);
7182 return this.addPointerListenerEnd(obj, type, handler, id);
7184 return this.addPointerListenerMove(obj, type, handler, id);
7186 throw 'Unknown touch event type';
7190 addPointerListenerStart: function (obj, type, handler, id) {
7191 var pre = '_leaflet_',
7192 pointers = this._pointers;
7194 var cb = function (e) {
7196 L.DomEvent.preventDefault(e);
7198 var alreadyInArray = false;
7199 for (var i = 0; i < pointers.length; i++) {
7200 if (pointers[i].pointerId === e.pointerId) {
7201 alreadyInArray = true;
7205 if (!alreadyInArray) {
7209 e.touches = pointers.slice();
7210 e.changedTouches = [e];
7215 obj[pre + 'touchstart' + id] = cb;
7216 obj.addEventListener(this.POINTER_DOWN, cb, false);
7218 // need to also listen for end events to keep the _pointers list accurate
7219 // this needs to be on the body and never go away
7220 if (!this._pointerDocumentListener) {
7221 var internalCb = function (e) {
7222 for (var i = 0; i < pointers.length; i++) {
7223 if (pointers[i].pointerId === e.pointerId) {
7224 pointers.splice(i, 1);
7229 //We listen on the documentElement as any drags that end by moving the touch off the screen get fired there
7230 document.documentElement.addEventListener(this.POINTER_UP, internalCb, false);
7231 document.documentElement.addEventListener(this.POINTER_CANCEL, internalCb, false);
7233 this._pointerDocumentListener = true;
7239 addPointerListenerMove: function (obj, type, handler, id) {
7240 var pre = '_leaflet_',
7241 touches = this._pointers;
7245 // don't fire touch moves when mouse isn't down
7246 if ((e.pointerType === e.MSPOINTER_TYPE_MOUSE || e.pointerType === 'mouse') && e.buttons === 0) { return; }
7248 for (var i = 0; i < touches.length; i++) {
7249 if (touches[i].pointerId === e.pointerId) {
7255 e.touches = touches.slice();
7256 e.changedTouches = [e];
7261 obj[pre + 'touchmove' + id] = cb;
7262 obj.addEventListener(this.POINTER_MOVE, cb, false);
7267 addPointerListenerEnd: function (obj, type, handler, id) {
7268 var pre = '_leaflet_',
7269 touches = this._pointers;
7271 var cb = function (e) {
7272 for (var i = 0; i < touches.length; i++) {
7273 if (touches[i].pointerId === e.pointerId) {
7274 touches.splice(i, 1);
7279 e.touches = touches.slice();
7280 e.changedTouches = [e];
7285 obj[pre + 'touchend' + id] = cb;
7286 obj.addEventListener(this.POINTER_UP, cb, false);
7287 obj.addEventListener(this.POINTER_CANCEL, cb, false);
7292 removePointerListener: function (obj, type, id) {
7293 var pre = '_leaflet_',
7294 cb = obj[pre + type + id];
7298 obj.removeEventListener(this.POINTER_DOWN, cb, false);
7301 obj.removeEventListener(this.POINTER_MOVE, cb, false);
7304 obj.removeEventListener(this.POINTER_UP, cb, false);
7305 obj.removeEventListener(this.POINTER_CANCEL, cb, false);
7315 * L.Handler.TouchZoom is used by L.Map to add pinch zoom on supported mobile browsers.
7318 L.Map.mergeOptions({
7319 touchZoom: L.Browser.touch && !L.Browser.android23,
7320 bounceAtZoomLimits: true
7323 L.Map.TouchZoom = L.Handler.extend({
7324 addHooks: function () {
7325 L.DomEvent.on(this._map._container, 'touchstart', this._onTouchStart, this);
7328 removeHooks: function () {
7329 L.DomEvent.off(this._map._container, 'touchstart', this._onTouchStart, this);
7332 _onTouchStart: function (e) {
7333 var map = this._map;
7335 if (!e.touches || e.touches.length !== 2 || map._animatingZoom || this._zooming) { return; }
7337 var p1 = map.mouseEventToLayerPoint(e.touches[0]),
7338 p2 = map.mouseEventToLayerPoint(e.touches[1]),
7339 viewCenter = map._getCenterLayerPoint();
7341 this._startCenter = p1.add(p2)._divideBy(2);
7342 this._startDist = p1.distanceTo(p2);
7344 this._moved = false;
7345 this._zooming = true;
7347 this._centerOffset = viewCenter.subtract(this._startCenter);
7350 map._panAnim.stop();
7354 .on(document, 'touchmove', this._onTouchMove, this)
7355 .on(document, 'touchend', this._onTouchEnd, this);
7357 L.DomEvent.preventDefault(e);
7360 _onTouchMove: function (e) {
7361 var map = this._map;
7363 if (!e.touches || e.touches.length !== 2 || !this._zooming) { return; }
7365 var p1 = map.mouseEventToLayerPoint(e.touches[0]),
7366 p2 = map.mouseEventToLayerPoint(e.touches[1]);
7368 this._scale = p1.distanceTo(p2) / this._startDist;
7369 this._delta = p1._add(p2)._divideBy(2)._subtract(this._startCenter);
7371 if (this._scale === 1) { return; }
7373 if (!map.options.bounceAtZoomLimits) {
7374 if ((map.getZoom() === map.getMinZoom() && this._scale < 1) ||
7375 (map.getZoom() === map.getMaxZoom() && this._scale > 1)) { return; }
7379 L.DomUtil.addClass(map._mapPane, 'leaflet-touching');
7388 L.Util.cancelAnimFrame(this._animRequest);
7389 this._animRequest = L.Util.requestAnimFrame(
7390 this._updateOnMove, this, true, this._map._container);
7392 L.DomEvent.preventDefault(e);
7395 _updateOnMove: function () {
7396 var map = this._map,
7397 origin = this._getScaleOrigin(),
7398 center = map.layerPointToLatLng(origin),
7399 zoom = map.getScaleZoom(this._scale);
7401 map._animateZoom(center, zoom, this._startCenter, this._scale, this._delta, false, true);
7404 _onTouchEnd: function () {
7405 if (!this._moved || !this._zooming) {
7406 this._zooming = false;
7410 var map = this._map;
7412 this._zooming = false;
7413 L.DomUtil.removeClass(map._mapPane, 'leaflet-touching');
7414 L.Util.cancelAnimFrame(this._animRequest);
7417 .off(document, 'touchmove', this._onTouchMove)
7418 .off(document, 'touchend', this._onTouchEnd);
7420 var origin = this._getScaleOrigin(),
7421 center = map.layerPointToLatLng(origin),
7423 oldZoom = map.getZoom(),
7424 floatZoomDelta = map.getScaleZoom(this._scale) - oldZoom,
7425 roundZoomDelta = (floatZoomDelta > 0 ?
7426 Math.ceil(floatZoomDelta) : Math.floor(floatZoomDelta)),
7428 zoom = map._limitZoom(oldZoom + roundZoomDelta),
7429 scale = map.getZoomScale(zoom) / this._scale;
7431 map._animateZoom(center, zoom, origin, scale);
7434 _getScaleOrigin: function () {
7435 var centerOffset = this._centerOffset.subtract(this._delta).divideBy(this._scale);
7436 return this._startCenter.add(centerOffset);
7440 L.Map.addInitHook('addHandler', 'touchZoom', L.Map.TouchZoom);
7444 * L.Map.Tap is used to enable mobile hacks like quick taps and long hold.
7447 L.Map.mergeOptions({
7452 L.Map.Tap = L.Handler.extend({
7453 addHooks: function () {
7454 L.DomEvent.on(this._map._container, 'touchstart', this._onDown, this);
7457 removeHooks: function () {
7458 L.DomEvent.off(this._map._container, 'touchstart', this._onDown, this);
7461 _onDown: function (e) {
7462 if (!e.touches) { return; }
7464 L.DomEvent.preventDefault(e);
7466 this._fireClick = true;
7468 // don't simulate click or track longpress if more than 1 touch
7469 if (e.touches.length > 1) {
7470 this._fireClick = false;
7471 clearTimeout(this._holdTimeout);
7475 var first = e.touches[0],
7478 this._startPos = this._newPos = new L.Point(first.clientX, first.clientY);
7480 // if touching a link, highlight it
7481 if (el.tagName && el.tagName.toLowerCase() === 'a') {
7482 L.DomUtil.addClass(el, 'leaflet-active');
7485 // simulate long hold but setting a timeout
7486 this._holdTimeout = setTimeout(L.bind(function () {
7487 if (this._isTapValid()) {
7488 this._fireClick = false;
7490 this._simulateEvent('contextmenu', first);
7495 .on(document, 'touchmove', this._onMove, this)
7496 .on(document, 'touchend', this._onUp, this);
7499 _onUp: function (e) {
7500 clearTimeout(this._holdTimeout);
7503 .off(document, 'touchmove', this._onMove, this)
7504 .off(document, 'touchend', this._onUp, this);
7506 if (this._fireClick && e && e.changedTouches) {
7508 var first = e.changedTouches[0],
7511 if (el && el.tagName && el.tagName.toLowerCase() === 'a') {
7512 L.DomUtil.removeClass(el, 'leaflet-active');
7515 // simulate click if the touch didn't move too much
7516 if (this._isTapValid()) {
7517 this._simulateEvent('click', first);
7522 _isTapValid: function () {
7523 return this._newPos.distanceTo(this._startPos) <= this._map.options.tapTolerance;
7526 _onMove: function (e) {
7527 var first = e.touches[0];
7528 this._newPos = new L.Point(first.clientX, first.clientY);
7531 _simulateEvent: function (type, e) {
7532 var simulatedEvent = document.createEvent('MouseEvents');
7534 simulatedEvent._simulated = true;
7535 e.target._simulatedClick = true;
7537 simulatedEvent.initMouseEvent(
7538 type, true, true, window, 1,
7539 e.screenX, e.screenY,
7540 e.clientX, e.clientY,
7541 false, false, false, false, 0, null);
7543 e.target.dispatchEvent(simulatedEvent);
7547 if (L.Browser.touch && !L.Browser.pointer) {
7548 L.Map.addInitHook('addHandler', 'tap', L.Map.Tap);
7553 * L.Handler.ShiftDragZoom is used to add shift-drag zoom interaction to the map
7554 * (zoom to a selected bounding box), enabled by default.
7557 L.Map.mergeOptions({
7561 L.Map.BoxZoom = L.Handler.extend({
7562 initialize: function (map) {
7564 this._container = map._container;
7565 this._pane = map._panes.overlayPane;
7566 this._moved = false;
7569 addHooks: function () {
7570 L.DomEvent.on(this._container, 'mousedown', this._onMouseDown, this);
7573 removeHooks: function () {
7574 L.DomEvent.off(this._container, 'mousedown', this._onMouseDown);
7575 this._moved = false;
7578 moved: function () {
7582 _onMouseDown: function (e) {
7583 this._moved = false;
7585 if (!e.shiftKey || ((e.which !== 1) && (e.button !== 1))) { return false; }
7587 L.DomUtil.disableTextSelection();
7588 L.DomUtil.disableImageDrag();
7590 this._startLayerPoint = this._map.mouseEventToLayerPoint(e);
7593 .on(document, 'mousemove', this._onMouseMove, this)
7594 .on(document, 'mouseup', this._onMouseUp, this)
7595 .on(document, 'keydown', this._onKeyDown, this);
7598 _onMouseMove: function (e) {
7600 this._box = L.DomUtil.create('div', 'leaflet-zoom-box', this._pane);
7601 L.DomUtil.setPosition(this._box, this._startLayerPoint);
7603 //TODO refactor: move cursor to styles
7604 this._container.style.cursor = 'crosshair';
7605 this._map.fire('boxzoomstart');
7608 var startPoint = this._startLayerPoint,
7611 layerPoint = this._map.mouseEventToLayerPoint(e),
7612 offset = layerPoint.subtract(startPoint),
7614 newPos = new L.Point(
7615 Math.min(layerPoint.x, startPoint.x),
7616 Math.min(layerPoint.y, startPoint.y));
7618 L.DomUtil.setPosition(box, newPos);
7622 // TODO refactor: remove hardcoded 4 pixels
7623 box.style.width = (Math.max(0, Math.abs(offset.x) - 4)) + 'px';
7624 box.style.height = (Math.max(0, Math.abs(offset.y) - 4)) + 'px';
7627 _finish: function () {
7629 this._pane.removeChild(this._box);
7630 this._container.style.cursor = '';
7633 L.DomUtil.enableTextSelection();
7634 L.DomUtil.enableImageDrag();
7637 .off(document, 'mousemove', this._onMouseMove)
7638 .off(document, 'mouseup', this._onMouseUp)
7639 .off(document, 'keydown', this._onKeyDown);
7642 _onMouseUp: function (e) {
7646 var map = this._map,
7647 layerPoint = map.mouseEventToLayerPoint(e);
7649 if (this._startLayerPoint.equals(layerPoint)) { return; }
7651 var bounds = new L.LatLngBounds(
7652 map.layerPointToLatLng(this._startLayerPoint),
7653 map.layerPointToLatLng(layerPoint));
7655 map.fitBounds(bounds);
7657 map.fire('boxzoomend', {
7658 boxZoomBounds: bounds
7662 _onKeyDown: function (e) {
7663 if (e.keyCode === 27) {
7669 L.Map.addInitHook('addHandler', 'boxZoom', L.Map.BoxZoom);
7673 * L.Map.Keyboard is handling keyboard interaction with the map, enabled by default.
7676 L.Map.mergeOptions({
7678 keyboardPanOffset: 80,
7679 keyboardZoomOffset: 1
7682 L.Map.Keyboard = L.Handler.extend({
7689 zoomIn: [187, 107, 61, 171],
7690 zoomOut: [189, 109, 173]
7693 initialize: function (map) {
7696 this._setPanOffset(map.options.keyboardPanOffset);
7697 this._setZoomOffset(map.options.keyboardZoomOffset);
7700 addHooks: function () {
7701 var container = this._map._container;
7703 // make the container focusable by tabbing
7704 if (container.tabIndex === -1) {
7705 container.tabIndex = '0';
7709 .on(container, 'focus', this._onFocus, this)
7710 .on(container, 'blur', this._onBlur, this)
7711 .on(container, 'mousedown', this._onMouseDown, this);
7714 .on('focus', this._addHooks, this)
7715 .on('blur', this._removeHooks, this);
7718 removeHooks: function () {
7719 this._removeHooks();
7721 var container = this._map._container;
7724 .off(container, 'focus', this._onFocus, this)
7725 .off(container, 'blur', this._onBlur, this)
7726 .off(container, 'mousedown', this._onMouseDown, this);
7729 .off('focus', this._addHooks, this)
7730 .off('blur', this._removeHooks, this);
7733 _onMouseDown: function () {
7734 if (this._focused) { return; }
7736 var body = document.body,
7737 docEl = document.documentElement,
7738 top = body.scrollTop || docEl.scrollTop,
7739 left = body.scrollLeft || docEl.scrollLeft;
7741 this._map._container.focus();
7743 window.scrollTo(left, top);
7746 _onFocus: function () {
7747 this._focused = true;
7748 this._map.fire('focus');
7751 _onBlur: function () {
7752 this._focused = false;
7753 this._map.fire('blur');
7756 _setPanOffset: function (pan) {
7757 var keys = this._panKeys = {},
7758 codes = this.keyCodes,
7761 for (i = 0, len = codes.left.length; i < len; i++) {
7762 keys[codes.left[i]] = [-1 * pan, 0];
7764 for (i = 0, len = codes.right.length; i < len; i++) {
7765 keys[codes.right[i]] = [pan, 0];
7767 for (i = 0, len = codes.down.length; i < len; i++) {
7768 keys[codes.down[i]] = [0, pan];
7770 for (i = 0, len = codes.up.length; i < len; i++) {
7771 keys[codes.up[i]] = [0, -1 * pan];
7775 _setZoomOffset: function (zoom) {
7776 var keys = this._zoomKeys = {},
7777 codes = this.keyCodes,
7780 for (i = 0, len = codes.zoomIn.length; i < len; i++) {
7781 keys[codes.zoomIn[i]] = zoom;
7783 for (i = 0, len = codes.zoomOut.length; i < len; i++) {
7784 keys[codes.zoomOut[i]] = -zoom;
7788 _addHooks: function () {
7789 L.DomEvent.on(document, 'keydown', this._onKeyDown, this);
7792 _removeHooks: function () {
7793 L.DomEvent.off(document, 'keydown', this._onKeyDown, this);
7796 _onKeyDown: function (e) {
7797 var key = e.keyCode,
7800 if (key in this._panKeys) {
7802 if (map._panAnim && map._panAnim._inProgress) { return; }
7804 map.panBy(this._panKeys[key]);
7806 if (map.options.maxBounds) {
7807 map.panInsideBounds(map.options.maxBounds);
7810 } else if (key in this._zoomKeys) {
7811 map.setZoom(map.getZoom() + this._zoomKeys[key]);
7821 L.Map.addInitHook('addHandler', 'keyboard', L.Map.Keyboard);
7825 * L.Handler.MarkerDrag is used internally by L.Marker to make the markers draggable.
7828 L.Handler.MarkerDrag = L.Handler.extend({
7829 initialize: function (marker) {
7830 this._marker = marker;
7833 addHooks: function () {
7834 var icon = this._marker._icon;
7835 if (!this._draggable) {
7836 this._draggable = new L.Draggable(icon, icon);
7840 .on('dragstart', this._onDragStart, this)
7841 .on('drag', this._onDrag, this)
7842 .on('dragend', this._onDragEnd, this);
7843 this._draggable.enable();
7844 L.DomUtil.addClass(this._marker._icon, 'leaflet-marker-draggable');
7847 removeHooks: function () {
7849 .off('dragstart', this._onDragStart, this)
7850 .off('drag', this._onDrag, this)
7851 .off('dragend', this._onDragEnd, this);
7853 this._draggable.disable();
7854 L.DomUtil.removeClass(this._marker._icon, 'leaflet-marker-draggable');
7857 moved: function () {
7858 return this._draggable && this._draggable._moved;
7861 _onDragStart: function () {
7868 _onDrag: function () {
7869 var marker = this._marker,
7870 shadow = marker._shadow,
7871 iconPos = L.DomUtil.getPosition(marker._icon),
7872 latlng = marker._map.layerPointToLatLng(iconPos);
7874 // update shadow position
7876 L.DomUtil.setPosition(shadow, iconPos);
7879 marker._latlng = latlng;
7882 .fire('move', {latlng: latlng})
7886 _onDragEnd: function (e) {
7889 .fire('dragend', e);
7895 * L.Control is a base class for implementing map controls. Handles positioning.
7896 * All other controls extend from this class.
7899 L.Control = L.Class.extend({
7901 position: 'topright'
7904 initialize: function (options) {
7905 L.setOptions(this, options);
7908 getPosition: function () {
7909 return this.options.position;
7912 setPosition: function (position) {
7913 var map = this._map;
7916 map.removeControl(this);
7919 this.options.position = position;
7922 map.addControl(this);
7928 getContainer: function () {
7929 return this._container;
7932 addTo: function (map) {
7935 var container = this._container = this.onAdd(map),
7936 pos = this.getPosition(),
7937 corner = map._controlCorners[pos];
7939 L.DomUtil.addClass(container, 'leaflet-control');
7941 if (pos.indexOf('bottom') !== -1) {
7942 corner.insertBefore(container, corner.firstChild);
7944 corner.appendChild(container);
7950 removeFrom: function (map) {
7951 var pos = this.getPosition(),
7952 corner = map._controlCorners[pos];
7954 corner.removeChild(this._container);
7957 if (this.onRemove) {
7964 _refocusOnMap: function () {
7966 this._map.getContainer().focus();
7971 L.control = function (options) {
7972 return new L.Control(options);
7976 // adds control-related methods to L.Map
7979 addControl: function (control) {
7980 control.addTo(this);
7984 removeControl: function (control) {
7985 control.removeFrom(this);
7989 _initControlPos: function () {
7990 var corners = this._controlCorners = {},
7992 container = this._controlContainer =
7993 L.DomUtil.create('div', l + 'control-container', this._container);
7995 function createCorner(vSide, hSide) {
7996 var className = l + vSide + ' ' + l + hSide;
7998 corners[vSide + hSide] = L.DomUtil.create('div', className, container);
8001 createCorner('top', 'left');
8002 createCorner('top', 'right');
8003 createCorner('bottom', 'left');
8004 createCorner('bottom', 'right');
8007 _clearControlPos: function () {
8008 this._container.removeChild(this._controlContainer);
8014 * L.Control.Zoom is used for the default zoom buttons on the map.
8017 L.Control.Zoom = L.Control.extend({
8019 position: 'topleft',
8021 zoomInTitle: 'Zoom in',
8023 zoomOutTitle: 'Zoom out'
8026 onAdd: function (map) {
8027 var zoomName = 'leaflet-control-zoom',
8028 container = L.DomUtil.create('div', zoomName + ' leaflet-bar');
8032 this._zoomInButton = this._createButton(
8033 this.options.zoomInText, this.options.zoomInTitle,
8034 zoomName + '-in', container, this._zoomIn, this);
8035 this._zoomOutButton = this._createButton(
8036 this.options.zoomOutText, this.options.zoomOutTitle,
8037 zoomName + '-out', container, this._zoomOut, this);
8039 this._updateDisabled();
8040 map.on('zoomend zoomlevelschange', this._updateDisabled, this);
8045 onRemove: function (map) {
8046 map.off('zoomend zoomlevelschange', this._updateDisabled, this);
8049 _zoomIn: function (e) {
8050 this._map.zoomIn(e.shiftKey ? 3 : 1);
8053 _zoomOut: function (e) {
8054 this._map.zoomOut(e.shiftKey ? 3 : 1);
8057 _createButton: function (html, title, className, container, fn, context) {
8058 var link = L.DomUtil.create('a', className, container);
8059 link.innerHTML = html;
8063 var stop = L.DomEvent.stopPropagation;
8066 .on(link, 'click', stop)
8067 .on(link, 'mousedown', stop)
8068 .on(link, 'dblclick', stop)
8069 .on(link, 'click', L.DomEvent.preventDefault)
8070 .on(link, 'click', fn, context)
8071 .on(link, 'click', this._refocusOnMap, context);
8076 _updateDisabled: function () {
8077 var map = this._map,
8078 className = 'leaflet-disabled';
8080 L.DomUtil.removeClass(this._zoomInButton, className);
8081 L.DomUtil.removeClass(this._zoomOutButton, className);
8083 if (map._zoom === map.getMinZoom()) {
8084 L.DomUtil.addClass(this._zoomOutButton, className);
8086 if (map._zoom === map.getMaxZoom()) {
8087 L.DomUtil.addClass(this._zoomInButton, className);
8092 L.Map.mergeOptions({
8096 L.Map.addInitHook(function () {
8097 if (this.options.zoomControl) {
8098 this.zoomControl = new L.Control.Zoom();
8099 this.addControl(this.zoomControl);
8103 L.control.zoom = function (options) {
8104 return new L.Control.Zoom(options);
8110 * L.Control.Attribution is used for displaying attribution on the map (added by default).
8113 L.Control.Attribution = L.Control.extend({
8115 position: 'bottomright',
8116 prefix: '<a href="http://leafletjs.com" title="A JS library for interactive maps">Leaflet</a>'
8119 initialize: function (options) {
8120 L.setOptions(this, options);
8122 this._attributions = {};
8125 onAdd: function (map) {
8126 this._container = L.DomUtil.create('div', 'leaflet-control-attribution');
8127 L.DomEvent.disableClickPropagation(this._container);
8129 for (var i in map._layers) {
8130 if (map._layers[i].getAttribution) {
8131 this.addAttribution(map._layers[i].getAttribution());
8136 .on('layeradd', this._onLayerAdd, this)
8137 .on('layerremove', this._onLayerRemove, this);
8141 return this._container;
8144 onRemove: function (map) {
8146 .off('layeradd', this._onLayerAdd)
8147 .off('layerremove', this._onLayerRemove);
8151 setPrefix: function (prefix) {
8152 this.options.prefix = prefix;
8157 addAttribution: function (text) {
8158 if (!text) { return; }
8160 if (!this._attributions[text]) {
8161 this._attributions[text] = 0;
8163 this._attributions[text]++;
8170 removeAttribution: function (text) {
8171 if (!text) { return; }
8173 if (this._attributions[text]) {
8174 this._attributions[text]--;
8181 _update: function () {
8182 if (!this._map) { return; }
8186 for (var i in this._attributions) {
8187 if (this._attributions[i]) {
8192 var prefixAndAttribs = [];
8194 if (this.options.prefix) {
8195 prefixAndAttribs.push(this.options.prefix);
8197 if (attribs.length) {
8198 prefixAndAttribs.push(attribs.join(', '));
8201 this._container.innerHTML = prefixAndAttribs.join(' | ');
8204 _onLayerAdd: function (e) {
8205 if (e.layer.getAttribution) {
8206 this.addAttribution(e.layer.getAttribution());
8210 _onLayerRemove: function (e) {
8211 if (e.layer.getAttribution) {
8212 this.removeAttribution(e.layer.getAttribution());
8217 L.Map.mergeOptions({
8218 attributionControl: true
8221 L.Map.addInitHook(function () {
8222 if (this.options.attributionControl) {
8223 this.attributionControl = (new L.Control.Attribution()).addTo(this);
8227 L.control.attribution = function (options) {
8228 return new L.Control.Attribution(options);
8233 * L.Control.Scale is used for displaying metric/imperial scale on the map.
8236 L.Control.Scale = L.Control.extend({
8238 position: 'bottomleft',
8242 updateWhenIdle: false
8245 onAdd: function (map) {
8248 var className = 'leaflet-control-scale',
8249 container = L.DomUtil.create('div', className),
8250 options = this.options;
8252 this._addScales(options, className, container);
8254 map.on(options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
8255 map.whenReady(this._update, this);
8260 onRemove: function (map) {
8261 map.off(this.options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
8264 _addScales: function (options, className, container) {
8265 if (options.metric) {
8266 this._mScale = L.DomUtil.create('div', className + '-line', container);
8268 if (options.imperial) {
8269 this._iScale = L.DomUtil.create('div', className + '-line', container);
8273 _update: function () {
8274 var bounds = this._map.getBounds(),
8275 centerLat = bounds.getCenter().lat,
8276 halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
8277 dist = halfWorldMeters * (bounds.getNorthEast().lng - bounds.getSouthWest().lng) / 180,
8279 size = this._map.getSize(),
8280 options = this.options,
8284 maxMeters = dist * (options.maxWidth / size.x);
8287 this._updateScales(options, maxMeters);
8290 _updateScales: function (options, maxMeters) {
8291 if (options.metric && maxMeters) {
8292 this._updateMetric(maxMeters);
8295 if (options.imperial && maxMeters) {
8296 this._updateImperial(maxMeters);
8300 _updateMetric: function (maxMeters) {
8301 var meters = this._getRoundNum(maxMeters);
8303 this._mScale.style.width = this._getScaleWidth(meters / maxMeters) + 'px';
8304 this._mScale.innerHTML = meters < 1000 ? meters + ' m' : (meters / 1000) + ' km';
8307 _updateImperial: function (maxMeters) {
8308 var maxFeet = maxMeters * 3.2808399,
8309 scale = this._iScale,
8310 maxMiles, miles, feet;
8312 if (maxFeet > 5280) {
8313 maxMiles = maxFeet / 5280;
8314 miles = this._getRoundNum(maxMiles);
8316 scale.style.width = this._getScaleWidth(miles / maxMiles) + 'px';
8317 scale.innerHTML = miles + ' mi';
8320 feet = this._getRoundNum(maxFeet);
8322 scale.style.width = this._getScaleWidth(feet / maxFeet) + 'px';
8323 scale.innerHTML = feet + ' ft';
8327 _getScaleWidth: function (ratio) {
8328 return Math.round(this.options.maxWidth * ratio) - 10;
8331 _getRoundNum: function (num) {
8332 var pow10 = Math.pow(10, (Math.floor(num) + '').length - 1),
8335 d = d >= 10 ? 10 : d >= 5 ? 5 : d >= 3 ? 3 : d >= 2 ? 2 : 1;
8341 L.control.scale = function (options) {
8342 return new L.Control.Scale(options);
8347 * L.Control.Layers is a control to allow users to switch between different layers on the map.
8350 L.Control.Layers = L.Control.extend({
8353 position: 'topright',
8357 initialize: function (baseLayers, overlays, options) {
8358 L.setOptions(this, options);
8361 this._lastZIndex = 0;
8362 this._handlingClick = false;
8364 for (var i in baseLayers) {
8365 this._addLayer(baseLayers[i], i);
8368 for (i in overlays) {
8369 this._addLayer(overlays[i], i, true);
8373 onAdd: function (map) {
8378 .on('layeradd', this._onLayerChange, this)
8379 .on('layerremove', this._onLayerChange, this);
8381 return this._container;
8384 onRemove: function (map) {
8386 .off('layeradd', this._onLayerChange, this)
8387 .off('layerremove', this._onLayerChange, this);
8390 addBaseLayer: function (layer, name) {
8391 this._addLayer(layer, name);
8396 addOverlay: function (layer, name) {
8397 this._addLayer(layer, name, true);
8402 removeLayer: function (layer) {
8403 var id = L.stamp(layer);
8404 delete this._layers[id];
8409 _initLayout: function () {
8410 var className = 'leaflet-control-layers',
8411 container = this._container = L.DomUtil.create('div', className);
8413 //Makes this work on IE10 Touch devices by stopping it from firing a mouseout event when the touch is released
8414 container.setAttribute('aria-haspopup', true);
8416 if (!L.Browser.touch) {
8418 .disableClickPropagation(container)
8419 .disableScrollPropagation(container);
8421 L.DomEvent.on(container, 'click', L.DomEvent.stopPropagation);
8424 var form = this._form = L.DomUtil.create('form', className + '-list');
8426 if (this.options.collapsed) {
8427 if (!L.Browser.android) {
8429 .on(container, 'mouseover', this._expand, this)
8430 .on(container, 'mouseout', this._collapse, this);
8432 var link = this._layersLink = L.DomUtil.create('a', className + '-toggle', container);
8434 link.title = 'Layers';
8436 if (L.Browser.touch) {
8438 .on(link, 'click', L.DomEvent.stop)
8439 .on(link, 'click', this._expand, this);
8442 L.DomEvent.on(link, 'focus', this._expand, this);
8444 //Work around for Firefox android issue https://github.com/Leaflet/Leaflet/issues/2033
8445 L.DomEvent.on(form, 'click', function () {
8446 setTimeout(L.bind(this._onInputClick, this), 0);
8449 this._map.on('click', this._collapse, this);
8450 // TODO keyboard accessibility
8455 this._baseLayersList = L.DomUtil.create('div', className + '-base', form);
8456 this._separator = L.DomUtil.create('div', className + '-separator', form);
8457 this._overlaysList = L.DomUtil.create('div', className + '-overlays', form);
8459 container.appendChild(form);
8462 _addLayer: function (layer, name, overlay) {
8463 var id = L.stamp(layer);
8465 this._layers[id] = {
8471 if (this.options.autoZIndex && layer.setZIndex) {
8473 layer.setZIndex(this._lastZIndex);
8477 _update: function () {
8478 if (!this._container) {
8482 this._baseLayersList.innerHTML = '';
8483 this._overlaysList.innerHTML = '';
8485 var baseLayersPresent = false,
8486 overlaysPresent = false,
8489 for (i in this._layers) {
8490 obj = this._layers[i];
8492 overlaysPresent = overlaysPresent || obj.overlay;
8493 baseLayersPresent = baseLayersPresent || !obj.overlay;
8496 this._separator.style.display = overlaysPresent && baseLayersPresent ? '' : 'none';
8499 _onLayerChange: function (e) {
8500 var obj = this._layers[L.stamp(e.layer)];
8502 if (!obj) { return; }
8504 if (!this._handlingClick) {
8508 var type = obj.overlay ?
8509 (e.type === 'layeradd' ? 'overlayadd' : 'overlayremove') :
8510 (e.type === 'layeradd' ? 'baselayerchange' : null);
8513 this._map.fire(type, obj);
8517 // IE7 bugs out if you create a radio dynamically, so you have to do it this hacky way (see http://bit.ly/PqYLBe)
8518 _createRadioElement: function (name, checked) {
8520 var radioHtml = '<input type="radio" class="leaflet-control-layers-selector" name="' + name + '"';
8522 radioHtml += ' checked="checked"';
8526 var radioFragment = document.createElement('div');
8527 radioFragment.innerHTML = radioHtml;
8529 return radioFragment.firstChild;
8532 _addItem: function (obj) {
8533 var label = document.createElement('label'),
8535 checked = this._map.hasLayer(obj.layer);
8538 input = document.createElement('input');
8539 input.type = 'checkbox';
8540 input.className = 'leaflet-control-layers-selector';
8541 input.defaultChecked = checked;
8543 input = this._createRadioElement('leaflet-base-layers', checked);
8546 input.layerId = L.stamp(obj.layer);
8548 L.DomEvent.on(input, 'click', this._onInputClick, this);
8550 var name = document.createElement('span');
8551 name.innerHTML = ' ' + obj.name;
8553 label.appendChild(input);
8554 label.appendChild(name);
8556 var container = obj.overlay ? this._overlaysList : this._baseLayersList;
8557 container.appendChild(label);
8562 _onInputClick: function () {
8564 inputs = this._form.getElementsByTagName('input'),
8565 inputsLen = inputs.length;
8567 this._handlingClick = true;
8569 for (i = 0; i < inputsLen; i++) {
8571 obj = this._layers[input.layerId];
8573 if (input.checked && !this._map.hasLayer(obj.layer)) {
8574 this._map.addLayer(obj.layer);
8576 } else if (!input.checked && this._map.hasLayer(obj.layer)) {
8577 this._map.removeLayer(obj.layer);
8581 this._handlingClick = false;
8583 this._refocusOnMap();
8586 _expand: function () {
8587 L.DomUtil.addClass(this._container, 'leaflet-control-layers-expanded');
8590 _collapse: function () {
8591 this._container.className = this._container.className.replace(' leaflet-control-layers-expanded', '');
8595 L.control.layers = function (baseLayers, overlays, options) {
8596 return new L.Control.Layers(baseLayers, overlays, options);
8601 * L.PosAnimation is used by Leaflet internally for pan animations.
8604 L.PosAnimation = L.Class.extend({
8605 includes: L.Mixin.Events,
8607 run: function (el, newPos, duration, easeLinearity) { // (HTMLElement, Point[, Number, Number])
8611 this._inProgress = true;
8612 this._newPos = newPos;
8616 el.style[L.DomUtil.TRANSITION] = 'all ' + (duration || 0.25) +
8617 's cubic-bezier(0,0,' + (easeLinearity || 0.5) + ',1)';
8619 L.DomEvent.on(el, L.DomUtil.TRANSITION_END, this._onTransitionEnd, this);
8620 L.DomUtil.setPosition(el, newPos);
8622 // toggle reflow, Chrome flickers for some reason if you don't do this
8623 L.Util.falseFn(el.offsetWidth);
8625 // there's no native way to track value updates of transitioned properties, so we imitate this
8626 this._stepTimer = setInterval(L.bind(this._onStep, this), 50);
8630 if (!this._inProgress) { return; }
8632 // if we just removed the transition property, the element would jump to its final position,
8633 // so we need to make it stay at the current position
8635 L.DomUtil.setPosition(this._el, this._getPos());
8636 this._onTransitionEnd();
8637 L.Util.falseFn(this._el.offsetWidth); // force reflow in case we are about to start a new animation
8640 _onStep: function () {
8641 var stepPos = this._getPos();
8643 this._onTransitionEnd();
8646 // jshint camelcase: false
8647 // make L.DomUtil.getPosition return intermediate position value during animation
8648 this._el._leaflet_pos = stepPos;
8653 // you can't easily get intermediate values of properties animated with CSS3 Transitions,
8654 // we need to parse computed style (in case of transform it returns matrix string)
8656 _transformRe: /([-+]?(?:\d*\.)?\d+)\D*, ([-+]?(?:\d*\.)?\d+)\D*\)/,
8658 _getPos: function () {
8659 var left, top, matches,
8661 style = window.getComputedStyle(el);
8663 if (L.Browser.any3d) {
8664 matches = style[L.DomUtil.TRANSFORM].match(this._transformRe);
8665 if (!matches) { return; }
8666 left = parseFloat(matches[1]);
8667 top = parseFloat(matches[2]);
8669 left = parseFloat(style.left);
8670 top = parseFloat(style.top);
8673 return new L.Point(left, top, true);
8676 _onTransitionEnd: function () {
8677 L.DomEvent.off(this._el, L.DomUtil.TRANSITION_END, this._onTransitionEnd, this);
8679 if (!this._inProgress) { return; }
8680 this._inProgress = false;
8682 this._el.style[L.DomUtil.TRANSITION] = '';
8684 // jshint camelcase: false
8685 // make sure L.DomUtil.getPosition returns the final position value after animation
8686 this._el._leaflet_pos = this._newPos;
8688 clearInterval(this._stepTimer);
8690 this.fire('step').fire('end');
8697 * Extends L.Map to handle panning animations.
8702 setView: function (center, zoom, options) {
8704 zoom = zoom === undefined ? this._zoom : this._limitZoom(zoom);
8705 center = this._limitCenter(L.latLng(center), zoom, this.options.maxBounds);
8706 options = options || {};
8708 if (this._panAnim) {
8709 this._panAnim.stop();
8712 if (this._loaded && !options.reset && options !== true) {
8714 if (options.animate !== undefined) {
8715 options.zoom = L.extend({animate: options.animate}, options.zoom);
8716 options.pan = L.extend({animate: options.animate}, options.pan);
8719 // try animating pan or zoom
8720 var animated = (this._zoom !== zoom) ?
8721 this._tryAnimatedZoom && this._tryAnimatedZoom(center, zoom, options.zoom) :
8722 this._tryAnimatedPan(center, options.pan);
8725 // prevent resize handler call, the view will refresh after animation anyway
8726 clearTimeout(this._sizeTimer);
8731 // animation didn't start, just reset the map view
8732 this._resetView(center, zoom);
8737 panBy: function (offset, options) {
8738 offset = L.point(offset).round();
8739 options = options || {};
8741 if (!offset.x && !offset.y) {
8745 if (!this._panAnim) {
8746 this._panAnim = new L.PosAnimation();
8749 'step': this._onPanTransitionStep,
8750 'end': this._onPanTransitionEnd
8754 // don't fire movestart if animating inertia
8755 if (!options.noMoveStart) {
8756 this.fire('movestart');
8759 // animate pan unless animate: false specified
8760 if (options.animate !== false) {
8761 L.DomUtil.addClass(this._mapPane, 'leaflet-pan-anim');
8763 var newPos = this._getMapPanePos().subtract(offset);
8764 this._panAnim.run(this._mapPane, newPos, options.duration || 0.25, options.easeLinearity);
8766 this._rawPanBy(offset);
8767 this.fire('move').fire('moveend');
8773 _onPanTransitionStep: function () {
8777 _onPanTransitionEnd: function () {
8778 L.DomUtil.removeClass(this._mapPane, 'leaflet-pan-anim');
8779 this.fire('moveend');
8782 _tryAnimatedPan: function (center, options) {
8783 // difference between the new and current centers in pixels
8784 var offset = this._getCenterOffset(center)._floor();
8786 // don't animate too far unless animate: true specified in options
8787 if ((options && options.animate) !== true && !this.getSize().contains(offset)) { return false; }
8789 this.panBy(offset, options);
8797 * L.PosAnimation fallback implementation that powers Leaflet pan animations
8798 * in browsers that don't support CSS3 Transitions.
8801 L.PosAnimation = L.DomUtil.TRANSITION ? L.PosAnimation : L.PosAnimation.extend({
8803 run: function (el, newPos, duration, easeLinearity) { // (HTMLElement, Point[, Number, Number])
8807 this._inProgress = true;
8808 this._duration = duration || 0.25;
8809 this._easeOutPower = 1 / Math.max(easeLinearity || 0.5, 0.2);
8811 this._startPos = L.DomUtil.getPosition(el);
8812 this._offset = newPos.subtract(this._startPos);
8813 this._startTime = +new Date();
8821 if (!this._inProgress) { return; }
8827 _animate: function () {
8829 this._animId = L.Util.requestAnimFrame(this._animate, this);
8833 _step: function () {
8834 var elapsed = (+new Date()) - this._startTime,
8835 duration = this._duration * 1000;
8837 if (elapsed < duration) {
8838 this._runFrame(this._easeOut(elapsed / duration));
8845 _runFrame: function (progress) {
8846 var pos = this._startPos.add(this._offset.multiplyBy(progress));
8847 L.DomUtil.setPosition(this._el, pos);
8852 _complete: function () {
8853 L.Util.cancelAnimFrame(this._animId);
8855 this._inProgress = false;
8859 _easeOut: function (t) {
8860 return 1 - Math.pow(1 - t, this._easeOutPower);
8866 * Extends L.Map to handle zoom animations.
8869 L.Map.mergeOptions({
8870 zoomAnimation: true,
8871 zoomAnimationThreshold: 4
8874 if (L.DomUtil.TRANSITION) {
8876 L.Map.addInitHook(function () {
8877 // don't animate on browsers without hardware-accelerated transitions or old Android/Opera
8878 this._zoomAnimated = this.options.zoomAnimation && L.DomUtil.TRANSITION &&
8879 L.Browser.any3d && !L.Browser.android23 && !L.Browser.mobileOpera;
8881 // zoom transitions run with the same duration for all layers, so if one of transitionend events
8882 // happens after starting zoom animation (propagating to the map pane), we know that it ended globally
8883 if (this._zoomAnimated) {
8884 L.DomEvent.on(this._mapPane, L.DomUtil.TRANSITION_END, this._catchTransitionEnd, this);
8889 L.Map.include(!L.DomUtil.TRANSITION ? {} : {
8891 _catchTransitionEnd: function (e) {
8892 if (this._animatingZoom && e.propertyName.indexOf('transform') >= 0) {
8893 this._onZoomTransitionEnd();
8897 _nothingToAnimate: function () {
8898 return !this._container.getElementsByClassName('leaflet-zoom-animated').length;
8901 _tryAnimatedZoom: function (center, zoom, options) {
8903 if (this._animatingZoom) { return true; }
8905 options = options || {};
8907 // don't animate if disabled, not supported or zoom difference is too large
8908 if (!this._zoomAnimated || options.animate === false || this._nothingToAnimate() ||
8909 Math.abs(zoom - this._zoom) > this.options.zoomAnimationThreshold) { return false; }
8911 // offset is the pixel coords of the zoom origin relative to the current center
8912 var scale = this.getZoomScale(zoom),
8913 offset = this._getCenterOffset(center)._divideBy(1 - 1 / scale),
8914 origin = this._getCenterLayerPoint()._add(offset);
8916 // don't animate if the zoom origin isn't within one screen from the current center, unless forced
8917 if (options.animate !== true && !this.getSize().contains(offset)) { return false; }
8923 this._animateZoom(center, zoom, origin, scale, null, true);
8928 _animateZoom: function (center, zoom, origin, scale, delta, backwards, forTouchZoom) {
8930 if (!forTouchZoom) {
8931 this._animatingZoom = true;
8934 // put transform transition on all layers with leaflet-zoom-animated class
8935 L.DomUtil.addClass(this._mapPane, 'leaflet-zoom-anim');
8937 // remember what center/zoom to set after animation
8938 this._animateToCenter = center;
8939 this._animateToZoom = zoom;
8941 // disable any dragging during animation
8943 L.Draggable._disabled = true;
8946 L.Util.requestAnimFrame(function () {
8947 this.fire('zoomanim', {
8953 backwards: backwards
8958 _onZoomTransitionEnd: function () {
8960 this._animatingZoom = false;
8962 L.DomUtil.removeClass(this._mapPane, 'leaflet-zoom-anim');
8964 this._resetView(this._animateToCenter, this._animateToZoom, true, true);
8967 L.Draggable._disabled = false;
8974 Zoom animation logic for L.TileLayer.
8977 L.TileLayer.include({
8978 _animateZoom: function (e) {
8979 if (!this._animating) {
8980 this._animating = true;
8981 this._prepareBgBuffer();
8984 var bg = this._bgBuffer,
8985 transform = L.DomUtil.TRANSFORM,
8986 initialTransform = e.delta ? L.DomUtil.getTranslateString(e.delta) : bg.style[transform],
8987 scaleStr = L.DomUtil.getScaleString(e.scale, e.origin);
8989 bg.style[transform] = e.backwards ?
8990 scaleStr + ' ' + initialTransform :
8991 initialTransform + ' ' + scaleStr;
8994 _endZoomAnim: function () {
8995 var front = this._tileContainer,
8996 bg = this._bgBuffer;
8998 front.style.visibility = '';
8999 front.parentNode.appendChild(front); // Bring to fore
9002 L.Util.falseFn(bg.offsetWidth);
9004 this._animating = false;
9007 _clearBgBuffer: function () {
9008 var map = this._map;
9010 if (map && !map._animatingZoom && !map.touchZoom._zooming) {
9011 this._bgBuffer.innerHTML = '';
9012 this._bgBuffer.style[L.DomUtil.TRANSFORM] = '';
9016 _prepareBgBuffer: function () {
9018 var front = this._tileContainer,
9019 bg = this._bgBuffer;
9021 // if foreground layer doesn't have many tiles but bg layer does,
9022 // keep the existing bg layer and just zoom it some more
9024 var bgLoaded = this._getLoadedTilesPercentage(bg),
9025 frontLoaded = this._getLoadedTilesPercentage(front);
9027 if (bg && bgLoaded > 0.5 && frontLoaded < 0.5) {
9029 front.style.visibility = 'hidden';
9030 this._stopLoadingImages(front);
9034 // prepare the buffer to become the front tile pane
9035 bg.style.visibility = 'hidden';
9036 bg.style[L.DomUtil.TRANSFORM] = '';
9038 // switch out the current layer to be the new bg layer (and vice-versa)
9039 this._tileContainer = bg;
9040 bg = this._bgBuffer = front;
9042 this._stopLoadingImages(bg);
9044 //prevent bg buffer from clearing right after zoom
9045 clearTimeout(this._clearBgBufferTimer);
9048 _getLoadedTilesPercentage: function (container) {
9049 var tiles = container.getElementsByTagName('img'),
9052 for (i = 0, len = tiles.length; i < len; i++) {
9053 if (tiles[i].complete) {
9060 // stops loading all tiles in the background layer
9061 _stopLoadingImages: function (container) {
9062 var tiles = Array.prototype.slice.call(container.getElementsByTagName('img')),
9065 for (i = 0, len = tiles.length; i < len; i++) {
9068 if (!tile.complete) {
9069 tile.onload = L.Util.falseFn;
9070 tile.onerror = L.Util.falseFn;
9071 tile.src = L.Util.emptyImageUrl;
9073 tile.parentNode.removeChild(tile);
9081 * Provides L.Map with convenient shortcuts for using browser geolocation features.
9085 _defaultLocateOptions: {
9091 enableHighAccuracy: false
9094 locate: function (/*Object*/ options) {
9096 options = this._locateOptions = L.extend(this._defaultLocateOptions, options);
9098 if (!navigator.geolocation) {
9099 this._handleGeolocationError({
9101 message: 'Geolocation not supported.'
9106 var onResponse = L.bind(this._handleGeolocationResponse, this),
9107 onError = L.bind(this._handleGeolocationError, this);
9109 if (options.watch) {
9110 this._locationWatchId =
9111 navigator.geolocation.watchPosition(onResponse, onError, options);
9113 navigator.geolocation.getCurrentPosition(onResponse, onError, options);
9118 stopLocate: function () {
9119 if (navigator.geolocation) {
9120 navigator.geolocation.clearWatch(this._locationWatchId);
9122 if (this._locateOptions) {
9123 this._locateOptions.setView = false;
9128 _handleGeolocationError: function (error) {
9130 message = error.message ||
9131 (c === 1 ? 'permission denied' :
9132 (c === 2 ? 'position unavailable' : 'timeout'));
9134 if (this._locateOptions.setView && !this._loaded) {
9138 this.fire('locationerror', {
9140 message: 'Geolocation error: ' + message + '.'
9144 _handleGeolocationResponse: function (pos) {
9145 var lat = pos.coords.latitude,
9146 lng = pos.coords.longitude,
9147 latlng = new L.LatLng(lat, lng),
9149 latAccuracy = 180 * pos.coords.accuracy / 40075017,
9150 lngAccuracy = latAccuracy / Math.cos(L.LatLng.DEG_TO_RAD * lat),
9152 bounds = L.latLngBounds(
9153 [lat - latAccuracy, lng - lngAccuracy],
9154 [lat + latAccuracy, lng + lngAccuracy]),
9156 options = this._locateOptions;
9158 if (options.setView) {
9159 var zoom = Math.min(this.getBoundsZoom(bounds), options.maxZoom);
9160 this.setView(latlng, zoom);
9166 timestamp: pos.timestamp
9169 for (var i in pos.coords) {
9170 if (typeof pos.coords[i] === 'number') {
9171 data[i] = pos.coords[i];
9175 this.fire('locationfound', data);
9180 }(window, document));