Draw.Circle.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * @class L.Draw.Circle
  3. * @aka Draw.Circle
  4. * @inherits L.Draw.SimpleShape
  5. */
  6. L.Draw.Circle = L.Draw.SimpleShape.extend({
  7. statics: {
  8. TYPE: 'circle'
  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. clickable: true
  20. },
  21. showRadius: true,
  22. metric: true, // Whether to use the metric measurement system or imperial
  23. feet: true, // When not metric, use feet instead of yards for display
  24. nautic: false // When not metric, not feet use nautic mile for display
  25. },
  26. // @method initialize(): void
  27. initialize: function (map, options) {
  28. // Save the type so super can fire, need to do this as cannot do this.TYPE :(
  29. this.type = L.Draw.Circle.TYPE;
  30. this._initialLabelText = L.drawLocal.draw.handlers.circle.tooltip.start;
  31. L.Draw.SimpleShape.prototype.initialize.call(this, map, options);
  32. },
  33. _drawShape: function (latlng) {
  34. if (!this._shape) {
  35. this._shape = new L.Circle(this._startLatLng, this._startLatLng.distanceTo(latlng), this.options.shapeOptions);
  36. this._map.addLayer(this._shape);
  37. } else {
  38. this._shape.setRadius(this._startLatLng.distanceTo(latlng));
  39. }
  40. },
  41. _fireCreatedEvent: function () {
  42. var circle = new L.Circle(this._startLatLng, this._shape.getRadius(), this.options.shapeOptions);
  43. L.Draw.SimpleShape.prototype._fireCreatedEvent.call(this, circle);
  44. },
  45. _onMouseMove: function (e) {
  46. var latlng = e.latlng,
  47. showRadius = this.options.showRadius,
  48. useMetric = this.options.metric,
  49. radius;
  50. this._tooltip.updatePosition(latlng);
  51. if (this._isDrawing) {
  52. this._drawShape(latlng);
  53. // Get the new radius (rounded to 1 dp)
  54. radius = this._shape.getRadius().toFixed(1);
  55. var subtext = '';
  56. if (showRadius) {
  57. subtext = L.drawLocal.draw.handlers.circle.radius + ': ' +
  58. L.GeometryUtil.readableDistance(radius, useMetric, this.options.feet, this.options.nautic);
  59. }
  60. this._tooltip.updateContent({
  61. text: this._endLabelText,
  62. subtext: subtext
  63. });
  64. }
  65. }
  66. });