Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make marker draggable and clickable

I have a vector-layer for displaying markers as features on my map, but now mather, what I do, they are eighter draggable or clickable. What can I do to get both?

/*** Init Drag Marker
*/
    var dragFeature = new OpenLayers.Control.DragFeature(layerTrackMarker, 
        {'onComplete': updateMarker}
    );
    map.addControl(dragFeature);
    dragFeature.activate();

/*** Init Click Marker
*/
    var clickFeature = new OpenLayers.Control.SelectFeature(layerTrackMarker,
        {}
    );
    map.addControl(clickFeature);
    layerTrackMarker.events.on({
                    "featureselected": function(e) {
                        var markerId = e.feature.geometry.id;
                        removeMarker(markerId);
                        /* ToDo: show context menu */
                    },
                    "featureunselected": function(e) {
                        /* ToDo: hide context menu */
                    }
                });
    clickFeature.activate();
like image 475
madc Avatar asked Aug 05 '11 09:08

madc


People also ask

How do I add a clicker marker?

Add onclick listener In the callback function, e. latLng is the position on Google Map clicked by user. Hence, it is passed as an argument into addMarker so that a new marker is added at the position clicked (9). If you are unsure what onclick listener means, you can read more about JavaScript Event Listener.


1 Answers

I didn't test it, but wouldn't something like this work?

var dragged = false;
var dragFeature = new OpenLayers.Control.DragFeature(layerTrackMarker, {
    onComplete: function(){
        if(dragged){
            dragCallback();
        }else{
            clickCallback();
        }
    },
    onStart: function(){
        dragged = false;
    },
    onDrag: function(){
        dragged = true;
    }
});
map.addControl(dragFeature);
dragFeature.activate();

Edit: onComplete doesn't fire if there was no actual drag. Here's the working code

var dragged = false;
var dragFeature = new OpenLayers.Control.DragFeature(pointLayer, {
    onStart: function(){dragged = false;},
    onDrag: function() {dragged = true;}
});

map.addControl(dragFeature);
dragFeature.activate();

map.events.register("mouseup", map , function(e){
    if(dragged == true){
        alert("dragged");
    }else{
        alert("clicked");
    }
});

jsFiddle: http://jsfiddle.net/reKRr/5/

EDIT: this is the less hacky way of doing it

var selectFeature = new OpenLayers.Control.SelectFeature(
    pointLayer,
    {
        onSelect: function(){            
            alert("selected");
        }
    }
);

var dragFeature = new OpenLayers.Control.DragFeature(pointLayer, {
    onComplete: function(){
        alert("drag");
    }
});

dragFeature.handlers['drag'].stopDown = false; 
dragFeature.handlers['drag'].stopUp = false; 
dragFeature.handlers['drag'].stopClick = false; 
dragFeature.handlers['feature'].stopDown = false; 
dragFeature.handlers['feature'].stopUp = false; 
dragFeature.handlers['feature'].stopClick = false; 

map.addControls([selectFeature,dragFeature]);

selectFeature.activate();
dragFeature.activate();
like image 183
Ilia Choly Avatar answered Oct 05 '22 13:10

Ilia Choly