Draw.Marker.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @class L.Draw.Marker
  3. * @aka Draw.Marker
  4. * @inherits L.Draw.Feature
  5. */
  6. L.Draw.Marker = L.Draw.Feature.extend({
  7. statics: {
  8. TYPE: 'marker'
  9. },
  10. options: {
  11. icon: new L.Icon.Default(),
  12. repeatMode: false,
  13. zIndexOffset: 2000 // This should be > than the highest z-index any markers
  14. },
  15. // @method initialize(): void
  16. initialize: function (map, options) {
  17. // Save the type so super can fire, need to do this as cannot do this.TYPE :(
  18. this.type = L.Draw.Marker.TYPE;
  19. L.Draw.Feature.prototype.initialize.call(this, map, options);
  20. },
  21. // @method addHooks(): void
  22. // Add listener hooks to this handler.
  23. addHooks: function () {
  24. L.Draw.Feature.prototype.addHooks.call(this);
  25. if (this._map) {
  26. this._tooltip.updateContent({ text: L.drawLocal.draw.handlers.marker.tooltip.start });
  27. // Same mouseMarker as in Draw.Polyline
  28. if (!this._mouseMarker) {
  29. this._mouseMarker = L.marker(this._map.getCenter(), {
  30. icon: L.divIcon({
  31. className: 'leaflet-mouse-marker',
  32. iconAnchor: [20, 20],
  33. iconSize: [40, 40]
  34. }),
  35. opacity: 0,
  36. zIndexOffset: this.options.zIndexOffset
  37. });
  38. }
  39. this._mouseMarker
  40. .on('click', this._onClick, this)
  41. .addTo(this._map);
  42. this._map.on('mousemove', this._onMouseMove, this);
  43. this._map.on('click', this._onTouch, this);
  44. }
  45. },
  46. // @method removeHooks(): void
  47. // Remove listener hooks from this handler.
  48. removeHooks: function () {
  49. L.Draw.Feature.prototype.removeHooks.call(this);
  50. if (this._map) {
  51. if (this._marker) {
  52. this._marker.off('click', this._onClick, this);
  53. this._map
  54. .off('click', this._onClick, this)
  55. .off('click', this._onTouch, this)
  56. .removeLayer(this._marker);
  57. delete this._marker;
  58. }
  59. this._mouseMarker.off('click', this._onClick, this);
  60. this._map.removeLayer(this._mouseMarker);
  61. delete this._mouseMarker;
  62. this._map.off('mousemove', this._onMouseMove, this);
  63. }
  64. },
  65. _onMouseMove: function (e) {
  66. var latlng = e.latlng;
  67. this._tooltip.updatePosition(latlng);
  68. this._mouseMarker.setLatLng(latlng);
  69. if (!this._marker) {
  70. this._marker = new L.Marker(latlng, {
  71. icon: this.options.icon,
  72. zIndexOffset: this.options.zIndexOffset
  73. });
  74. // Bind to both marker and map to make sure we get the click event.
  75. this._marker.on('click', this._onClick, this);
  76. this._map
  77. .on('click', this._onClick, this)
  78. .addLayer(this._marker);
  79. }
  80. else {
  81. latlng = this._mouseMarker.getLatLng();
  82. this._marker.setLatLng(latlng);
  83. }
  84. },
  85. _onClick: function () {
  86. this._fireCreatedEvent();
  87. this.disable();
  88. if (this.options.repeatMode) {
  89. this.enable();
  90. }
  91. },
  92. _onTouch: function (e) {
  93. // called on click & tap, only really does any thing on tap
  94. this._onMouseMove(e); // creates & places marker
  95. this._onClick(); // permanently places marker & ends interaction
  96. },
  97. _fireCreatedEvent: function () {
  98. var marker = new L.Marker.Touch(this._marker.getLatLng(), { icon: this.options.icon });
  99. L.Draw.Feature.prototype._fireCreatedEvent.call(this, marker);
  100. }
  101. });