Edit.Circle.js 1.5 KB

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