makinacorpus / Leaflet.GeometryUtil

Leaflet utility function on geometries
http://makinacorpus.github.io/Leaflet.GeometryUtil/
BSD 3-Clause "New" or "Revised" License
255 stars 94 forks source link

Closest layer between Circle and Line #10

Open prophe05 opened 10 years ago

prophe05 commented 10 years ago

When line traverse circle in center, what is the closest layer in case of

  1. mouse is over border of circle
  2. line is the first layer in the layers array? Now line is the closest layer to mouse position. But correct - circle is the closest layer. Need to find closest distance to mouse position using latlng of circle center and it's radius!

maybe edit method closestLayer: function (map, layers, latlng); like:

closestLayer: function (map, layers, latlng) {
        var mindist = Infinity,
            result = null,
            ll = null,
            distance = Infinity;

        for (var i = 0, n = layers.length; i < n; i++) {
            var layer = layers[i];
            // Single dimension, snap on points, else snap on closest
            if (typeof layer.getLatLng == 'function') {
                ll = layer.getLatLng();
                distance = L.GeometryUtil.distance(map, latlng, ll);

                if (layer instanceof L.Circle){
                    distance = distance - layer.getRadius();
                }
            }
            else {
                ll = L.GeometryUtil.closest(map, layer, latlng);
                if (ll) distance = ll.distance; // Can return null if layer has no points.
            }
            if (distance < mindist) {
                mindist = distance;
                result = {layer: layer, latlng: ll, distance: distance};
            }
        }
        return result;
    }
leplatrem commented 10 years ago

Indeed, currently, we ignore the circle radius to compute distances.

If you have any idea about a sound way to implement this, do not hesitate to contribute ! I would be glad to review your pull-request !

Thanks !

danyhoron commented 11 months ago

I created a PR that finds the closest point on a Circle. #101