2 Copyright (c) 2010-2012, CloudMade, Vladimir Agafonkin
3 Leaflet is an open-source JavaScript library for mobile-friendly interactive maps.
6 (function (window, undefined) {
10 if (typeof exports !== undefined + '') {
16 L.noConflict = function () {
28 * L.Util is a namespace for various utility functions.
32 extend: function (dest) { // (Object[, Object, ...]) ->
33 var sources = Array.prototype.slice.call(arguments, 1),
36 for (j = 0, len = sources.length; j < len; j++) {
37 src = sources[j] || {};
39 if (src.hasOwnProperty(i)) {
47 bind: function (fn, obj) { // (Function, Object) -> Function
48 var args = arguments.length > 2 ? Array.prototype.slice.call(arguments, 2) : null;
50 return fn.apply(obj, args || arguments);
55 var lastId = 0, key = '_leaflet_id';
56 return function (/*Object*/ obj) {
57 obj[key] = obj[key] || ++lastId;
62 limitExecByInterval: function (fn, time, context) {
63 var lock, execOnUnlock;
65 return function wrapperFn() {
75 setTimeout(function () {
79 wrapperFn.apply(context, args);
84 fn.apply(context, args);
88 falseFn: function () {
92 formatNum: function (num, digits) {
93 var pow = Math.pow(10, digits || 5);
94 return Math.round(num * pow) / pow;
97 splitWords: function (str) {
98 return str.replace(/^\s+|\s+$/g, '').split(/\s+/);
101 setOptions: function (obj, options) {
102 obj.options = L.extend({}, obj.options, options);
106 getParamString: function (obj) {
109 if (obj.hasOwnProperty(i)) {
110 params.push(i + '=' + obj[i]);
113 return '?' + params.join('&');
116 template: function (str, data) {
117 return str.replace(/\{ *([\w_]+) *\}/g, function (str, key) {
118 var value = data[key];
119 if (!data.hasOwnProperty(key)) {
120 throw new Error('No value provided for variable ' + str);
126 emptyImageUrl: 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='
131 // inspired by http://paulirish.com/2011/requestanimationframe-for-smart-animating/
133 function getPrefixed(name) {
135 prefixes = ['webkit', 'moz', 'o', 'ms'];
137 for (i = 0; i < prefixes.length && !fn; i++) {
138 fn = window[prefixes[i] + name];
146 function timeoutDefer(fn) {
147 var time = +new Date(),
148 timeToCall = Math.max(0, 16 - (time - lastTime));
150 lastTime = time + timeToCall;
151 return window.setTimeout(fn, timeToCall);
154 var requestFn = window.requestAnimationFrame ||
155 getPrefixed('RequestAnimationFrame') || timeoutDefer;
157 var cancelFn = window.cancelAnimationFrame ||
158 getPrefixed('CancelAnimationFrame') ||
159 getPrefixed('CancelRequestAnimationFrame') ||
160 function (id) { window.clearTimeout(id); };
163 L.Util.requestAnimFrame = function (fn, context, immediate, element) {
164 fn = L.bind(fn, context);
166 if (immediate && requestFn === timeoutDefer) {
169 return requestFn.call(window, fn, element);
173 L.Util.cancelAnimFrame = function (id) {
175 cancelFn.call(window, id);
181 // shortcuts for most used utility functions
182 L.extend = L.Util.extend;
183 L.bind = L.Util.bind;
184 L.stamp = L.Util.stamp;
185 L.setOptions = L.Util.setOptions;
189 * Class powers the OOP facilities of the library. Thanks to John Resig and Dean Edwards for inspiration!
192 L.Class = function () {};
194 L.Class.extend = function (/*Object*/ props) /*-> Class*/ {
196 // extended class with the new prototype
197 var NewClass = function () {
198 if (this.initialize) {
199 this.initialize.apply(this, arguments);
203 // instantiate class without calling constructor
204 var F = function () {};
205 F.prototype = this.prototype;
208 proto.constructor = NewClass;
210 NewClass.prototype = proto;
212 //inherit parent's statics
213 for (var i in this) {
214 if (this.hasOwnProperty(i) && i !== 'prototype') {
215 NewClass[i] = this[i];
219 // mix static properties into the class
221 L.extend(NewClass, props.statics);
222 delete props.statics;
225 // mix includes into the prototype
226 if (props.includes) {
227 L.Util.extend.apply(null, [proto].concat(props.includes));
228 delete props.includes;
232 if (props.options && proto.options) {
233 props.options = L.extend({}, proto.options, props.options);
236 // mix given properties into the prototype
237 L.extend(proto, props);
243 // method for adding properties to prototype
244 L.Class.include = function (props) {
245 L.extend(this.prototype, props);
248 L.Class.mergeOptions = function (options) {
249 L.extend(this.prototype.options, options);
254 * L.Mixin.Events adds custom events functionality to Leaflet classes
257 var key = '_leaflet_events';
263 addEventListener: function (types, fn, context) { // (String, Function[, Object]) or (Object[, Object])
264 var events = this[key] = this[key] || {},
267 // Types can be a map of types/handlers
268 if (typeof types === 'object') {
269 for (type in types) {
270 if (types.hasOwnProperty(type)) {
271 this.addEventListener(type, types[type], fn);
278 types = L.Util.splitWords(types);
280 for (i = 0, len = types.length; i < len; i++) {
281 events[types[i]] = events[types[i]] || [];
282 events[types[i]].push({
284 context: context || this
291 hasEventListeners: function (type) { // (String) -> Boolean
292 return (key in this) && (type in this[key]) && (this[key][type].length > 0);
295 removeEventListener: function (types, fn, context) { // (String[, Function, Object]) or (Object[, Object])
296 var events = this[key],
297 type, i, len, listeners, j;
299 if (typeof types === 'object') {
300 for (type in types) {
301 if (types.hasOwnProperty(type)) {
302 this.removeEventListener(type, types[type], fn);
309 types = L.Util.splitWords(types);
311 for (i = 0, len = types.length; i < len; i++) {
313 if (this.hasEventListeners(types[i])) {
314 listeners = events[types[i]];
316 for (j = listeners.length - 1; j >= 0; j--) {
318 (!fn || listeners[j].action === fn) &&
319 (!context || (listeners[j].context === context))
321 listeners.splice(j, 1);
330 fireEvent: function (type, data) { // (String[, Object])
331 if (!this.hasEventListeners(type)) {
335 var event = L.extend({
340 var listeners = this[key][type].slice();
342 for (var i = 0, len = listeners.length; i < len; i++) {
343 listeners[i].action.call(listeners[i].context || this, event);
350 L.Mixin.Events.on = L.Mixin.Events.addEventListener;
351 L.Mixin.Events.off = L.Mixin.Events.removeEventListener;
352 L.Mixin.Events.fire = L.Mixin.Events.fireEvent;
357 var ie = !!window.ActiveXObject,
358 // http://tanalin.com/en/articles/ie-version-js/
359 ie6 = ie && !window.XMLHttpRequest,
360 ie7 = ie && !document.querySelector,
362 // terrible browser detection to work around Safari / iOS / Android browser bugs
363 // see TileLayer._addTile and debug/hacks/jitter.html
365 ua = navigator.userAgent.toLowerCase(),
366 webkit = ua.indexOf("webkit") !== -1,
367 chrome = ua.indexOf("chrome") !== -1,
368 android = ua.indexOf("android") !== -1,
369 android23 = ua.search("android [23]") !== -1,
371 mobile = typeof orientation !== undefined + '',
372 msTouch = (window.navigator && window.navigator.msPointerEnabled && window.navigator.msMaxTouchPoints),
373 retina = (('devicePixelRatio' in window && window.devicePixelRatio > 1) ||
374 ('matchMedia' in window && window.matchMedia("(min-resolution:144dpi)").matches)),
376 doc = document.documentElement,
377 ie3d = ie && ('transition' in doc.style),
378 webkit3d = ('WebKitCSSMatrix' in window) && ('m11' in new window.WebKitCSSMatrix()),
379 gecko3d = 'MozPerspective' in doc.style,
380 opera3d = 'OTransition' in doc.style,
381 any3d = !window.L_DISABLE_3D && (ie3d || webkit3d || gecko3d || opera3d);
384 var touch = !window.L_NO_TOUCH && (function () {
386 var startName = 'ontouchstart';
388 // IE10+ (We simulate these into touch* events in L.DomEvent and L.DomEvent.MsTouch) or WebKit, etc.
389 if (msTouch || (startName in doc)) {
394 var div = document.createElement('div'),
397 if (!div.setAttribute) {
400 div.setAttribute(startName, 'return;');
402 if (typeof div[startName] === 'function') {
406 div.removeAttribute(startName);
419 android23: android23,
430 mobileWebkit: mobile && webkit,
431 mobileWebkit3d: mobile && webkit3d,
432 mobileOpera: mobile && window.opera,
444 * L.Point represents a point with x and y coordinates.
447 L.Point = function (/*Number*/ x, /*Number*/ y, /*Boolean*/ round) {
448 this.x = (round ? Math.round(x) : x);
449 this.y = (round ? Math.round(y) : y);
452 L.Point.prototype = {
455 return new L.Point(this.x, this.y);
458 // non-destructive, returns a new point
459 add: function (point) {
460 return this.clone()._add(L.point(point));
463 // destructive, used directly for performance in situations where it's safe to modify existing point
464 _add: function (point) {
470 subtract: function (point) {
471 return this.clone()._subtract(L.point(point));
474 _subtract: function (point) {
480 divideBy: function (num) {
481 return this.clone()._divideBy(num);
484 _divideBy: function (num) {
490 multiplyBy: function (num) {
491 return this.clone()._multiplyBy(num);
494 _multiplyBy: function (num) {
501 return this.clone()._round();
504 _round: function () {
505 this.x = Math.round(this.x);
506 this.y = Math.round(this.y);
511 return this.clone()._floor();
514 _floor: function () {
515 this.x = Math.floor(this.x);
516 this.y = Math.floor(this.y);
520 distanceTo: function (point) {
521 point = L.point(point);
523 var x = point.x - this.x,
524 y = point.y - this.y;
526 return Math.sqrt(x * x + y * y);
529 toString: function () {
531 L.Util.formatNum(this.x) + ', ' +
532 L.Util.formatNum(this.y) + ')';
536 L.point = function (x, y, round) {
537 if (x instanceof L.Point) {
540 if (x instanceof Array) {
541 return new L.Point(x[0], x[1]);
546 return new L.Point(x, y, round);
551 * L.Bounds represents a rectangular area on the screen in pixel coordinates.
554 L.Bounds = L.Class.extend({
556 initialize: function (a, b) { //(Point, Point) or Point[]
559 var points = b ? [a, b] : a;
561 for (var i = 0, len = points.length; i < len; i++) {
562 this.extend(points[i]);
566 // extend the bounds to contain the given point
567 extend: function (point) { // (Point)
568 point = L.point(point);
570 if (!this.min && !this.max) {
571 this.min = point.clone();
572 this.max = point.clone();
574 this.min.x = Math.min(point.x, this.min.x);
575 this.max.x = Math.max(point.x, this.max.x);
576 this.min.y = Math.min(point.y, this.min.y);
577 this.max.y = Math.max(point.y, this.max.y);
582 getCenter: function (round) { // (Boolean) -> Point
584 (this.min.x + this.max.x) / 2,
585 (this.min.y + this.max.y) / 2, round);
588 getBottomLeft: function () { // -> Point
589 return new L.Point(this.min.x, this.max.y);
592 getTopRight: function () { // -> Point
593 return new L.Point(this.max.x, this.min.y);
596 contains: function (obj) { // (Bounds) or (Point) -> Boolean
599 if (typeof obj[0] === 'number' || obj instanceof L.Point) {
605 if (obj instanceof L.Bounds) {
612 return (min.x >= this.min.x) &&
613 (max.x <= this.max.x) &&
614 (min.y >= this.min.y) &&
615 (max.y <= this.max.y);
618 intersects: function (bounds) { // (Bounds) -> Boolean
619 bounds = L.bounds(bounds);
625 xIntersects = (max2.x >= min.x) && (min2.x <= max.x),
626 yIntersects = (max2.y >= min.y) && (min2.y <= max.y);
628 return xIntersects && yIntersects;
631 isValid: function () {
632 return !!(this.min && this.max);
636 L.bounds = function (a, b) { // (Bounds) or (Point, Point) or (Point[])
637 if (!a || a instanceof L.Bounds) {
640 return new L.Bounds(a, b);
645 * L.Transformation is an utility class to perform simple point transformations through a 2d-matrix.
648 L.Transformation = L.Class.extend({
649 initialize: function (/*Number*/ a, /*Number*/ b, /*Number*/ c, /*Number*/ d) {
656 transform: function (point, scale) {
657 return this._transform(point.clone(), scale);
660 // destructive transform (faster)
661 _transform: function (/*Point*/ point, /*Number*/ scale) /*-> Point*/ {
663 point.x = scale * (this._a * point.x + this._b);
664 point.y = scale * (this._c * point.y + this._d);
668 untransform: function (/*Point*/ point, /*Number*/ scale) /*-> Point*/ {
671 (point.x / scale - this._b) / this._a,
672 (point.y / scale - this._d) / this._c);
678 * L.DomUtil contains various utility functions for working with DOM.
683 return (typeof id === 'string' ? document.getElementById(id) : id);
686 getStyle: function (el, style) {
688 var value = el.style[style];
690 if (!value && el.currentStyle) {
691 value = el.currentStyle[style];
694 if ((!value || value === 'auto') && document.defaultView) {
695 var css = document.defaultView.getComputedStyle(el, null);
696 value = css ? css[style] : null;
699 return value === 'auto' ? null : value;
702 getViewportOffset: function (element) {
707 docBody = document.body,
712 top += el.offsetTop || 0;
713 left += el.offsetLeft || 0;
714 pos = L.DomUtil.getStyle(el, 'position');
716 if (el.offsetParent === docBody && pos === 'absolute') { break; }
718 if (pos === 'fixed') {
719 top += docBody.scrollTop || 0;
720 left += docBody.scrollLeft || 0;
723 el = el.offsetParent;
730 if (el === docBody) { break; }
732 top -= el.scrollTop || 0;
733 left -= el.scrollLeft || 0;
735 // webkit (and ie <= 7) handles RTL scrollLeft different to everyone else
736 // https://code.google.com/p/closure-library/source/browse/trunk/closure/goog/style/bidi.js
737 if (!L.DomUtil.documentIsLtr() && (L.Browser.webkit || ie7)) {
738 left += el.scrollWidth - el.clientWidth;
740 // ie7 shows the scrollbar by default and provides clientWidth counting it, so we
741 // need to add it back in if it is visible; scrollbar is on the left as we are RTL
742 if (ie7 && L.DomUtil.getStyle(el, 'overflow-y') !== 'hidden' &&
743 L.DomUtil.getStyle(el, 'overflow') !== 'hidden') {
751 return new L.Point(left, top);
754 documentIsLtr: function () {
755 if (!L.DomUtil._docIsLtrCached) {
756 L.DomUtil._docIsLtrCached = true;
757 L.DomUtil._docIsLtr = L.DomUtil.getStyle(document.body, 'direction') === "ltr";
759 return L.DomUtil._docIsLtr;
762 create: function (tagName, className, container) {
764 var el = document.createElement(tagName);
765 el.className = className;
768 container.appendChild(el);
774 disableTextSelection: function () {
775 if (document.selection && document.selection.empty) {
776 document.selection.empty();
778 if (!this._onselectstart) {
779 this._onselectstart = document.onselectstart;
780 document.onselectstart = L.Util.falseFn;
784 enableTextSelection: function () {
785 if (document.onselectstart === L.Util.falseFn) {
786 document.onselectstart = this._onselectstart;
787 this._onselectstart = null;
791 hasClass: function (el, name) {
792 return (el.className.length > 0) &&
793 new RegExp("(^|\\s)" + name + "(\\s|$)").test(el.className);
796 addClass: function (el, name) {
797 if (!L.DomUtil.hasClass(el, name)) {
798 el.className += (el.className ? ' ' : '') + name;
802 removeClass: function (el, name) {
804 function replaceFn(w, match) {
805 if (match === name) { return ''; }
809 el.className = el.className
810 .replace(/(\S+)\s*/g, replaceFn)
811 .replace(/(^\s+|\s+$)/, '');
814 setOpacity: function (el, value) {
816 if ('opacity' in el.style) {
817 el.style.opacity = value;
819 } else if ('filter' in el.style) {
822 filterName = 'DXImageTransform.Microsoft.Alpha';
824 // filters collection throws an error if we try to retrieve a filter that doesn't exist
825 try { filter = el.filters.item(filterName); } catch (e) {}
827 value = Math.round(value * 100);
830 filter.Enabled = (value !== 100);
831 filter.Opacity = value;
833 el.style.filter += ' progid:' + filterName + '(opacity=' + value + ')';
838 testProp: function (props) {
840 var style = document.documentElement.style;
842 for (var i = 0; i < props.length; i++) {
843 if (props[i] in style) {
850 getTranslateString: function (point) {
851 // on WebKit browsers (Chrome/Safari/iOS Safari/Android) using translate3d instead of translate
852 // makes animation smoother as it ensures HW accel is used. Firefox 13 doesn't care
853 // (same speed either way), Opera 12 doesn't support translate3d
855 var is3d = L.Browser.webkit3d,
856 open = 'translate' + (is3d ? '3d' : '') + '(',
857 close = (is3d ? ',0' : '') + ')';
859 return open + point.x + 'px,' + point.y + 'px' + close;
862 getScaleString: function (scale, origin) {
864 var preTranslateStr = L.DomUtil.getTranslateString(origin.add(origin.multiplyBy(-1 * scale))),
865 scaleStr = ' scale(' + scale + ') ';
867 return preTranslateStr + scaleStr;
870 setPosition: function (el, point, disable3D) { // (HTMLElement, Point[, Boolean])
872 el._leaflet_pos = point;
874 if (!disable3D && L.Browser.any3d) {
875 el.style[L.DomUtil.TRANSFORM] = L.DomUtil.getTranslateString(point);
877 // workaround for Android 2/3 stability (https://github.com/CloudMade/Leaflet/issues/69)
878 if (L.Browser.mobileWebkit3d) {
879 el.style.WebkitBackfaceVisibility = 'hidden';
882 el.style.left = point.x + 'px';
883 el.style.top = point.y + 'px';
887 getPosition: function (el) {
888 // this method is only used for elements previously positioned using setPosition,
889 // so it's safe to cache the position for performance
890 return el._leaflet_pos;
895 // prefix style property names
897 L.DomUtil.TRANSFORM = L.DomUtil.testProp(
898 ['transform', 'WebkitTransform', 'OTransform', 'MozTransform', 'msTransform']);
900 L.DomUtil.TRANSITION = L.DomUtil.testProp(
901 ['transition', 'webkitTransition', 'OTransition', 'MozTransition', 'msTransition']);
903 L.DomUtil.TRANSITION_END =
904 L.DomUtil.TRANSITION === 'webkitTransition' || L.DomUtil.TRANSITION === 'OTransition' ?
905 L.DomUtil.TRANSITION + 'End' : 'transitionend';
909 CM.LatLng represents a geographical point with latitude and longtitude coordinates.
912 L.LatLng = function (rawLat, rawLng, noWrap) { // (Number, Number[, Boolean])
913 var lat = parseFloat(rawLat),
914 lng = parseFloat(rawLng);
916 if (isNaN(lat) || isNaN(lng)) {
917 throw new Error('Invalid LatLng object: (' + rawLat + ', ' + rawLng + ')');
920 if (noWrap !== true) {
921 lat = Math.max(Math.min(lat, 90), -90); // clamp latitude into -90..90
922 lng = (lng + 180) % 360 + ((lng < -180 || lng === 180) ? 180 : -180); // wrap longtitude into -180..180
930 DEG_TO_RAD: Math.PI / 180,
931 RAD_TO_DEG: 180 / Math.PI,
932 MAX_MARGIN: 1.0E-9 // max margin of error for the "equals" check
935 L.LatLng.prototype = {
936 equals: function (obj) { // (LatLng) -> Boolean
937 if (!obj) { return false; }
941 var margin = Math.max(Math.abs(this.lat - obj.lat), Math.abs(this.lng - obj.lng));
942 return margin <= L.LatLng.MAX_MARGIN;
945 toString: function (precision) { // -> String
947 L.Util.formatNum(this.lat, precision) + ', ' +
948 L.Util.formatNum(this.lng, precision) + ')';
951 // Haversine distance formula, see http://en.wikipedia.org/wiki/Haversine_formula
952 distanceTo: function (other) { // (LatLng) -> Number
953 other = L.latLng(other);
955 var R = 6378137, // earth radius in meters
956 d2r = L.LatLng.DEG_TO_RAD,
957 dLat = (other.lat - this.lat) * d2r,
958 dLon = (other.lng - this.lng) * d2r,
959 lat1 = this.lat * d2r,
960 lat2 = other.lat * d2r,
961 sin1 = Math.sin(dLat / 2),
962 sin2 = Math.sin(dLon / 2);
964 var a = sin1 * sin1 + sin2 * sin2 * Math.cos(lat1) * Math.cos(lat2);
966 return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
970 L.latLng = function (a, b, c) { // (LatLng) or ([Number, Number]) or (Number, Number, Boolean)
971 if (a instanceof L.LatLng) {
974 if (a instanceof Array) {
975 return new L.LatLng(a[0], a[1]);
980 return new L.LatLng(a, b, c);
986 * L.LatLngBounds represents a rectangular area on the map in geographical coordinates.
989 L.LatLngBounds = L.Class.extend({
990 initialize: function (southWest, northEast) { // (LatLng, LatLng) or (LatLng[])
991 if (!southWest) { return; }
993 var latlngs = northEast ? [southWest, northEast] : southWest;
995 for (var i = 0, len = latlngs.length; i < len; i++) {
996 this.extend(latlngs[i]);
1000 // extend the bounds to contain the given point or bounds
1001 extend: function (obj) { // (LatLng) or (LatLngBounds)
1002 if (typeof obj[0] === 'number' || obj instanceof L.LatLng) {
1003 obj = L.latLng(obj);
1005 obj = L.latLngBounds(obj);
1008 if (obj instanceof L.LatLng) {
1009 if (!this._southWest && !this._northEast) {
1010 this._southWest = new L.LatLng(obj.lat, obj.lng, true);
1011 this._northEast = new L.LatLng(obj.lat, obj.lng, true);
1013 this._southWest.lat = Math.min(obj.lat, this._southWest.lat);
1014 this._southWest.lng = Math.min(obj.lng, this._southWest.lng);
1016 this._northEast.lat = Math.max(obj.lat, this._northEast.lat);
1017 this._northEast.lng = Math.max(obj.lng, this._northEast.lng);
1019 } else if (obj instanceof L.LatLngBounds) {
1020 this.extend(obj._southWest);
1021 this.extend(obj._northEast);
1026 // extend the bounds by a percentage
1027 pad: function (bufferRatio) { // (Number) -> LatLngBounds
1028 var sw = this._southWest,
1029 ne = this._northEast,
1030 heightBuffer = Math.abs(sw.lat - ne.lat) * bufferRatio,
1031 widthBuffer = Math.abs(sw.lng - ne.lng) * bufferRatio;
1033 return new L.LatLngBounds(
1034 new L.LatLng(sw.lat - heightBuffer, sw.lng - widthBuffer),
1035 new L.LatLng(ne.lat + heightBuffer, ne.lng + widthBuffer));
1038 getCenter: function () { // -> LatLng
1039 return new L.LatLng(
1040 (this._southWest.lat + this._northEast.lat) / 2,
1041 (this._southWest.lng + this._northEast.lng) / 2);
1044 getSouthWest: function () {
1045 return this._southWest;
1048 getNorthEast: function () {
1049 return this._northEast;
1052 getNorthWest: function () {
1053 return new L.LatLng(this._northEast.lat, this._southWest.lng, true);
1056 getSouthEast: function () {
1057 return new L.LatLng(this._southWest.lat, this._northEast.lng, true);
1060 contains: function (obj) { // (LatLngBounds) or (LatLng) -> Boolean
1061 if (typeof obj[0] === 'number' || obj instanceof L.LatLng) {
1062 obj = L.latLng(obj);
1064 obj = L.latLngBounds(obj);
1067 var sw = this._southWest,
1068 ne = this._northEast,
1071 if (obj instanceof L.LatLngBounds) {
1072 sw2 = obj.getSouthWest();
1073 ne2 = obj.getNorthEast();
1078 return (sw2.lat >= sw.lat) && (ne2.lat <= ne.lat) &&
1079 (sw2.lng >= sw.lng) && (ne2.lng <= ne.lng);
1082 intersects: function (bounds) { // (LatLngBounds)
1083 bounds = L.latLngBounds(bounds);
1085 var sw = this._southWest,
1086 ne = this._northEast,
1087 sw2 = bounds.getSouthWest(),
1088 ne2 = bounds.getNorthEast(),
1090 latIntersects = (ne2.lat >= sw.lat) && (sw2.lat <= ne.lat),
1091 lngIntersects = (ne2.lng >= sw.lng) && (sw2.lng <= ne.lng);
1093 return latIntersects && lngIntersects;
1096 toBBoxString: function () {
1097 var sw = this._southWest,
1098 ne = this._northEast;
1100 return [sw.lng, sw.lat, ne.lng, ne.lat].join(',');
1103 equals: function (bounds) { // (LatLngBounds)
1104 if (!bounds) { return false; }
1106 bounds = L.latLngBounds(bounds);
1108 return this._southWest.equals(bounds.getSouthWest()) &&
1109 this._northEast.equals(bounds.getNorthEast());
1112 isValid: function () {
1113 return !!(this._southWest && this._northEast);
1117 //TODO International date line?
1119 L.latLngBounds = function (a, b) { // (LatLngBounds) or (LatLng, LatLng)
1120 if (!a || a instanceof L.LatLngBounds) {
1123 return new L.LatLngBounds(a, b);
1128 * L.Projection contains various geographical projections used by CRS classes.
1135 L.Projection.SphericalMercator = {
1136 MAX_LATITUDE: 85.0511287798,
1138 project: function (latlng) { // (LatLng) -> Point
1139 var d = L.LatLng.DEG_TO_RAD,
1140 max = this.MAX_LATITUDE,
1141 lat = Math.max(Math.min(max, latlng.lat), -max),
1145 y = Math.log(Math.tan((Math.PI / 4) + (y / 2)));
1147 return new L.Point(x, y);
1150 unproject: function (point) { // (Point, Boolean) -> LatLng
1151 var d = L.LatLng.RAD_TO_DEG,
1153 lat = (2 * Math.atan(Math.exp(point.y)) - (Math.PI / 2)) * d;
1155 // TODO refactor LatLng wrapping
1156 return new L.LatLng(lat, lng, true);
1162 L.Projection.LonLat = {
1163 project: function (latlng) {
1164 return new L.Point(latlng.lng, latlng.lat);
1167 unproject: function (point) {
1168 return new L.LatLng(point.y, point.x, true);
1175 latLngToPoint: function (latlng, zoom) { // (LatLng, Number) -> Point
1176 var projectedPoint = this.projection.project(latlng),
1177 scale = this.scale(zoom);
1179 return this.transformation._transform(projectedPoint, scale);
1182 pointToLatLng: function (point, zoom) { // (Point, Number[, Boolean]) -> LatLng
1183 var scale = this.scale(zoom),
1184 untransformedPoint = this.transformation.untransform(point, scale);
1186 return this.projection.unproject(untransformedPoint);
1189 project: function (latlng) {
1190 return this.projection.project(latlng);
1193 scale: function (zoom) {
1194 return 256 * Math.pow(2, zoom);
1200 L.CRS.Simple = L.extend({}, L.CRS, {
1201 projection: L.Projection.LonLat,
1202 transformation: new L.Transformation(1, 0, 1, 0)
1207 L.CRS.EPSG3857 = L.extend({}, L.CRS, {
1210 projection: L.Projection.SphericalMercator,
1211 transformation: new L.Transformation(0.5 / Math.PI, 0.5, -0.5 / Math.PI, 0.5),
1213 project: function (latlng) { // (LatLng) -> Point
1214 var projectedPoint = this.projection.project(latlng),
1215 earthRadius = 6378137;
1216 return projectedPoint.multiplyBy(earthRadius);
1220 L.CRS.EPSG900913 = L.extend({}, L.CRS.EPSG3857, {
1226 L.CRS.EPSG4326 = L.extend({}, L.CRS, {
1229 projection: L.Projection.LonLat,
1230 transformation: new L.Transformation(1 / 360, 0.5, -1 / 360, 0.5)
1235 * L.Map is the central class of the API - it is used to create a map.
1238 L.Map = L.Class.extend({
1240 includes: L.Mixin.Events,
1243 crs: L.CRS.EPSG3857,
1251 fadeAnimation: L.DomUtil.TRANSITION && !L.Browser.android23,
1253 markerZoomAnimation: L.DomUtil.TRANSITION && L.Browser.any3d
1256 initialize: function (id, options) { // (HTMLElement or String, Object)
1257 options = L.setOptions(this, options);
1259 this._initContainer(id);
1264 if (options.maxBounds) {
1265 this.setMaxBounds(options.maxBounds);
1268 if (options.center && options.zoom !== undefined) {
1269 this.setView(L.latLng(options.center), options.zoom, true);
1272 this._initLayers(options.layers);
1276 // public methods that modify map state
1278 // replaced by animation-powered implementation in Map.PanAnimation.js
1279 setView: function (center, zoom) {
1280 this._resetView(L.latLng(center), this._limitZoom(zoom));
1284 setZoom: function (zoom) { // (Number)
1285 return this.setView(this.getCenter(), zoom);
1288 zoomIn: function (delta) {
1289 return this.setZoom(this._zoom + (delta || 1));
1292 zoomOut: function (delta) {
1293 return this.setZoom(this._zoom - (delta || 1));
1296 fitBounds: function (bounds) { // (LatLngBounds)
1297 var zoom = this.getBoundsZoom(bounds);
1298 return this.setView(L.latLngBounds(bounds).getCenter(), zoom);
1301 fitWorld: function () {
1302 var sw = new L.LatLng(-60, -170),
1303 ne = new L.LatLng(85, 179);
1305 return this.fitBounds(new L.LatLngBounds(sw, ne));
1308 panTo: function (center) { // (LatLng)
1309 return this.setView(center, this._zoom);
1312 panBy: function (offset) { // (Point)
1313 // replaced with animated panBy in Map.Animation.js
1314 this.fire('movestart');
1316 this._rawPanBy(L.point(offset));
1319 return this.fire('moveend');
1322 setMaxBounds: function (bounds) {
1323 bounds = L.latLngBounds(bounds);
1325 this.options.maxBounds = bounds;
1328 this._boundsMinZoom = null;
1332 var minZoom = this.getBoundsZoom(bounds, true);
1334 this._boundsMinZoom = minZoom;
1337 if (this._zoom < minZoom) {
1338 this.setView(bounds.getCenter(), minZoom);
1340 this.panInsideBounds(bounds);
1347 panInsideBounds: function (bounds) {
1348 bounds = L.latLngBounds(bounds);
1350 var viewBounds = this.getBounds(),
1351 viewSw = this.project(viewBounds.getSouthWest()),
1352 viewNe = this.project(viewBounds.getNorthEast()),
1353 sw = this.project(bounds.getSouthWest()),
1354 ne = this.project(bounds.getNorthEast()),
1358 if (viewNe.y < ne.y) { // north
1359 dy = ne.y - viewNe.y;
1361 if (viewNe.x > ne.x) { // east
1362 dx = ne.x - viewNe.x;
1364 if (viewSw.y > sw.y) { // south
1365 dy = sw.y - viewSw.y;
1367 if (viewSw.x < sw.x) { // west
1368 dx = sw.x - viewSw.x;
1371 return this.panBy(new L.Point(dx, dy, true));
1374 addLayer: function (layer) {
1375 // TODO method is too big, refactor
1377 var id = L.stamp(layer);
1379 if (this._layers[id]) { return this; }
1381 this._layers[id] = layer;
1383 // TODO getMaxZoom, getMinZoom in ILayer (instead of options)
1384 if (layer.options && !isNaN(layer.options.maxZoom)) {
1385 this._layersMaxZoom = Math.max(this._layersMaxZoom || 0, layer.options.maxZoom);
1387 if (layer.options && !isNaN(layer.options.minZoom)) {
1388 this._layersMinZoom = Math.min(this._layersMinZoom || Infinity, layer.options.minZoom);
1391 // TODO looks ugly, refactor!!!
1392 if (this.options.zoomAnimation && L.TileLayer && (layer instanceof L.TileLayer)) {
1393 this._tileLayersNum++;
1394 this._tileLayersToLoad++;
1395 layer.on('load', this._onTileLayerLoad, this);
1398 this.whenReady(function () {
1400 this.fire('layeradd', {layer: layer});
1406 removeLayer: function (layer) {
1407 var id = L.stamp(layer);
1409 if (!this._layers[id]) { return; }
1411 layer.onRemove(this);
1413 delete this._layers[id];
1415 // TODO looks ugly, refactor
1416 if (this.options.zoomAnimation && L.TileLayer && (layer instanceof L.TileLayer)) {
1417 this._tileLayersNum--;
1418 this._tileLayersToLoad--;
1419 layer.off('load', this._onTileLayerLoad, this);
1422 return this.fire('layerremove', {layer: layer});
1425 hasLayer: function (layer) {
1426 var id = L.stamp(layer);
1427 return this._layers.hasOwnProperty(id);
1430 invalidateSize: function (animate) {
1431 var oldSize = this.getSize();
1433 this._sizeChanged = true;
1435 if (this.options.maxBounds) {
1436 this.setMaxBounds(this.options.maxBounds);
1439 if (!this._loaded) { return this; }
1441 var offset = oldSize._subtract(this.getSize())._divideBy(2)._round();
1443 if (animate === true) {
1446 this._rawPanBy(offset);
1450 clearTimeout(this._sizeTimer);
1451 this._sizeTimer = setTimeout(L.bind(this.fire, this, 'moveend'), 200);
1456 // TODO handler.addTo
1457 addHandler: function (name, HandlerClass) {
1458 if (!HandlerClass) { return; }
1460 this[name] = new HandlerClass(this);
1462 if (this.options[name]) {
1463 this[name].enable();
1470 // public methods for getting map state
1472 getCenter: function () { // (Boolean) -> LatLng
1473 return this.layerPointToLatLng(this._getCenterLayerPoint());
1476 getZoom: function () {
1480 getBounds: function () {
1481 var bounds = this.getPixelBounds(),
1482 sw = this.unproject(bounds.getBottomLeft()),
1483 ne = this.unproject(bounds.getTopRight());
1485 return new L.LatLngBounds(sw, ne);
1488 getMinZoom: function () {
1489 var z1 = this.options.minZoom || 0,
1490 z2 = this._layersMinZoom || 0,
1491 z3 = this._boundsMinZoom || 0;
1493 return Math.max(z1, z2, z3);
1496 getMaxZoom: function () {
1497 var z1 = this.options.maxZoom === undefined ? Infinity : this.options.maxZoom,
1498 z2 = this._layersMaxZoom === undefined ? Infinity : this._layersMaxZoom;
1500 return Math.min(z1, z2);
1503 getBoundsZoom: function (bounds, inside) { // (LatLngBounds, Boolean) -> Number
1504 bounds = L.latLngBounds(bounds);
1506 var size = this.getSize(),
1507 zoom = this.options.minZoom || 0,
1508 maxZoom = this.getMaxZoom(),
1509 ne = bounds.getNorthEast(),
1510 sw = bounds.getSouthWest(),
1514 zoomNotFound = true;
1522 nePoint = this.project(ne, zoom);
1523 swPoint = this.project(sw, zoom);
1525 boundsSize = new L.Point(
1526 Math.abs(nePoint.x - swPoint.x),
1527 Math.abs(swPoint.y - nePoint.y));
1530 zoomNotFound = boundsSize.x <= size.x && boundsSize.y <= size.y;
1532 zoomNotFound = boundsSize.x < size.x || boundsSize.y < size.y;
1534 } while (zoomNotFound && zoom <= maxZoom);
1536 if (zoomNotFound && inside) {
1540 return inside ? zoom : zoom - 1;
1543 getSize: function () {
1544 if (!this._size || this._sizeChanged) {
1545 this._size = new L.Point(
1546 this._container.clientWidth,
1547 this._container.clientHeight);
1549 this._sizeChanged = false;
1551 return this._size.clone();
1554 getPixelBounds: function () {
1555 var topLeftPoint = this._getTopLeftPoint();
1556 return new L.Bounds(topLeftPoint, topLeftPoint.add(this.getSize()));
1559 getPixelOrigin: function () {
1560 return this._initialTopLeftPoint;
1563 getPanes: function () {
1567 getContainer: function () {
1568 return this._container;
1572 // TODO replace with universal implementation after refactoring projections
1574 getZoomScale: function (toZoom) {
1575 var crs = this.options.crs;
1576 return crs.scale(toZoom) / crs.scale(this._zoom);
1579 getScaleZoom: function (scale) {
1580 return this._zoom + (Math.log(scale) / Math.LN2);
1584 // conversion methods
1586 project: function (latlng, zoom) { // (LatLng[, Number]) -> Point
1587 zoom = zoom === undefined ? this._zoom : zoom;
1588 return this.options.crs.latLngToPoint(L.latLng(latlng), zoom);
1591 unproject: function (point, zoom) { // (Point[, Number]) -> LatLng
1592 zoom = zoom === undefined ? this._zoom : zoom;
1593 return this.options.crs.pointToLatLng(L.point(point), zoom);
1596 layerPointToLatLng: function (point) { // (Point)
1597 var projectedPoint = L.point(point).add(this._initialTopLeftPoint);
1598 return this.unproject(projectedPoint);
1601 latLngToLayerPoint: function (latlng) { // (LatLng)
1602 var projectedPoint = this.project(L.latLng(latlng))._round();
1603 return projectedPoint._subtract(this._initialTopLeftPoint);
1606 containerPointToLayerPoint: function (point) { // (Point)
1607 return L.point(point).subtract(this._getMapPanePos());
1610 layerPointToContainerPoint: function (point) { // (Point)
1611 return L.point(point).add(this._getMapPanePos());
1614 containerPointToLatLng: function (point) {
1615 var layerPoint = this.containerPointToLayerPoint(L.point(point));
1616 return this.layerPointToLatLng(layerPoint);
1619 latLngToContainerPoint: function (latlng) {
1620 return this.layerPointToContainerPoint(this.latLngToLayerPoint(L.latLng(latlng)));
1623 mouseEventToContainerPoint: function (e) { // (MouseEvent)
1624 return L.DomEvent.getMousePosition(e, this._container);
1627 mouseEventToLayerPoint: function (e) { // (MouseEvent)
1628 return this.containerPointToLayerPoint(this.mouseEventToContainerPoint(e));
1631 mouseEventToLatLng: function (e) { // (MouseEvent)
1632 return this.layerPointToLatLng(this.mouseEventToLayerPoint(e));
1636 // map initialization methods
1638 _initContainer: function (id) {
1639 var container = this._container = L.DomUtil.get(id);
1641 if (container._leaflet) {
1642 throw new Error("Map container is already initialized.");
1645 container._leaflet = true;
1648 _initLayout: function () {
1649 var container = this._container;
1651 container.innerHTML = '';
1652 L.DomUtil.addClass(container, 'leaflet-container');
1654 if (L.Browser.touch) {
1655 L.DomUtil.addClass(container, 'leaflet-touch');
1658 if (this.options.fadeAnimation) {
1659 L.DomUtil.addClass(container, 'leaflet-fade-anim');
1662 var position = L.DomUtil.getStyle(container, 'position');
1664 if (position !== 'absolute' && position !== 'relative' && position !== 'fixed') {
1665 container.style.position = 'relative';
1670 if (this._initControlPos) {
1671 this._initControlPos();
1675 _initPanes: function () {
1676 var panes = this._panes = {};
1678 this._mapPane = panes.mapPane = this._createPane('leaflet-map-pane', this._container);
1680 this._tilePane = panes.tilePane = this._createPane('leaflet-tile-pane', this._mapPane);
1681 panes.objectsPane = this._createPane('leaflet-objects-pane', this._mapPane);
1682 panes.shadowPane = this._createPane('leaflet-shadow-pane');
1683 panes.overlayPane = this._createPane('leaflet-overlay-pane');
1684 panes.markerPane = this._createPane('leaflet-marker-pane');
1685 panes.popupPane = this._createPane('leaflet-popup-pane');
1687 var zoomHide = ' leaflet-zoom-hide';
1689 if (!this.options.markerZoomAnimation) {
1690 L.DomUtil.addClass(panes.markerPane, zoomHide);
1691 L.DomUtil.addClass(panes.shadowPane, zoomHide);
1692 L.DomUtil.addClass(panes.popupPane, zoomHide);
1696 _createPane: function (className, container) {
1697 return L.DomUtil.create('div', className, container || this._panes.objectsPane);
1702 _initHooks: function () {
1704 for (i = 0, len = this._initializers.length; i < len; i++) {
1705 this._initializers[i].call(this);
1709 _initLayers: function (layers) {
1710 layers = layers ? (layers instanceof Array ? layers : [layers]) : [];
1713 this._tileLayersNum = 0;
1717 for (i = 0, len = layers.length; i < len; i++) {
1718 this.addLayer(layers[i]);
1723 // private methods that modify map state
1725 _resetView: function (center, zoom, preserveMapOffset, afterZoomAnim) {
1727 var zoomChanged = (this._zoom !== zoom);
1729 if (!afterZoomAnim) {
1730 this.fire('movestart');
1733 this.fire('zoomstart');
1739 this._initialTopLeftPoint = this._getNewTopLeftPoint(center);
1741 if (!preserveMapOffset) {
1742 L.DomUtil.setPosition(this._mapPane, new L.Point(0, 0));
1744 this._initialTopLeftPoint._add(this._getMapPanePos());
1747 this._tileLayersToLoad = this._tileLayersNum;
1749 var loading = !this._loaded;
1750 this._loaded = true;
1752 this.fire('viewreset', {hard: !preserveMapOffset});
1756 if (zoomChanged || afterZoomAnim) {
1757 this.fire('zoomend');
1760 this.fire('moveend', {hard: !preserveMapOffset});
1767 _rawPanBy: function (offset) {
1768 L.DomUtil.setPosition(this._mapPane, this._getMapPanePos().subtract(offset));
1774 _initEvents: function () {
1775 if (!L.DomEvent) { return; }
1777 L.DomEvent.on(this._container, 'click', this._onMouseClick, this);
1779 var events = ['dblclick', 'mousedown', 'mouseup', 'mouseenter',
1780 'mouseleave', 'mousemove', 'contextmenu'],
1783 for (i = 0, len = events.length; i < len; i++) {
1784 L.DomEvent.on(this._container, events[i], this._fireMouseEvent, this);
1787 if (this.options.trackResize) {
1788 L.DomEvent.on(window, 'resize', this._onResize, this);
1792 _onResize: function () {
1793 L.Util.cancelAnimFrame(this._resizeRequest);
1794 this._resizeRequest = L.Util.requestAnimFrame(
1795 this.invalidateSize, this, false, this._container);
1798 _onMouseClick: function (e) {
1799 if (!this._loaded || (this.dragging && this.dragging.moved())) { return; }
1801 this.fire('preclick');
1802 this._fireMouseEvent(e);
1805 _fireMouseEvent: function (e) {
1806 if (!this._loaded) { return; }
1810 type = (type === 'mouseenter' ? 'mouseover' : (type === 'mouseleave' ? 'mouseout' : type));
1812 if (!this.hasEventListeners(type)) { return; }
1814 if (type === 'contextmenu') {
1815 L.DomEvent.preventDefault(e);
1818 var containerPoint = this.mouseEventToContainerPoint(e),
1819 layerPoint = this.containerPointToLayerPoint(containerPoint),
1820 latlng = this.layerPointToLatLng(layerPoint);
1824 layerPoint: layerPoint,
1825 containerPoint: containerPoint,
1830 _onTileLayerLoad: function () {
1831 // TODO super-ugly, refactor!!!
1832 // clear scaled tiles after all new tiles are loaded (for performance)
1833 this._tileLayersToLoad--;
1834 if (this._tileLayersNum && !this._tileLayersToLoad && this._tileBg) {
1835 clearTimeout(this._clearTileBgTimer);
1836 this._clearTileBgTimer = setTimeout(L.bind(this._clearTileBg, this), 500);
1840 whenReady: function (callback, context) {
1842 callback.call(context || this, this);
1844 this.on('load', callback, context);
1850 // private methods for getting map state
1852 _getMapPanePos: function () {
1853 return L.DomUtil.getPosition(this._mapPane);
1856 _getTopLeftPoint: function () {
1857 if (!this._loaded) {
1858 throw new Error('Set map center and zoom first.');
1861 return this._initialTopLeftPoint.subtract(this._getMapPanePos());
1864 _getNewTopLeftPoint: function (center, zoom) {
1865 var viewHalf = this.getSize()._divideBy(2);
1866 // TODO round on display, not calculation to increase precision?
1867 return this.project(center, zoom)._subtract(viewHalf)._round();
1870 _latLngToNewLayerPoint: function (latlng, newZoom, newCenter) {
1871 var topLeft = this._getNewTopLeftPoint(newCenter, newZoom).add(this._getMapPanePos());
1872 return this.project(latlng, newZoom)._subtract(topLeft);
1875 _getCenterLayerPoint: function () {
1876 return this.containerPointToLayerPoint(this.getSize()._divideBy(2));
1879 _getCenterOffset: function (center) {
1880 return this.latLngToLayerPoint(center).subtract(this._getCenterLayerPoint());
1883 _limitZoom: function (zoom) {
1884 var min = this.getMinZoom(),
1885 max = this.getMaxZoom();
1887 return Math.max(min, Math.min(max, zoom));
1891 L.Map.addInitHook = function (fn) {
1892 var args = Array.prototype.slice.call(arguments, 1);
1894 var init = typeof fn === 'function' ? fn : function () {
1895 this[fn].apply(this, args);
1898 this.prototype._initializers.push(init);
1901 L.map = function (id, options) {
1902 return new L.Map(id, options);
1907 L.Projection.Mercator = {
1908 MAX_LATITUDE: 85.0840591556,
1910 R_MINOR: 6356752.3142,
1913 project: function (latlng) { // (LatLng) -> Point
1914 var d = L.LatLng.DEG_TO_RAD,
1915 max = this.MAX_LATITUDE,
1916 lat = Math.max(Math.min(max, latlng.lat), -max),
1919 x = latlng.lng * d * r,
1922 eccent = Math.sqrt(1.0 - tmp * tmp),
1923 con = eccent * Math.sin(y);
1925 con = Math.pow((1 - con) / (1 + con), eccent * 0.5);
1927 var ts = Math.tan(0.5 * ((Math.PI * 0.5) - y)) / con;
1928 y = -r2 * Math.log(ts);
1930 return new L.Point(x, y);
1933 unproject: function (point) { // (Point, Boolean) -> LatLng
1934 var d = L.LatLng.RAD_TO_DEG,
1937 lng = point.x * d / r,
1939 eccent = Math.sqrt(1 - (tmp * tmp)),
1940 ts = Math.exp(- point.y / r2),
1941 phi = (Math.PI / 2) - 2 * Math.atan(ts),
1948 while ((Math.abs(dphi) > tol) && (--i > 0)) {
1949 con = eccent * Math.sin(phi);
1950 dphi = (Math.PI / 2) - 2 * Math.atan(ts *
1951 Math.pow((1.0 - con) / (1.0 + con), 0.5 * eccent)) - phi;
1955 return new L.LatLng(phi * d, lng, true);
1961 L.CRS.EPSG3395 = L.extend({}, L.CRS, {
1964 projection: L.Projection.Mercator,
1966 transformation: (function () {
1967 var m = L.Projection.Mercator,
1971 return new L.Transformation(0.5 / (Math.PI * r), 0.5, -0.5 / (Math.PI * r2), 0.5);
1977 * L.TileLayer is used for standard xyz-numbered tile layers.
1980 L.TileLayer = L.Class.extend({
1981 includes: L.Mixin.Events,
1992 /* (undefined works too)
1995 continuousWorld: false,
1998 detectRetina: false,
2001 unloadInvisibleTiles: L.Browser.mobile,
2002 updateWhenIdle: L.Browser.mobile
2005 initialize: function (url, options) {
2006 options = L.setOptions(this, options);
2008 // detecting retina displays, adjusting tileSize and zoom levels
2009 if (options.detectRetina && L.Browser.retina && options.maxZoom > 0) {
2011 options.tileSize = Math.floor(options.tileSize / 2);
2012 options.zoomOffset++;
2014 if (options.minZoom > 0) {
2017 this.options.maxZoom--;
2022 var subdomains = this.options.subdomains;
2024 if (typeof subdomains === 'string') {
2025 this.options.subdomains = subdomains.split('');
2029 onAdd: function (map) {
2032 // create a container div for tiles
2033 this._initContainer();
2035 // create an image to clone for tiles
2036 this._createTileProto();
2040 'viewreset': this._resetCallback,
2041 'moveend': this._update
2044 if (!this.options.updateWhenIdle) {
2045 this._limitedUpdate = L.Util.limitExecByInterval(this._update, 150, this);
2046 map.on('move', this._limitedUpdate, this);
2053 addTo: function (map) {
2058 onRemove: function (map) {
2059 map._panes.tilePane.removeChild(this._container);
2062 'viewreset': this._resetCallback,
2063 'moveend': this._update
2066 if (!this.options.updateWhenIdle) {
2067 map.off('move', this._limitedUpdate, this);
2070 this._container = null;
2074 bringToFront: function () {
2075 var pane = this._map._panes.tilePane;
2077 if (this._container) {
2078 pane.appendChild(this._container);
2079 this._setAutoZIndex(pane, Math.max);
2085 bringToBack: function () {
2086 var pane = this._map._panes.tilePane;
2088 if (this._container) {
2089 pane.insertBefore(this._container, pane.firstChild);
2090 this._setAutoZIndex(pane, Math.min);
2096 getAttribution: function () {
2097 return this.options.attribution;
2100 setOpacity: function (opacity) {
2101 this.options.opacity = opacity;
2104 this._updateOpacity();
2110 setZIndex: function (zIndex) {
2111 this.options.zIndex = zIndex;
2112 this._updateZIndex();
2117 setUrl: function (url, noRedraw) {
2127 redraw: function () {
2129 this._map._panes.tilePane.empty = false;
2136 _updateZIndex: function () {
2137 if (this._container && this.options.zIndex !== undefined) {
2138 this._container.style.zIndex = this.options.zIndex;
2142 _setAutoZIndex: function (pane, compare) {
2144 var layers = pane.getElementsByClassName('leaflet-layer'),
2145 edgeZIndex = -compare(Infinity, -Infinity), // -Ifinity for max, Infinity for min
2148 for (i = 0, len = layers.length; i < len; i++) {
2150 if (layers[i] !== this._container) {
2151 zIndex = parseInt(layers[i].style.zIndex, 10);
2153 if (!isNaN(zIndex)) {
2154 edgeZIndex = compare(edgeZIndex, zIndex);
2159 this.options.zIndex = this._container.style.zIndex =
2160 (isFinite(edgeZIndex) ? edgeZIndex : 0) + compare(1, -1);
2163 _updateOpacity: function () {
2164 L.DomUtil.setOpacity(this._container, this.options.opacity);
2166 // stupid webkit hack to force redrawing of tiles
2168 tiles = this._tiles;
2170 if (L.Browser.webkit) {
2172 if (tiles.hasOwnProperty(i)) {
2173 tiles[i].style.webkitTransform += ' translate(0,0)';
2179 _initContainer: function () {
2180 var tilePane = this._map._panes.tilePane;
2182 if (!this._container || tilePane.empty) {
2183 this._container = L.DomUtil.create('div', 'leaflet-layer');
2185 this._updateZIndex();
2187 tilePane.appendChild(this._container);
2189 if (this.options.opacity < 1) {
2190 this._updateOpacity();
2195 _resetCallback: function (e) {
2196 this._reset(e.hard);
2199 _reset: function (clearOldContainer) {
2200 var tiles = this._tiles;
2202 for (var key in tiles) {
2203 if (tiles.hasOwnProperty(key)) {
2204 this.fire('tileunload', {tile: tiles[key]});
2209 this._tilesToLoad = 0;
2211 if (this.options.reuseTiles) {
2212 this._unusedTiles = [];
2215 if (clearOldContainer && this._container) {
2216 this._container.innerHTML = "";
2219 this._initContainer();
2222 _update: function (e) {
2224 if (!this._map) { return; }
2226 var bounds = this._map.getPixelBounds(),
2227 zoom = this._map.getZoom(),
2228 tileSize = this.options.tileSize;
2230 if (zoom > this.options.maxZoom || zoom < this.options.minZoom) {
2234 var nwTilePoint = new L.Point(
2235 Math.floor(bounds.min.x / tileSize),
2236 Math.floor(bounds.min.y / tileSize)),
2238 seTilePoint = new L.Point(
2239 Math.floor(bounds.max.x / tileSize),
2240 Math.floor(bounds.max.y / tileSize)),
2242 tileBounds = new L.Bounds(nwTilePoint, seTilePoint);
2244 this._addTilesFromCenterOut(tileBounds);
2246 if (this.options.unloadInvisibleTiles || this.options.reuseTiles) {
2247 this._removeOtherTiles(tileBounds);
2251 _addTilesFromCenterOut: function (bounds) {
2253 center = bounds.getCenter();
2257 for (j = bounds.min.y; j <= bounds.max.y; j++) {
2258 for (i = bounds.min.x; i <= bounds.max.x; i++) {
2259 point = new L.Point(i, j);
2261 if (this._tileShouldBeLoaded(point)) {
2267 var tilesToLoad = queue.length;
2269 if (tilesToLoad === 0) { return; }
2271 // load tiles in order of their distance to center
2272 queue.sort(function (a, b) {
2273 return a.distanceTo(center) - b.distanceTo(center);
2276 var fragment = document.createDocumentFragment();
2278 // if its the first batch of tiles to load
2279 if (!this._tilesToLoad) {
2280 this.fire('loading');
2283 this._tilesToLoad += tilesToLoad;
2285 for (i = 0; i < tilesToLoad; i++) {
2286 this._addTile(queue[i], fragment);
2289 this._container.appendChild(fragment);
2292 _tileShouldBeLoaded: function (tilePoint) {
2293 if ((tilePoint.x + ':' + tilePoint.y) in this._tiles) {
2294 return false; // already loaded
2297 if (!this.options.continuousWorld) {
2298 var limit = this._getWrapTileNum();
2300 if (this.options.noWrap && (tilePoint.x < 0 || tilePoint.x >= limit) ||
2301 tilePoint.y < 0 || tilePoint.y >= limit) {
2302 return false; // exceeds world bounds
2309 _removeOtherTiles: function (bounds) {
2310 var kArr, x, y, key;
2312 for (key in this._tiles) {
2313 if (this._tiles.hasOwnProperty(key)) {
2314 kArr = key.split(':');
2315 x = parseInt(kArr[0], 10);
2316 y = parseInt(kArr[1], 10);
2318 // remove tile if it's out of bounds
2319 if (x < bounds.min.x || x > bounds.max.x || y < bounds.min.y || y > bounds.max.y) {
2320 this._removeTile(key);
2326 _removeTile: function (key) {
2327 var tile = this._tiles[key];
2329 this.fire("tileunload", {tile: tile, url: tile.src});
2331 if (this.options.reuseTiles) {
2332 L.DomUtil.removeClass(tile, 'leaflet-tile-loaded');
2333 this._unusedTiles.push(tile);
2335 } else if (tile.parentNode === this._container) {
2336 this._container.removeChild(tile);
2339 // for https://github.com/CloudMade/Leaflet/issues/137
2340 if (!L.Browser.android) {
2341 tile.src = L.Util.emptyImageUrl;
2344 delete this._tiles[key];
2347 _addTile: function (tilePoint, container) {
2348 var tilePos = this._getTilePos(tilePoint);
2350 // get unused tile - or create a new tile
2351 var tile = this._getTile();
2354 Chrome 20 layouts much faster with top/left (verify with timeline, frames)
2355 Android 4 browser has display issues with top/left and requires transform instead
2356 Android 3 browser not tested
2357 Android 2 browser requires top/left or tiles disappear on load or first drag
2358 (reappear after zoom) https://github.com/CloudMade/Leaflet/issues/866
2359 (other browsers don't currently care) - see debug/hacks/jitter.html for an example
2361 L.DomUtil.setPosition(tile, tilePos, L.Browser.chrome || L.Browser.android23);
2363 this._tiles[tilePoint.x + ':' + tilePoint.y] = tile;
2365 this._loadTile(tile, tilePoint);
2367 if (tile.parentNode !== this._container) {
2368 container.appendChild(tile);
2372 _getZoomForUrl: function () {
2374 var options = this.options,
2375 zoom = this._map.getZoom();
2377 if (options.zoomReverse) {
2378 zoom = options.maxZoom - zoom;
2381 return zoom + options.zoomOffset;
2384 _getTilePos: function (tilePoint) {
2385 var origin = this._map.getPixelOrigin(),
2386 tileSize = this.options.tileSize;
2388 return tilePoint.multiplyBy(tileSize).subtract(origin);
2391 // image-specific code (override to implement e.g. Canvas or SVG tile layer)
2393 getTileUrl: function (tilePoint) {
2394 this._adjustTilePoint(tilePoint);
2396 return L.Util.template(this._url, L.extend({
2397 s: this._getSubdomain(tilePoint),
2398 z: this._getZoomForUrl(),
2404 _getWrapTileNum: function () {
2405 // TODO refactor, limit is not valid for non-standard projections
2406 return Math.pow(2, this._getZoomForUrl());
2409 _adjustTilePoint: function (tilePoint) {
2411 var limit = this._getWrapTileNum();
2413 // wrap tile coordinates
2414 if (!this.options.continuousWorld && !this.options.noWrap) {
2415 tilePoint.x = ((tilePoint.x % limit) + limit) % limit;
2418 if (this.options.tms) {
2419 tilePoint.y = limit - tilePoint.y - 1;
2423 _getSubdomain: function (tilePoint) {
2424 var index = (tilePoint.x + tilePoint.y) % this.options.subdomains.length;
2425 return this.options.subdomains[index];
2428 _createTileProto: function () {
2429 var img = this._tileImg = L.DomUtil.create('img', 'leaflet-tile');
2430 img.style.width = img.style.height = this.options.tileSize + 'px';
2431 img.galleryimg = 'no';
2434 _getTile: function () {
2435 if (this.options.reuseTiles && this._unusedTiles.length > 0) {
2436 var tile = this._unusedTiles.pop();
2437 this._resetTile(tile);
2440 return this._createTile();
2443 _resetTile: function (tile) {
2444 // Override if data stored on a tile needs to be cleaned up before reuse
2447 _createTile: function () {
2448 var tile = this._tileImg.cloneNode(false);
2449 tile.onselectstart = tile.onmousemove = L.Util.falseFn;
2453 _loadTile: function (tile, tilePoint) {
2455 tile.onload = this._tileOnLoad;
2456 tile.onerror = this._tileOnError;
2458 tile.src = this.getTileUrl(tilePoint);
2461 _tileLoaded: function () {
2462 this._tilesToLoad--;
2463 if (!this._tilesToLoad) {
2468 _tileOnLoad: function (e) {
2469 var layer = this._layer;
2471 //Only if we are loading an actual image
2472 if (this.src !== L.Util.emptyImageUrl) {
2473 L.DomUtil.addClass(this, 'leaflet-tile-loaded');
2475 layer.fire('tileload', {
2481 layer._tileLoaded();
2484 _tileOnError: function (e) {
2485 var layer = this._layer;
2487 layer.fire('tileerror', {
2492 var newUrl = layer.options.errorTileUrl;
2497 layer._tileLoaded();
2501 L.tileLayer = function (url, options) {
2502 return new L.TileLayer(url, options);
2506 L.TileLayer.WMS = L.TileLayer.extend({
2514 format: 'image/jpeg',
2518 initialize: function (url, options) { // (String, Object)
2522 var wmsParams = L.extend({}, this.defaultWmsParams);
2524 if (options.detectRetina && L.Browser.retina) {
2525 wmsParams.width = wmsParams.height = this.options.tileSize * 2;
2527 wmsParams.width = wmsParams.height = this.options.tileSize;
2530 for (var i in options) {
2531 // all keys that are not TileLayer options go to WMS params
2532 if (!this.options.hasOwnProperty(i)) {
2533 wmsParams[i] = options[i];
2537 this.wmsParams = wmsParams;
2539 L.setOptions(this, options);
2542 onAdd: function (map) {
2544 var projectionKey = parseFloat(this.wmsParams.version) >= 1.3 ? 'crs' : 'srs';
2545 this.wmsParams[projectionKey] = map.options.crs.code;
2547 L.TileLayer.prototype.onAdd.call(this, map);
2550 getTileUrl: function (tilePoint, zoom) { // (Point, Number) -> String
2552 var map = this._map,
2553 crs = map.options.crs,
2554 tileSize = this.options.tileSize,
2556 nwPoint = tilePoint.multiplyBy(tileSize),
2557 sePoint = nwPoint.add(new L.Point(tileSize, tileSize)),
2559 nw = crs.project(map.unproject(nwPoint, zoom)),
2560 se = crs.project(map.unproject(sePoint, zoom)),
2562 bbox = [nw.x, se.y, se.x, nw.y].join(','),
2564 url = L.Util.template(this._url, {s: this._getSubdomain(tilePoint)});
2566 return url + L.Util.getParamString(this.wmsParams) + "&bbox=" + bbox;
2569 setParams: function (params, noRedraw) {
2571 L.extend(this.wmsParams, params);
2581 L.tileLayer.wms = function (url, options) {
2582 return new L.TileLayer.WMS(url, options);
2586 L.TileLayer.Canvas = L.TileLayer.extend({
2591 initialize: function (options) {
2592 L.setOptions(this, options);
2595 redraw: function () {
2596 var tiles = this._tiles;
2598 for (var i in tiles) {
2599 if (tiles.hasOwnProperty(i)) {
2600 this._redrawTile(tiles[i]);
2605 _redrawTile: function (tile) {
2606 this.drawTile(tile, tile._tilePoint, this._map._zoom);
2609 _createTileProto: function () {
2610 var proto = this._canvasProto = L.DomUtil.create('canvas', 'leaflet-tile');
2611 proto.width = proto.height = this.options.tileSize;
2614 _createTile: function () {
2615 var tile = this._canvasProto.cloneNode(false);
2616 tile.onselectstart = tile.onmousemove = L.Util.falseFn;
2620 _loadTile: function (tile, tilePoint) {
2622 tile._tilePoint = tilePoint;
2624 this._redrawTile(tile);
2626 if (!this.options.async) {
2627 this.tileDrawn(tile);
2631 drawTile: function (tile, tilePoint) {
2632 // override with rendering code
2635 tileDrawn: function (tile) {
2636 this._tileOnLoad.call(tile);
2641 L.tileLayer.canvas = function (options) {
2642 return new L.TileLayer.Canvas(options);
2646 L.ImageOverlay = L.Class.extend({
2647 includes: L.Mixin.Events,
2653 initialize: function (url, bounds, options) { // (String, LatLngBounds, Object)
2655 this._bounds = L.latLngBounds(bounds);
2657 L.setOptions(this, options);
2660 onAdd: function (map) {
2667 map._panes.overlayPane.appendChild(this._image);
2669 map.on('viewreset', this._reset, this);
2671 if (map.options.zoomAnimation && L.Browser.any3d) {
2672 map.on('zoomanim', this._animateZoom, this);
2678 onRemove: function (map) {
2679 map.getPanes().overlayPane.removeChild(this._image);
2681 map.off('viewreset', this._reset, this);
2683 if (map.options.zoomAnimation) {
2684 map.off('zoomanim', this._animateZoom, this);
2688 addTo: function (map) {
2693 setOpacity: function (opacity) {
2694 this.options.opacity = opacity;
2695 this._updateOpacity();
2699 // TODO remove bringToFront/bringToBack duplication from TileLayer/Path
2700 bringToFront: function () {
2702 this._map._panes.overlayPane.appendChild(this._image);
2707 bringToBack: function () {
2708 var pane = this._map._panes.overlayPane;
2710 pane.insertBefore(this._image, pane.firstChild);
2715 _initImage: function () {
2716 this._image = L.DomUtil.create('img', 'leaflet-image-layer');
2718 if (this._map.options.zoomAnimation && L.Browser.any3d) {
2719 L.DomUtil.addClass(this._image, 'leaflet-zoom-animated');
2721 L.DomUtil.addClass(this._image, 'leaflet-zoom-hide');
2724 this._updateOpacity();
2726 //TODO createImage util method to remove duplication
2727 L.extend(this._image, {
2729 onselectstart: L.Util.falseFn,
2730 onmousemove: L.Util.falseFn,
2731 onload: L.bind(this._onImageLoad, this),
2736 _animateZoom: function (e) {
2737 var map = this._map,
2738 image = this._image,
2739 scale = map.getZoomScale(e.zoom),
2740 nw = this._bounds.getNorthWest(),
2741 se = this._bounds.getSouthEast(),
2743 topLeft = map._latLngToNewLayerPoint(nw, e.zoom, e.center),
2744 size = map._latLngToNewLayerPoint(se, e.zoom, e.center)._subtract(topLeft),
2745 currentSize = map.latLngToLayerPoint(se)._subtract(map.latLngToLayerPoint(nw)),
2746 origin = topLeft._add(size._subtract(currentSize)._divideBy(2));
2748 image.style[L.DomUtil.TRANSFORM] =
2749 L.DomUtil.getTranslateString(origin) + ' scale(' + scale + ') ';
2752 _reset: function () {
2753 var image = this._image,
2754 topLeft = this._map.latLngToLayerPoint(this._bounds.getNorthWest()),
2755 size = this._map.latLngToLayerPoint(this._bounds.getSouthEast())._subtract(topLeft);
2757 L.DomUtil.setPosition(image, topLeft);
2759 image.style.width = size.x + 'px';
2760 image.style.height = size.y + 'px';
2763 _onImageLoad: function () {
2767 _updateOpacity: function () {
2768 L.DomUtil.setOpacity(this._image, this.options.opacity);
2772 L.imageOverlay = function (url, bounds, options) {
2773 return new L.ImageOverlay(url, bounds, options);
2777 L.Icon = L.Class.extend({
2780 iconUrl: (String) (required)
2781 iconSize: (Point) (can be set through CSS)
2782 iconAnchor: (Point) (centered by default, can be set in CSS with negative margins)
2783 popupAnchor: (Point) (if not specified, popup opens in the anchor point)
2784 shadowUrl: (Point) (no shadow by default)
2786 shadowAnchor: (Point)
2791 initialize: function (options) {
2792 L.setOptions(this, options);
2795 createIcon: function () {
2796 return this._createIcon('icon');
2799 createShadow: function () {
2800 return this._createIcon('shadow');
2803 _createIcon: function (name) {
2804 var src = this._getIconUrl(name);
2807 if (name === 'icon') {
2808 throw new Error("iconUrl not set in Icon options (see the docs).");
2813 var img = this._createImg(src);
2814 this._setIconStyles(img, name);
2819 _setIconStyles: function (img, name) {
2820 var options = this.options,
2821 size = L.point(options[name + 'Size']),
2824 if (name === 'shadow') {
2825 anchor = L.point(options.shadowAnchor || options.iconAnchor);
2827 anchor = L.point(options.iconAnchor);
2830 if (!anchor && size) {
2831 anchor = size.divideBy(2, true);
2834 img.className = 'leaflet-marker-' + name + ' ' + options.className;
2837 img.style.marginLeft = (-anchor.x) + 'px';
2838 img.style.marginTop = (-anchor.y) + 'px';
2842 img.style.width = size.x + 'px';
2843 img.style.height = size.y + 'px';
2847 _createImg: function (src) {
2850 if (!L.Browser.ie6) {
2851 el = document.createElement('img');
2854 el = document.createElement('div');
2856 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '")';
2861 _getIconUrl: function (name) {
2862 return this.options[name + 'Url'];
2866 L.icon = function (options) {
2867 return new L.Icon(options);
2872 L.Icon.Default = L.Icon.extend({
2875 iconSize: new L.Point(25, 41),
2876 iconAnchor: new L.Point(12, 41),
2877 popupAnchor: new L.Point(1, -34),
2879 shadowSize: new L.Point(41, 41)
2882 _getIconUrl: function (name) {
2883 var key = name + 'Url';
2885 if (this.options[key]) {
2886 return this.options[key];
2889 var path = L.Icon.Default.imagePath;
2892 throw new Error("Couldn't autodetect L.Icon.Default.imagePath, set it manually.");
2895 return path + '/marker-' + name + '.png';
2899 L.Icon.Default.imagePath = (function () {
2900 var scripts = document.getElementsByTagName('script'),
2901 leafletRe = /\/?leaflet[\-\._]?([\w\-\._]*)\.js\??/;
2903 var i, len, src, matches;
2905 for (i = 0, len = scripts.length; i < len; i++) {
2906 src = scripts[i].src;
2907 matches = src.match(leafletRe);
2910 return src.split(leafletRe)[0] + '/images';
2917 * L.Marker is used to display clickable/draggable icons on the map.
2920 L.Marker = L.Class.extend({
2922 includes: L.Mixin.Events,
2925 icon: new L.Icon.Default(),
2935 initialize: function (latlng, options) {
2936 L.setOptions(this, options);
2937 this._latlng = L.latLng(latlng);
2940 onAdd: function (map) {
2943 map.on('viewreset', this.update, this);
2948 if (map.options.zoomAnimation && map.options.markerZoomAnimation) {
2949 map.on('zoomanim', this._animateZoom, this);
2953 addTo: function (map) {
2958 onRemove: function (map) {
2961 this.fire('remove');
2964 'viewreset': this.update,
2965 'zoomanim': this._animateZoom
2971 getLatLng: function () {
2972 return this._latlng;
2975 setLatLng: function (latlng) {
2976 this._latlng = L.latLng(latlng);
2980 this.fire('move', { latlng: this._latlng });
2983 setZIndexOffset: function (offset) {
2984 this.options.zIndexOffset = offset;
2988 setIcon: function (icon) {
2993 this.options.icon = icon;
3001 update: function () {
3002 if (!this._icon) { return; }
3004 var pos = this._map.latLngToLayerPoint(this._latlng).round();
3008 _initIcon: function () {
3009 var options = this.options,
3011 animation = (map.options.zoomAnimation && map.options.markerZoomAnimation),
3012 classToAdd = animation ? 'leaflet-zoom-animated' : 'leaflet-zoom-hide',
3013 needOpacityUpdate = false;
3016 this._icon = options.icon.createIcon();
3018 if (options.title) {
3019 this._icon.title = options.title;
3022 this._initInteraction();
3023 needOpacityUpdate = (this.options.opacity < 1);
3025 L.DomUtil.addClass(this._icon, classToAdd);
3027 if (options.riseOnHover) {
3029 .on(this._icon, 'mouseover', this._bringToFront, this)
3030 .on(this._icon, 'mouseout', this._resetZIndex, this);
3034 if (!this._shadow) {
3035 this._shadow = options.icon.createShadow();
3038 L.DomUtil.addClass(this._shadow, classToAdd);
3039 needOpacityUpdate = (this.options.opacity < 1);
3043 if (needOpacityUpdate) {
3044 this._updateOpacity();
3047 var panes = this._map._panes;
3049 panes.markerPane.appendChild(this._icon);
3052 panes.shadowPane.appendChild(this._shadow);
3056 _removeIcon: function () {
3057 var panes = this._map._panes;
3059 if (this.options.riseOnHover) {
3061 .off(this._icon, 'mouseover', this._bringToFront)
3062 .off(this._icon, 'mouseout', this._resetZIndex);
3065 panes.markerPane.removeChild(this._icon);
3068 panes.shadowPane.removeChild(this._shadow);
3071 this._icon = this._shadow = null;
3074 _setPos: function (pos) {
3075 L.DomUtil.setPosition(this._icon, pos);
3078 L.DomUtil.setPosition(this._shadow, pos);
3081 this._zIndex = pos.y + this.options.zIndexOffset;
3083 this._resetZIndex();
3086 _updateZIndex: function (offset) {
3087 this._icon.style.zIndex = this._zIndex + offset;
3090 _animateZoom: function (opt) {
3091 var pos = this._map._latLngToNewLayerPoint(this._latlng, opt.zoom, opt.center);
3096 _initInteraction: function () {
3097 if (!this.options.clickable) {
3101 var icon = this._icon,
3102 events = ['dblclick', 'mousedown', 'mouseover', 'mouseout'];
3104 L.DomUtil.addClass(icon, 'leaflet-clickable');
3105 L.DomEvent.on(icon, 'click', this._onMouseClick, this);
3107 for (var i = 0; i < events.length; i++) {
3108 L.DomEvent.on(icon, events[i], this._fireMouseEvent, this);
3111 if (L.Handler.MarkerDrag) {
3112 this.dragging = new L.Handler.MarkerDrag(this);
3114 if (this.options.draggable) {
3115 this.dragging.enable();
3120 _onMouseClick: function (e) {
3121 var wasDragged = this.dragging && this.dragging.moved();
3122 if (this.hasEventListeners(e.type) || wasDragged) {
3123 L.DomEvent.stopPropagation(e);
3125 if (wasDragged) { return; }
3126 if (this._map.dragging && this._map.dragging.moved()) { return; }
3132 _fireMouseEvent: function (e) {
3136 if (e.type !== 'mousedown') {
3137 L.DomEvent.stopPropagation(e);
3141 setOpacity: function (opacity) {
3142 this.options.opacity = opacity;
3144 this._updateOpacity();
3148 _updateOpacity: function () {
3149 L.DomUtil.setOpacity(this._icon, this.options.opacity);
3151 L.DomUtil.setOpacity(this._shadow, this.options.opacity);
3155 _bringToFront: function () {
3156 this._updateZIndex(this.options.riseOffset);
3159 _resetZIndex: function () {
3160 this._updateZIndex(0);
3164 L.marker = function (latlng, options) {
3165 return new L.Marker(latlng, options);
3169 L.DivIcon = L.Icon.extend({
3171 iconSize: new L.Point(12, 12), // also can be set through CSS
3174 popupAnchor: (Point)
3178 className: 'leaflet-div-icon'
3181 createIcon: function () {
3182 var div = document.createElement('div'),
3183 options = this.options;
3186 div.innerHTML = options.html;
3189 if (options.bgPos) {
3190 div.style.backgroundPosition =
3191 (-options.bgPos.x) + 'px ' + (-options.bgPos.y) + 'px';
3194 this._setIconStyles(div, 'icon');
3198 createShadow: function () {
3203 L.divIcon = function (options) {
3204 return new L.DivIcon(options);
3209 L.Map.mergeOptions({
3210 closePopupOnClick: true
3213 L.Popup = L.Class.extend({
3214 includes: L.Mixin.Events,
3222 offset: new L.Point(0, 6),
3223 autoPanPadding: new L.Point(5, 5),
3227 initialize: function (options, source) {
3228 L.setOptions(this, options);
3230 this._source = source;
3233 onAdd: function (map) {
3236 if (!this._container) {
3239 this._updateContent();
3241 var animFade = map.options.fadeAnimation;
3244 L.DomUtil.setOpacity(this._container, 0);
3246 map._panes.popupPane.appendChild(this._container);
3248 map.on('viewreset', this._updatePosition, this);
3250 if (L.Browser.any3d) {
3251 map.on('zoomanim', this._zoomAnimation, this);
3254 if (map.options.closePopupOnClick) {
3255 map.on('preclick', this._close, this);
3261 L.DomUtil.setOpacity(this._container, 1);
3265 addTo: function (map) {
3270 openOn: function (map) {
3271 map.openPopup(this);
3275 onRemove: function (map) {
3276 map._panes.popupPane.removeChild(this._container);
3278 L.Util.falseFn(this._container.offsetWidth); // force reflow
3281 viewreset: this._updatePosition,
3282 preclick: this._close,
3283 zoomanim: this._zoomAnimation
3286 if (map.options.fadeAnimation) {
3287 L.DomUtil.setOpacity(this._container, 0);
3293 setLatLng: function (latlng) {
3294 this._latlng = L.latLng(latlng);
3299 setContent: function (content) {
3300 this._content = content;
3305 _close: function () {
3306 var map = this._map;
3313 .fire('popupclose', {popup: this});
3317 _initLayout: function () {
3318 var prefix = 'leaflet-popup',
3319 containerClass = prefix + ' ' + this.options.className + ' leaflet-zoom-animated',
3320 container = this._container = L.DomUtil.create('div', containerClass),
3323 if (this.options.closeButton) {
3324 closeButton = this._closeButton =
3325 L.DomUtil.create('a', prefix + '-close-button', container);
3326 closeButton.href = '#close';
3327 closeButton.innerHTML = '×';
3329 L.DomEvent.on(closeButton, 'click', this._onCloseButtonClick, this);
3332 var wrapper = this._wrapper =
3333 L.DomUtil.create('div', prefix + '-content-wrapper', container);
3334 L.DomEvent.disableClickPropagation(wrapper);
3336 this._contentNode = L.DomUtil.create('div', prefix + '-content', wrapper);
3337 L.DomEvent.on(this._contentNode, 'mousewheel', L.DomEvent.stopPropagation);
3339 this._tipContainer = L.DomUtil.create('div', prefix + '-tip-container', container);
3340 this._tip = L.DomUtil.create('div', prefix + '-tip', this._tipContainer);
3343 _update: function () {
3344 if (!this._map) { return; }
3346 this._container.style.visibility = 'hidden';
3348 this._updateContent();
3349 this._updateLayout();
3350 this._updatePosition();
3352 this._container.style.visibility = '';
3357 _updateContent: function () {
3358 if (!this._content) { return; }
3360 if (typeof this._content === 'string') {
3361 this._contentNode.innerHTML = this._content;
3363 while (this._contentNode.hasChildNodes()) {
3364 this._contentNode.removeChild(this._contentNode.firstChild);
3366 this._contentNode.appendChild(this._content);
3368 this.fire('contentupdate');
3371 _updateLayout: function () {
3372 var container = this._contentNode,
3373 style = container.style;
3376 style.whiteSpace = 'nowrap';
3378 var width = container.offsetWidth;
3379 width = Math.min(width, this.options.maxWidth);
3380 width = Math.max(width, this.options.minWidth);
3382 style.width = (width + 1) + 'px';
3383 style.whiteSpace = '';
3387 var height = container.offsetHeight,
3388 maxHeight = this.options.maxHeight,
3389 scrolledClass = 'leaflet-popup-scrolled';
3391 if (maxHeight && height > maxHeight) {
3392 style.height = maxHeight + 'px';
3393 L.DomUtil.addClass(container, scrolledClass);
3395 L.DomUtil.removeClass(container, scrolledClass);
3398 this._containerWidth = this._container.offsetWidth;
3401 _updatePosition: function () {
3402 if (!this._map) { return; }
3404 var pos = this._map.latLngToLayerPoint(this._latlng),
3405 is3d = L.Browser.any3d,
3406 offset = this.options.offset;
3409 L.DomUtil.setPosition(this._container, pos);
3412 this._containerBottom = -offset.y - (is3d ? 0 : pos.y);
3413 this._containerLeft = -Math.round(this._containerWidth / 2) + offset.x + (is3d ? 0 : pos.x);
3415 //Bottom position the popup in case the height of the popup changes (images loading etc)
3416 this._container.style.bottom = this._containerBottom + 'px';
3417 this._container.style.left = this._containerLeft + 'px';
3420 _zoomAnimation: function (opt) {
3421 var pos = this._map._latLngToNewLayerPoint(this._latlng, opt.zoom, opt.center);
3423 L.DomUtil.setPosition(this._container, pos);
3426 _adjustPan: function () {
3427 if (!this.options.autoPan) { return; }
3429 var map = this._map,
3430 containerHeight = this._container.offsetHeight,
3431 containerWidth = this._containerWidth,
3433 layerPos = new L.Point(this._containerLeft, -containerHeight - this._containerBottom);
3435 if (L.Browser.any3d) {
3436 layerPos._add(L.DomUtil.getPosition(this._container));
3439 var containerPos = map.layerPointToContainerPoint(layerPos),
3440 padding = this.options.autoPanPadding,
3441 size = map.getSize(),
3445 if (containerPos.x < 0) {
3446 dx = containerPos.x - padding.x;
3448 if (containerPos.x + containerWidth > size.x) {
3449 dx = containerPos.x + containerWidth - size.x + padding.x;
3451 if (containerPos.y < 0) {
3452 dy = containerPos.y - padding.y;
3454 if (containerPos.y + containerHeight > size.y) {
3455 dy = containerPos.y + containerHeight - size.y + padding.y;
3459 map.panBy(new L.Point(dx, dy));
3463 _onCloseButtonClick: function (e) {
3469 L.popup = function (options, source) {
3470 return new L.Popup(options, source);
3475 * Popup extension to L.Marker, adding openPopup & bindPopup methods.
3479 openPopup: function () {
3480 if (this._popup && this._map) {
3481 this._popup.setLatLng(this._latlng);
3482 this._map.openPopup(this._popup);
3488 closePopup: function () {
3490 this._popup._close();
3495 bindPopup: function (content, options) {
3496 var anchor = L.point(this.options.icon.options.popupAnchor) || new L.Point(0, 0);
3498 anchor = anchor.add(L.Popup.prototype.options.offset);
3500 if (options && options.offset) {
3501 anchor = anchor.add(options.offset);
3504 options = L.extend({offset: anchor}, options);
3508 .on('click', this.openPopup, this)
3509 .on('remove', this.closePopup, this)
3510 .on('move', this._movePopup, this);
3513 this._popup = new L.Popup(options, this)
3514 .setContent(content);
3519 unbindPopup: function () {
3523 .off('click', this.openPopup)
3524 .off('remove', this.closePopup)
3525 .off('move', this._movePopup);
3530 _movePopup: function (e) {
3531 this._popup.setLatLng(e.latlng);
3538 openPopup: function (popup) {
3541 this._popup = popup;
3545 .fire('popupopen', {popup: this._popup});
3548 closePopup: function () {
3550 this._popup._close();
3558 * L.LayerGroup is a class to combine several layers so you can manipulate the group (e.g. add/remove it) as one layer.
3561 L.LayerGroup = L.Class.extend({
3562 initialize: function (layers) {
3568 for (i = 0, len = layers.length; i < len; i++) {
3569 this.addLayer(layers[i]);
3574 addLayer: function (layer) {
3575 var id = L.stamp(layer);
3577 this._layers[id] = layer;
3580 this._map.addLayer(layer);
3586 removeLayer: function (layer) {
3587 var id = L.stamp(layer);
3589 delete this._layers[id];
3592 this._map.removeLayer(layer);
3598 clearLayers: function () {
3599 this.eachLayer(this.removeLayer, this);
3603 invoke: function (methodName) {
3604 var args = Array.prototype.slice.call(arguments, 1),
3607 for (i in this._layers) {
3608 if (this._layers.hasOwnProperty(i)) {
3609 layer = this._layers[i];
3611 if (layer[methodName]) {
3612 layer[methodName].apply(layer, args);
3620 onAdd: function (map) {
3622 this.eachLayer(map.addLayer, map);
3625 onRemove: function (map) {
3626 this.eachLayer(map.removeLayer, map);
3630 addTo: function (map) {
3635 eachLayer: function (method, context) {
3636 for (var i in this._layers) {
3637 if (this._layers.hasOwnProperty(i)) {
3638 method.call(context, this._layers[i]);
3644 L.layerGroup = function (layers) {
3645 return new L.LayerGroup(layers);
3650 * L.FeatureGroup extends L.LayerGroup by introducing mouse events and bindPopup method shared between a group of layers.
3653 L.FeatureGroup = L.LayerGroup.extend({
3654 includes: L.Mixin.Events,
3657 EVENTS: 'click dblclick mouseover mouseout mousemove contextmenu'
3660 addLayer: function (layer) {
3661 if (this._layers[L.stamp(layer)]) {
3665 layer.on(L.FeatureGroup.EVENTS, this._propagateEvent, this);
3667 L.LayerGroup.prototype.addLayer.call(this, layer);
3669 if (this._popupContent && layer.bindPopup) {
3670 layer.bindPopup(this._popupContent);
3673 return this.fire('layeradd', {layer: layer});
3676 removeLayer: function (layer) {
3677 layer.off(L.FeatureGroup.EVENTS, this._propagateEvent, this);
3679 L.LayerGroup.prototype.removeLayer.call(this, layer);
3682 if (this._popupContent) {
3683 this.invoke('unbindPopup');
3686 return this.fire('layerremove', {layer: layer});
3689 bindPopup: function (content) {
3690 this._popupContent = content;
3691 return this.invoke('bindPopup', content);
3694 setStyle: function (style) {
3695 return this.invoke('setStyle', style);
3698 bringToFront: function () {
3699 return this.invoke('bringToFront');
3702 bringToBack: function () {
3703 return this.invoke('bringToBack');
3706 getBounds: function () {
3707 var bounds = new L.LatLngBounds();
3709 this.eachLayer(function (layer) {
3710 bounds.extend(layer instanceof L.Marker ? layer.getLatLng() : layer.getBounds());
3716 _propagateEvent: function (e) {
3720 this.fire(e.type, e);
3724 L.featureGroup = function (layers) {
3725 return new L.FeatureGroup(layers);
3730 * L.Path is a base class for rendering vector paths on a map. Inherited by Polyline, Circle, etc.
3733 L.Path = L.Class.extend({
3734 includes: [L.Mixin.Events],
3737 // how much to extend the clip area around the map view
3738 // (relative to its size, e.g. 0.5 is half the screen in each direction)
3739 // set it so that SVG element doesn't exceed 1280px (vectors flicker on dragend if it is)
3740 CLIP_PADDING: L.Browser.mobile ?
3741 Math.max(0, Math.min(0.5,
3742 (1280 / Math.max(window.innerWidth, window.innerHeight) - 1) / 2)) : 0.5
3753 fillColor: null, //same as color by default
3759 initialize: function (options) {
3760 L.setOptions(this, options);
3763 onAdd: function (map) {
3766 if (!this._container) {
3767 this._initElements();
3771 this.projectLatlngs();
3774 if (this._container) {
3775 this._map._pathRoot.appendChild(this._container);
3779 'viewreset': this.projectLatlngs,
3780 'moveend': this._updatePath
3784 addTo: function (map) {
3789 onRemove: function (map) {
3790 map._pathRoot.removeChild(this._container);
3794 if (L.Browser.vml) {
3795 this._container = null;
3796 this._stroke = null;
3800 this.fire('remove');
3803 'viewreset': this.projectLatlngs,
3804 'moveend': this._updatePath
3808 projectLatlngs: function () {
3809 // do all projection stuff here
3812 setStyle: function (style) {
3813 L.setOptions(this, style);
3815 if (this._container) {
3816 this._updateStyle();
3822 redraw: function () {
3824 this.projectLatlngs();
3832 _updatePathViewport: function () {
3833 var p = L.Path.CLIP_PADDING,
3834 size = this.getSize(),
3835 panePos = L.DomUtil.getPosition(this._mapPane),
3836 min = panePos.multiplyBy(-1)._subtract(size.multiplyBy(p)._round()),
3837 max = min.add(size.multiplyBy(1 + p * 2)._round());
3839 this._pathViewport = new L.Bounds(min, max);
3844 L.Path.SVG_NS = 'http://www.w3.org/2000/svg';
3846 L.Browser.svg = !!(document.createElementNS && document.createElementNS(L.Path.SVG_NS, 'svg').createSVGRect);
3848 L.Path = L.Path.extend({
3853 bringToFront: function () {
3854 var root = this._map._pathRoot,
3855 path = this._container;
3857 if (path && root.lastChild !== path) {
3858 root.appendChild(path);
3863 bringToBack: function () {
3864 var root = this._map._pathRoot,
3865 path = this._container,
3866 first = root.firstChild;
3868 if (path && first !== path) {
3869 root.insertBefore(path, first);
3874 getPathString: function () {
3875 // form path string here
3878 _createElement: function (name) {
3879 return document.createElementNS(L.Path.SVG_NS, name);
3882 _initElements: function () {
3883 this._map._initPathRoot();
3888 _initPath: function () {
3889 this._container = this._createElement('g');
3891 this._path = this._createElement('path');
3892 this._container.appendChild(this._path);
3895 _initStyle: function () {
3896 if (this.options.stroke) {
3897 this._path.setAttribute('stroke-linejoin', 'round');
3898 this._path.setAttribute('stroke-linecap', 'round');
3900 if (this.options.fill) {
3901 this._path.setAttribute('fill-rule', 'evenodd');
3903 this._updateStyle();
3906 _updateStyle: function () {
3907 if (this.options.stroke) {
3908 this._path.setAttribute('stroke', this.options.color);
3909 this._path.setAttribute('stroke-opacity', this.options.opacity);
3910 this._path.setAttribute('stroke-width', this.options.weight);
3911 if (this.options.dashArray) {
3912 this._path.setAttribute('stroke-dasharray', this.options.dashArray);
3914 this._path.removeAttribute('stroke-dasharray');
3917 this._path.setAttribute('stroke', 'none');
3919 if (this.options.fill) {
3920 this._path.setAttribute('fill', this.options.fillColor || this.options.color);
3921 this._path.setAttribute('fill-opacity', this.options.fillOpacity);
3923 this._path.setAttribute('fill', 'none');
3927 _updatePath: function () {
3928 var str = this.getPathString();
3930 // fix webkit empty string parsing bug
3933 this._path.setAttribute('d', str);
3936 // TODO remove duplication with L.Map
3937 _initEvents: function () {
3938 if (this.options.clickable) {
3939 if (L.Browser.svg || !L.Browser.vml) {
3940 this._path.setAttribute('class', 'leaflet-clickable');
3943 L.DomEvent.on(this._container, 'click', this._onMouseClick, this);
3945 var events = ['dblclick', 'mousedown', 'mouseover',
3946 'mouseout', 'mousemove', 'contextmenu'];
3947 for (var i = 0; i < events.length; i++) {
3948 L.DomEvent.on(this._container, events[i], this._fireMouseEvent, this);
3953 _onMouseClick: function (e) {
3954 if (this._map.dragging && this._map.dragging.moved()) { return; }
3956 this._fireMouseEvent(e);
3959 _fireMouseEvent: function (e) {
3960 if (!this.hasEventListeners(e.type)) { return; }
3962 var map = this._map,
3963 containerPoint = map.mouseEventToContainerPoint(e),
3964 layerPoint = map.containerPointToLayerPoint(containerPoint),
3965 latlng = map.layerPointToLatLng(layerPoint);