Draw.Rectangle.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * @class L.Draw.Rectangle
  3. * @aka Draw.Rectangle
  4. * @inherits L.Draw.SimpleShape
  5. */
  6. L.Draw.Rectangle = L.Draw.SimpleShape.extend({
  7. statics: {
  8. TYPE: 'rectangle'
  9. },
  10. options: {
  11. shapeOptions: {
  12. stroke: true,
  13. color: '#3388ff',
  14. weight: 4,
  15. opacity: 0.5,
  16. fill: true,
  17. fillColor: null, //same as color by default
  18. fillOpacity: 0.2,
  19. showArea: true,
  20. clickable: true
  21. },
  22. metric: true // Whether to use the metric measurement system or imperial
  23. },
  24. // @method initialize(): void
  25. initialize: function (map, options) {
  26. // Save the type so super can fire, need to do this as cannot do this.TYPE :(
  27. this.type = L.Draw.Rectangle.TYPE;
  28. this._initialLabelText = L.drawLocal.draw.handlers.rectangle.tooltip.start;
  29. L.Draw.SimpleShape.prototype.initialize.call(this, map, options);
  30. },
  31. _drawShape: function (latlng) {
  32. if (!this._shape) {
  33. this._shape = new L.Rectangle(new L.LatLngBounds(this._startLatLng, latlng), this.options.shapeOptions);
  34. this._map.addLayer(this._shape);
  35. } else {
  36. this._shape.setBounds(new L.LatLngBounds(this._startLatLng, latlng));
  37. }
  38. },
  39. _fireCreatedEvent: function () {
  40. var rectangle = new L.Rectangle(this._shape.getBounds(), this.options.shapeOptions);
  41. L.Draw.SimpleShape.prototype._fireCreatedEvent.call(this, rectangle);
  42. },
  43. _getTooltipText: function () {
  44. var tooltipText = L.Draw.SimpleShape.prototype._getTooltipText.call(this),
  45. shape = this._shape,
  46. showArea = this.options.showArea,
  47. latLngs, area, subtext;
  48. if (shape) {
  49. latLngs = this._shape._defaultShape ? this._shape._defaultShape() : this._shape.getLatLngs();
  50. area = L.GeometryUtil.geodesicArea(latLngs);
  51. subtext = showArea ? L.GeometryUtil.readableArea(area, this.options.metric) : ''
  52. }
  53. return {
  54. text: tooltipText.text,
  55. subtext: subtext
  56. };
  57. }
  58. });