Skip to content

Select nearest feature? #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Infiniverse opened this issue Dec 2, 2016 · 0 comments
Open

Select nearest feature? #82

Infiniverse opened this issue Dec 2, 2016 · 0 comments

Comments

@Infiniverse
Copy link
Contributor

The default SelectionInteraction is a bit tricky to use - quite often it is not easy to click on a feature, especially if it's a narrow line. One approach to resolving this is outlined in:

http://jsfiddle.net/XfEmn/

It sets things up so that when a selection is made, the nearest feature is selected instead, which makes it much easier to select features.

Is this an approach could be integrated into v-ol3?

(function() {

//util func for creating some poitn features
function createFeatures () {
    var points = [
        {"lon": 10, "lat": 60},
        {"lon": -10, "lat": 40},
        {"lon": 30, "lat": 30},
        {"lon": 20, "lat": 20}
    ];

    return _.map(points, function(point, idx) {
        var p = new OpenLayers.Geometry.Point(point.lon, point.lat).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
        return new OpenLayers.Feature.Vector(p, {"index": idx});
    });
}

var map = new OpenLayers.Map({
    div: "map",
    theme: null,
    
    layers: [
        new OpenLayers.Layer.OSM("OpenStreetMap", null, {
            transitionEffect: "resize"              
        })
    ],
    center: new OpenLayers.LonLat(0, 0),
    zoom: 1
}); 

var nodeLayer = new OpenLayers.Layer.Vector();
nodeLayer.addFeatures(createFeatures());
map.addLayer(nodeLayer);

var selectFeatureControl = new OpenLayers.Control.SelectFeature(nodeLayer);
map.addControl(selectFeatureControl);

map.events.register("click", map,  function(e) {
    selectFeatureControl.deactivate();
    var pos = this.getLonLatFromPixel(e.xy);        
    var point =  new OpenLayers.Geometry.Point(pos.lon, pos.lat);
    var closest =_.min(nodeLayer.features, function(feature) {
        return feature.geometry.distanceTo(point);
    });
    selectFeatureControl.activate();
    selectFeatureControl.select(closest);        
});

}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant