diff --git a/debug/map/map.html b/debug/map/map.html index 0b3cc1b0c..ae847c5cd 100644 --- a/debug/map/map.html +++ b/debug/map/map.html @@ -19,14 +19,15 @@ diff --git a/src/geo/LatLng.js b/src/geo/LatLng.js index 03897ac6a..4f6b3339e 100644 --- a/src/geo/LatLng.js +++ b/src/geo/LatLng.js @@ -15,7 +15,6 @@ L.LatLng = function (/*Number*/ rawLat, /*Number*/ rawLng, /*Boolean*/ noWrap) { lng = (lng + 180) % 360 + ((lng < -180 || lng === 180) ? 180 : -180); // wrap longtitude into -180..180 } - //TODO change to lat() & lng() this.lat = lat; this.lng = lng; }; @@ -58,3 +57,13 @@ L.LatLng.prototype = { return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); } }; + +L.latLng = function (a, b, c) { + if (a instanceof L.LatLng) { + return a; + } + if (a instanceof Array) { + return new L.LatLng(a[0], a[1]); + } + return new L.LatLng(a, b, c); +}; diff --git a/src/geo/LatLngBounds.js b/src/geo/LatLngBounds.js index 504258f46..75f1fb3b7 100644 --- a/src/geo/LatLngBounds.js +++ b/src/geo/LatLngBounds.js @@ -4,10 +4,10 @@ L.LatLngBounds = L.Class.extend({ initialize: function (southWest, northEast) { // (LatLng, LatLng) or (LatLng[]) - if (!southWest) { - return; - } - var latlngs = (southWest instanceof Array ? southWest : [southWest, northEast]); + if (!southWest) { return; } + + var latlngs = northEast ? [southWest, northEast] : southWest; + for (var i = 0, len = latlngs.length; i < len; i++) { this.extend(latlngs[i]); } @@ -15,6 +15,12 @@ L.LatLngBounds = L.Class.extend({ // extend the bounds to contain the given point or bounds extend: function (/*LatLng or LatLngBounds*/ obj) { + if (typeof obj[0] === 'number' || obj instanceof L.LatLng) { + obj = L.latLng(obj) + } else { + obj = L.latLngBounds(obj); + } + if (obj instanceof L.LatLng) { if (!this._southWest && !this._northEast) { this._southWest = new L.LatLng(obj.lat, obj.lng, true); @@ -67,6 +73,12 @@ L.LatLngBounds = L.Class.extend({ }, contains: function (/*LatLngBounds or LatLng*/ obj) /*-> Boolean*/ { + if (typeof obj[0] === 'number' || obj instanceof L.LatLng) { + obj = L.latLng(obj) + } else { + obj = L.latLngBounds(obj); + } + var sw = this._southWest, ne = this._northEast, sw2, ne2; @@ -83,6 +95,8 @@ L.LatLngBounds = L.Class.extend({ }, intersects: function (/*LatLngBounds*/ bounds) { + bounds = L.latLngBounds(bounds); + var sw = this._southWest, ne = this._northEast, sw2 = bounds.getSouthWest(), @@ -107,3 +121,10 @@ L.LatLngBounds = L.Class.extend({ }); //TODO International date line? + +L.latLngBounds = function (a, b) { + if (a instanceof L.Bounds) { + return a; + } + return new L.LatLngBounds(a, b); +}; diff --git a/src/geometry/Bounds.js b/src/geometry/Bounds.js index 5f9fccaf2..e616fb17e 100644 --- a/src/geometry/Bounds.js +++ b/src/geometry/Bounds.js @@ -3,12 +3,12 @@ */ L.Bounds = L.Class.extend({ - - initialize: function (min, max) { //(Point, Point) or Point[] - if (!min) { - return; - } - var points = (min instanceof Array ? min : [min, max]); + + initialize: function (a, b) { //(Point, Point) or Point[] + if (!a) { return; } + + var points = b ? [a, b] : a; + for (var i = 0, len = points.length; i < len; i++) { this.extend(points[i]); } @@ -16,9 +16,10 @@ L.Bounds = L.Class.extend({ // extend the bounds to contain the given point extend: function (/*Point*/ point) { + point = L.point(point); if (!this.min && !this.max) { - this.min = new L.Point(point.x, point.y); - this.max = new L.Point(point.x, point.y); + this.min = point.clone(); + this.max = point.clone(); } else { this.min.x = Math.min(point.x, this.min.x); this.max.x = Math.max(point.x, this.max.x); @@ -44,6 +45,12 @@ L.Bounds = L.Class.extend({ contains: function (/*Bounds or Point*/ obj)/*->Boolean*/ { var min, max; + if (typeof obj[0] === 'number' || obj instanceof L.Point) { + obj = L.point(obj) + } else { + obj = L.bounds(obj); + } + if (obj instanceof L.Bounds) { min = obj.min; max = obj.max; @@ -58,6 +65,8 @@ L.Bounds = L.Class.extend({ }, intersects: function (/*Bounds*/ bounds) { + bounds = L.bounds(bounds); + var min = this.min, max = this.max, min2 = bounds.min, @@ -70,3 +79,10 @@ L.Bounds = L.Class.extend({ } }); + +L.bounds = function (a, b) { + if (a instanceof L.Bounds) { + return a; + } + return new L.Bounds(a, b); +}; diff --git a/src/geometry/Point.js b/src/geometry/Point.js index db414572a..fbe0248c4 100644 --- a/src/geometry/Point.js +++ b/src/geometry/Point.js @@ -64,3 +64,13 @@ L.Point.prototype = { L.Util.formatNum(this.y) + ')'; } }; + +L.point = function (x, y, round) { + if (x instanceof L.Point) { + return x; + } + if (x instanceof Array) { + return new L.Point(x[0], x[1]); + } + return new L.Point(x, y, round); +}; diff --git a/src/layer/ImageOverlay.js b/src/layer/ImageOverlay.js index 61b04cbe9..2b1f837ca 100644 --- a/src/layer/ImageOverlay.js +++ b/src/layer/ImageOverlay.js @@ -7,7 +7,7 @@ L.ImageOverlay = L.Class.extend({ initialize: function (url, bounds, options) { // (String, LatLngBounds) this._url = url; - this._bounds = bounds; + this._bounds = L.latLngBounds(bounds); L.Util.setOptions(this, options); }, @@ -32,7 +32,7 @@ L.ImageOverlay = L.Class.extend({ onRemove: function (map) { map.getPanes().overlayPane.removeChild(this._image); - + map.off('viewreset', this._reset, this); if (map.options.zoomAnimation) { diff --git a/src/layer/Popup.js b/src/layer/Popup.js index b47a49ccb..84a001549 100644 --- a/src/layer/Popup.js +++ b/src/layer/Popup.js @@ -66,7 +66,7 @@ L.Popup = L.Class.extend({ }, setLatLng: function (latlng) { - this._latlng = latlng; + this._latlng = L.latLng(latlng); this._update(); return this; }, @@ -185,7 +185,7 @@ L.Popup = L.Class.extend({ this._container.style.bottom = this._containerBottom + 'px'; this._container.style.left = this._containerLeft + 'px'; }, - + _zoomAnimation: function (opt) { var pos = this._map._latLngToNewLayerPoint(this._latlng, opt.zoom, opt.center)._round(); diff --git a/src/layer/marker/Marker.js b/src/layer/marker/Marker.js index 3897d3c07..ca27c927f 100644 --- a/src/layer/marker/Marker.js +++ b/src/layer/marker/Marker.js @@ -17,7 +17,7 @@ L.Marker = L.Class.extend({ initialize: function (latlng, options) { L.Util.setOptions(this, options); - this._latlng = latlng; + this._latlng = L.latLng(latlng); }, onAdd: function (map) { @@ -33,6 +33,11 @@ L.Marker = L.Class.extend({ this.update(); }, + addTo: function (map) { + map.addLayer(this); + return this; + }, + onRemove: function (map) { this._removeIcon(); @@ -54,7 +59,7 @@ L.Marker = L.Class.extend({ }, setLatLng: function (latlng) { - this._latlng = latlng; + this._latlng = L.latLng(latlng); this.update(); @@ -195,3 +200,7 @@ L.Marker = L.Class.extend({ L.DomUtil.setOpacity(this._icon, this.options.opacity); } }); + +L.marker = function (latlng, options) { + return new L.Marker(latlng, options); +}; diff --git a/src/layer/tile/TileLayer.js b/src/layer/tile/TileLayer.js index ae84e9d39..5eb484001 100644 --- a/src/layer/tile/TileLayer.js +++ b/src/layer/tile/TileLayer.js @@ -74,6 +74,11 @@ L.TileLayer = L.Class.extend({ this._update(); }, + addTo: function (map) { + map.addLayer(this); + return this; + }, + onRemove: function (map) { map._panes.tilePane.removeChild(this._container); @@ -410,3 +415,7 @@ L.TileLayer = L.Class.extend({ layer._tileLoaded(); } }); + +L.tileLayer = function (url, options) { + return new L.TileLayer(url, options); +}; diff --git a/src/layer/vector/Circle.js b/src/layer/vector/Circle.js index 91a8d32fd..2a84281cc 100644 --- a/src/layer/vector/Circle.js +++ b/src/layer/vector/Circle.js @@ -6,7 +6,7 @@ L.Circle = L.Path.extend({ initialize: function (latlng, radius, options) { L.Path.prototype.initialize.call(this, options); - this._latlng = latlng; + this._latlng = L.latLng(latlng); this._mRadius = radius; }, @@ -15,7 +15,7 @@ L.Circle = L.Path.extend({ }, setLatLng: function (latlng) { - this._latlng = latlng; + this._latlng = L.latLng(latlng); return this.redraw(); }, @@ -44,7 +44,7 @@ L.Circle = L.Path.extend({ return new L.LatLngBounds(sw, ne); }, - + getLatLng: function () { return this._latlng; }, @@ -67,7 +67,7 @@ L.Circle = L.Path.extend({ return "AL " + p.x + "," + p.y + " " + r + "," + r + " 0," + (65535 * 360); } }, - + getRadius: function () { return this._mRadius; }, @@ -91,3 +91,7 @@ L.Circle = L.Path.extend({ p.x + r < vp.min.x || p.y + r < vp.min.y; } }); + +L.circle = function (latlng, radius, options) { + return new L.Circle(latlng, radius, options); +}; diff --git a/src/layer/vector/CircleMarker.js b/src/layer/vector/CircleMarker.js index 8163df14d..39d323bf6 100644 --- a/src/layer/vector/CircleMarker.js +++ b/src/layer/vector/CircleMarker.js @@ -22,3 +22,7 @@ L.CircleMarker = L.Circle.extend({ return this.redraw(); } }); + +L.circleMarker = function (latlng, options) { + return new L.CircleMarker(latlng, options); +}; diff --git a/src/layer/vector/Path.js b/src/layer/vector/Path.js index 0cfa5ee04..20ca4a23d 100644 --- a/src/layer/vector/Path.js +++ b/src/layer/vector/Path.js @@ -42,6 +42,11 @@ L.Path = L.Class.extend({ }, this); }, + addTo: function (map) { + map.addLayer(this); + return this; + }, + onRemove: function (map) { this._map = null; diff --git a/src/layer/vector/Polygon.js b/src/layer/vector/Polygon.js index c71fc3610..15e5e7ae8 100644 --- a/src/layer/vector/Polygon.js +++ b/src/layer/vector/Polygon.js @@ -10,10 +10,10 @@ L.Polygon = L.Polyline.extend({ initialize: function (latlngs, options) { L.Polyline.prototype.initialize.call(this, latlngs, options); - if (latlngs && (latlngs[0] instanceof Array)) { + /*if (latlngs && (latlngs[0] instanceof Array)) { this._latlngs = latlngs[0]; this._holes = latlngs.slice(1); - } + }*/ }, projectLatlngs: function () { @@ -62,3 +62,7 @@ L.Polygon = L.Polyline.extend({ return str + (L.Browser.svg ? 'z' : 'x'); } }); + +L.polygon = function (latlngs, options) { + return new L.Polygon(latlngs, options); +}; diff --git a/src/layer/vector/Polyline.js b/src/layer/vector/Polyline.js index c1bdbb7b9..1694c492b 100644 --- a/src/layer/vector/Polyline.js +++ b/src/layer/vector/Polyline.js @@ -169,3 +169,7 @@ L.Polyline = L.Path.extend({ L.Path.prototype._updatePath.call(this); } }); + +L.polyline = function (latlngs, options) { + return new L.Polyline(latlngs, options); +}; diff --git a/src/layer/vector/Rectangle.js b/src/layer/vector/Rectangle.js index 2ea985b91..85a30f64c 100644 --- a/src/layer/vector/Rectangle.js +++ b/src/layer/vector/Rectangle.js @@ -9,15 +9,19 @@ L.Rectangle = L.Polygon.extend({ setBounds: function (latLngBounds) { this.setLatLngs(this._boundsToLatLngs(latLngBounds)); - }, - - _boundsToLatLngs: function (latLngBounds) { - return [ - latLngBounds.getSouthWest(), - latLngBounds.getNorthWest(), - latLngBounds.getNorthEast(), - latLngBounds.getSouthEast(), - latLngBounds.getSouthWest() - ]; + }, + + _boundsToLatLngs: function (latLngBounds) { + return [ + latLngBounds.getSouthWest(), + latLngBounds.getNorthWest(), + latLngBounds.getNorthEast(), + latLngBounds.getSouthEast(), + latLngBounds.getSouthWest() + ]; } }); + +L.rectangle = function (latLngBounds, options) { + return new L.Rectangle(latLngBounds, options); +}; diff --git a/src/map/Map.js b/src/map/Map.js index ac84883a1..cf36fe8fd 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -33,7 +33,7 @@ L.Map = L.Class.extend({ } if (options.center && options.zoom !== undefined) { - this.setView(options.center, options.zoom, true); + this.setView(L.latLng(options.center), options.zoom, true); } this._initLayers(options.layers); @@ -44,7 +44,7 @@ L.Map = L.Class.extend({ // replaced by animation-powered implementation in Map.PanAnimation.js setView: function (center, zoom) { - this._resetView(center, this._limitZoom(zoom)); + this._resetView(L.latLng(center), this._limitZoom(zoom)); return this; }, @@ -246,7 +246,7 @@ L.Map = L.Class.extend({ var bounds = this.getPixelBounds(), sw = this.unproject(bounds.getBottomLeft()), ne = this.unproject(bounds.getTopRight()); - + return new L.LatLngBounds(sw, ne); }, @@ -323,12 +323,12 @@ L.Map = L.Class.extend({ getPanes: function () { return this._panes; }, - + getContainer: function () { return this._container; }, - + // TODO replace with universal implementation after refactoring projections getZoomScale: function (toZoom) { @@ -345,7 +345,7 @@ L.Map = L.Class.extend({ project: function (latlng, zoom) { // (LatLng[, Number]) -> Point zoom = zoom === undefined ? this._zoom : zoom; - return this.options.crs.latLngToPoint(latlng, zoom); + return this.options.crs.latLngToPoint(L.latLng(latlng), zoom); }, unproject: function (point, zoom) { // (Point[, Number]) -> LatLng @@ -358,7 +358,7 @@ L.Map = L.Class.extend({ }, latLngToLayerPoint: function (latlng) { // (LatLng) - return this.project(latlng)._round()._subtract(this._initialTopLeftPoint); + return this.project(L.latLng(latlng))._round()._subtract(this._initialTopLeftPoint); }, containerPointToLayerPoint: function (point) { // (Point) @@ -374,7 +374,7 @@ L.Map = L.Class.extend({ }, latLngToContainerPoint: function (latlng) { - return this.layerPointToContainerPoint(this.latLngToLayerPoint(latlng)); + return this.layerPointToContainerPoint(this.latLngToLayerPoint(L.latLng(latlng))); }, mouseEventToContainerPoint: function (e) { // (MouseEvent) @@ -552,7 +552,7 @@ L.Map = L.Class.extend({ _onMouseClick: function (e) { if (!this._loaded || (this.dragging && this.dragging.moved())) { return; } - + this.fire('preclick'); this._fireMouseEvent(e); }, @@ -642,4 +642,8 @@ L.Map.addInitHook = function (fn) { }; this.prototype._initializers.push(init); -}; \ No newline at end of file +}; + +L.map = function (id, options) { + return new L.Map(id, options); +};