Edit.Circle.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. L.Edit = L.Edit || {};
  2. /**
  3. * @class L.Edit.Circle
  4. * @aka Edit.Circle
  5. * @inherits L.Edit.SimpleShape
  6. */
  7. L.Edit.Circle = L.Edit.SimpleShape.extend({
  8. _createMoveMarker: function () {
  9. var center = this._shape.getLatLng();
  10. this._moveMarker = this._createMarker(center, this.options.moveIcon);
  11. },
  12. _createResizeMarker: function () {
  13. var center = this._shape.getLatLng(),
  14. resizemarkerPoint = this._getResizeMarkerPoint(center);
  15. this._resizeMarkers = [];
  16. this._resizeMarkers.push(this._createMarker(resizemarkerPoint, this.options.resizeIcon));
  17. },
  18. _getResizeMarkerPoint: function (latlng) {
  19. // From L.shape.getBounds()
  20. var delta = this._shape._radius * Math.cos(Math.PI / 4),
  21. point = this._map.project(latlng);
  22. return this._map.unproject([point.x + delta, point.y - delta]);
  23. },
  24. _move: function (latlng) {
  25. var resizemarkerPoint = this._getResizeMarkerPoint(latlng);
  26. // Move the resize marker
  27. this._resizeMarkers[0].setLatLng(resizemarkerPoint);
  28. // Move the circle
  29. this._shape.setLatLng(latlng);
  30. this._map.fire(L.Draw.Event.EDITMOVE, { layer: this._shape });
  31. },
  32. _resize: function (latlng) {
  33. var moveLatLng = this._moveMarker.getLatLng(),
  34. radius = moveLatLng.distanceTo(latlng);
  35. this._shape.setRadius(radius);
  36. this._map.fire(L.Draw.Event.EDITRESIZE, { layer: this._shape });
  37. }
  38. });
  39. L.Circle.addInitHook(function () {
  40. if (L.Edit.Circle) {
  41. this.editing = new L.Edit.Circle(this);
  42. if (this.options.editable) {
  43. this.editing.enable();
  44. }
  45. }
  46. this.on('add', function () {
  47. if (this.editing && this.editing.enabled()) {
  48. this.editing.addHooks();
  49. }
  50. });
  51. this.on('remove', function () {
  52. if (this.editing && this.editing.enabled()) {
  53. this.editing.removeHooks();
  54. }
  55. });
  56. });