2011-08-05 221 views
6

我有一個向量圖層,用於在地圖上顯示標記作爲要素,但現在,我做了什麼,它們是可拖動的還是可點擊的。我能做些什麼來獲得兩個?使標記可拖動並可點擊

/*** 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(); 

回答

9

我沒有測試它,但不會像這樣工作嗎?

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(); 

編輯:如果沒有實際拖動,onComplete不會觸發。這裏的工作代碼

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/

編輯:這是做的少哈克的方式,

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(); 
+0

哇,是的。你救了我的一天! 還剩下一件事。有沒有可能在鼠標上取消選擇標記,所以它會識別單擊同一標記的secound? – madc

+0

謝謝,明白了。 \t ONSELECT:函數(){ \t \t \t \t //我的Funktion ... \t \t \t \t this.unselectAll(); \t} – madc