diff --git a/build/deps.js b/build/deps.js index c7bf692e..e335cf45 100644 --- a/build/deps.js +++ b/build/deps.js @@ -158,7 +158,8 @@ var deps = { 'geometry/LineUtil.js', 'layer/vector2/Polyline.js', 'geometry/PolyUtil.js', - 'layer/vector2/Polygon.js' + 'layer/vector2/Polygon.js', + 'layer/vector2/Rectangle.js' ], desc: 'New vector layers implementation.' }, diff --git a/src/layer/vector2/Rectangle.js b/src/layer/vector2/Rectangle.js new file mode 100644 index 00000000..0af0a0a9 --- /dev/null +++ b/src/layer/vector2/Rectangle.js @@ -0,0 +1,27 @@ +/* + * L.Rectangle extends Polygon and creates a rectangle when passed a LatLngBounds object. + */ + +L.Rectangle = L.Polygon.extend({ + initialize: function (latLngBounds, options) { + L.Polygon.prototype.initialize.call(this, this._boundsToLatLngs(latLngBounds), options); + }, + + setBounds: function (latLngBounds) { + this.setLatLngs(this._boundsToLatLngs(latLngBounds)); + }, + + _boundsToLatLngs: function (latLngBounds) { + latLngBounds = L.latLngBounds(latLngBounds); + return [ + latLngBounds.getSouthWest(), + latLngBounds.getNorthWest(), + latLngBounds.getNorthEast(), + latLngBounds.getSouthEast() + ]; + } +}); + +L.rectangle = function (latLngBounds, options) { + return new L.Rectangle(latLngBounds, options); +};