leaflet.almostover.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. L.Map.mergeOptions({
  2. almostOver: true
  3. });
  4. L.Handler.AlmostOver = L.Handler.extend({
  5. includes: L.Mixin.Events,
  6. options: {
  7. distance: 20, // pixels
  8. samplingPeriod: 50, // ms
  9. },
  10. initialize: function (map) {
  11. this._map = map;
  12. this._layers = [];
  13. this._previous = null;
  14. this._marker = null;
  15. this._buffer = 0;
  16. // Reduce 'mousemove' event frequency
  17. this.__mouseMoveSampling = (function () {
  18. var timer = new Date();
  19. return function (e) {
  20. var date = new Date(),
  21. filtered = (date - timer) < this.options.samplingPeriod;
  22. if (filtered || this._layers.length === 0) {
  23. return; // Ignore movement
  24. }
  25. timer = date;
  26. this._map.fire('mousemovesample', {latlng: e.latlng});
  27. };
  28. })();
  29. },
  30. addHooks: function () {
  31. this._map.on('mousemove', this.__mouseMoveSampling, this);
  32. this._map.on('mousemovesample', this._onMouseMove, this);
  33. this._map.on('click dblclick', this._onMouseClick, this);
  34. var map = this._map;
  35. function computeBuffer() {
  36. this._buffer = this._map.layerPointToLatLng([0, 0]).lat -
  37. this._map.layerPointToLatLng([this.options.distance,
  38. this.options.distance]).lat;
  39. }
  40. this._map.on('viewreset zoomend', computeBuffer, this);
  41. this._map.whenReady(computeBuffer, this);
  42. },
  43. removeHooks: function () {
  44. this._map.off('mousemovesample');
  45. this._map.off('mousemove', this.__mouseMoveSampling, this);
  46. this._map.off('click dblclick', this._onMouseClick, this);
  47. },
  48. addLayer: function (layer) {
  49. if (typeof layer.eachLayer == 'function') {
  50. layer.eachLayer(function (l) {
  51. this.addLayer(l);
  52. }, this);
  53. }
  54. else {
  55. if (typeof this.indexLayer == 'function') {
  56. this.indexLayer(layer);
  57. }
  58. this._layers.push(layer);
  59. }
  60. },
  61. removeLayer: function (layer) {
  62. if (typeof layer.eachLayer == 'function') {
  63. layer.eachLayer(function (l) {
  64. this.removeLayer(l);
  65. }, this);
  66. }
  67. else {
  68. if (typeof this.unindexLayer == 'function') {
  69. this.unindexLayer(layer);
  70. }
  71. var index = this._layers.indexOf(layer);
  72. this._layers.splice(index, 1);
  73. }
  74. },
  75. getClosest: function (latlng) {
  76. var snapfunc = L.GeometryUtil.closestLayerSnap,
  77. distance = this.options.distance;
  78. var snaplist = [];
  79. if (typeof this.searchBuffer == 'function') {
  80. snaplist = this.searchBuffer(latlng, this._buffer);
  81. }
  82. else {
  83. snaplist = this._layers;
  84. }
  85. return snapfunc(this._map, snaplist, latlng, distance, false);
  86. },
  87. _onMouseMove: function (e) {
  88. var closest = this.getClosest(e.latlng);
  89. if (closest) {
  90. if (!this._previous) {
  91. this._map.fire('almost:over', {layer: closest.layer,
  92. latlng: closest.latlng});
  93. }
  94. else if (L.stamp(this._previous.layer) != L.stamp(closest.layer)) {
  95. this._map.fire('almost:out', {layer: this._previous.layer});
  96. this._map.fire('almost:over', {layer: closest.layer,
  97. latlng: closest.latlng});
  98. }
  99. this._map.fire('almost:move', {layer: closest.layer,
  100. latlng: closest.latlng});
  101. }
  102. else {
  103. if (this._previous) {
  104. this._map.fire('almost:out', {layer: this._previous.layer});
  105. }
  106. }
  107. this._previous = closest;
  108. },
  109. _onMouseClick: function (e) {
  110. var closest = this.getClosest(e.latlng);
  111. if (closest) {
  112. this._map.fire('almost:' + e.type, {layer: closest.layer,
  113. latlng: closest.latlng});
  114. }
  115. },
  116. });
  117. if (L.LayerIndexMixin !== undefined) {
  118. L.Handler.AlmostOver.include(L.LayerIndexMixin);
  119. }
  120. L.Map.addInitHook('addHandler', 'almostOver', L.Handler.AlmostOver);