leaflet
Version:
JavaScript library for mobile-friendly interactive maps
51 lines (45 loc) • 1.32 kB
JavaScript
import {Polygon} from './Polygon.js';
import {LatLngBounds} from '../../geo/LatLngBounds.js';
/*
* Rectangle extends Polygon and creates a rectangle when passed a LatLngBounds object.
*/
/*
* @class Rectangle
* @inherits Polygon
*
* A class for drawing rectangle overlays on a map. Extends `Polygon`.
*
* @example
*
* ```js
* // define rectangle geographical bounds
* const bounds = [[54.559322, -5.767822], [56.1210604, -3.021240]];
*
* // create an orange rectangle
* new Rectangle(bounds, {color: "#ff7800", weight: 1}).addTo(map);
*
* // zoom the map to the rectangle bounds
* map.fitBounds(bounds);
* ```
*
*/
// @constructor Rectangle(latLngBounds: LatLngBounds, options?: Polyline options)
export class Rectangle extends Polygon {
initialize(latLngBounds, options) {
Polygon.prototype.initialize.call(this, this._boundsToLatLngs(latLngBounds), options);
}
// @method setBounds(latLngBounds: LatLngBounds): this
// Redraws the rectangle with the passed bounds.
setBounds(latLngBounds) {
return this.setLatLngs(this._boundsToLatLngs(latLngBounds));
}
_boundsToLatLngs(latLngBounds) {
latLngBounds = new LatLngBounds(latLngBounds);
return [
latLngBounds.getSouthWest(),
latLngBounds.getNorthWest(),
latLngBounds.getNorthEast(),
latLngBounds.getSouthEast()
];
}
}