Code Monkey home page Code Monkey logo

leaflet.almostover's Introduction

Leaflet.AlmostOver

This plugin allows to detect mouse click and overing events on lines, with a tolerance distance.

It is useful if paths are drawn with a very small weight, or for clicks detection on mobile devices, for which finger precision can be a problem.

Play with online demo.

It requires Leaflet.GeometryUtil.

Build Status

Usage

    var map = L.map('map');
    ...
    var lines = L.geoJson(...);
    ...
    map.almostOver.addLayer(lines);

    map.on('almost:over', function (e) {
        alert('Almost clicked !');
    });

Events

Events triggered when mouse is almost over a layer :

  • almost:over (latlng, layer)
  • almost:move (latlng, layer)
  • almost:out (layer)
  • almost:click (latlng, layer)
  • almost:dblclick (latlng, layer)

Caveats

If the layer has many features, this plugin can slow down 'panning' performance, as, by default, it process 'mousemove' events. If only 'almost:click' or 'almost:dblclick' are needed, and 'almost' events related to 'mousemove' events are not needed, they can be disabled with almostOnMouseMove map option.

    var map = L.map('map', {
      almostOnMouseMove: false,
    });
    ...
    var lines = L.geoJson(...);
    ...
    map.almostOver.addLayer(lines);

    map.on('almost:click', function (e) {
        alert('Almost clicked !');
    });

Authors

Makina Corpus

leaflet.almostover's People

Contributors

bo-duke avatar fpuga avatar leplatrem avatar mhameon avatar sbats avatar siddharthbhagwan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

leaflet.almostover's Issues

L.Mixin.Events Deprecated

Please change L.Mixin.Events to L.Evented as L.Mixin.Events will be removed from future versions.

almost:out not fired when user move mouse along one line and then switch to another line

add else if something like:

_onMouseMove: function (e) {
        var closest = this.getClosest(e.latlng);
        if (closest) {
            if (!this._previous) {
                this._map.fire('almost:over', {layer: closest.layer,
                                               latlng: closest.latlng});
            } else if(this._previous.layer._leaflet_id != closest.layer._leaflet_id){

                  this._map.fire('almost:out', {layer: this._previous.layer});
                  this._map.fire('almost:over', {layer: closest.layer,
                                               latlng: closest.latlng}); 

            }
            this._map.fire('almost:move', {layer: closest.layer,
                                           latlng: closest.latlng});
        }
        else {
            if (this._previous) {
                this._map.fire('almost:out', {layer: this._previous.layer});
            }
        }
        this._previous = closest;
    },

cursor style

Hi,

Thanks for creating this fantastic plugin!

I am facing a tiny issue. I think the cursor style is a bit confusing when the cursor hovers over the layer. It is in grabbing mode when the user is over the almost overlayer and in Pointer Mode when the cursor is exactly over the line.

I am not sure if it is possible, but I think it would be better if the cursor style was changed to pointer when the cursor is almost over.

Best,

Plugin does not work with Leaflet 1.3.1

Hi there,

the following code snippet works fine when using leaflet-src.js version 1.0.2 but not using the newest leaflet version 1.3.1:

map.almostOver.addLayer(myPolyline]);

map.on('almost:over', function (e) {
                console.log('almost over line');
        });

With the new version I do not get any console.log statement when the mouse goes over the line.

Changing to the old leaflet file with no other changes it works perfect.

Any idea what is going wrong?

Thanks for your help,

Andrea

Maintaining references to original layer

I set a custom object property on the layer before adding it to the almostOver layer group. When almost:click is triggered that property is no longer present.

map.cableLayer = new L.featureGroup();

L.geoJson(data, {
    onEachFeature: function(feature, layer) {

        // layer.cableId set in the layer
        layer.cableId = layer.feature.properties.cable_id;

        map.cableLayer.addLayer(layer);

        layer.on('click', function(e) {

            sidebar.focusCable(e.target, e.latlng);
        });
    }
});

map.m.almostOver.addLayer(map.cableLayer);
map.cableLayer.addTo(map.m);

 map.m.on('almost:click', function(e) {

    console.log(e); // layer.cableId not present in e.layer or e.target
    sidebar.focusCable(e.target, e.latlng);
});

Could not resolve dependency

The listed leaflet peer dependency causes a conflict when installing with newer versions of npm. Would it be possible to update it to reference a 1.x.x version of leaflet? Everything still works as of leaflet 1.9.3 as far as I can tell.

Slows down app significantly

I love this feature, but when I use it, my app is significantly slower. There is a lot of latency when panning and zooming. Is there something I can do to speed it up?

Edit: I only need it for click events, not hover. I increased the sampling period to 1 second and it is much much better.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.