当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Unable to select vector when hover is active

问题描述:

Well, I want a nice popup when one is hover but I also want the vector selectable.

If I skip the hover part, it works like charm, but else :/ Is there something I've missed?

vectors.removeAllFeatures();

var geojson_format = new OpenLayers.Format.GeoJSON();

vectors.addFeatures(geojson_format.read(data));

var selectCtrl = new OpenLayers.Control.SelectFeature(vectors,

{

clickout: true

});

map.addControl(selectCtrl);

selectCtrl.activate();

vectors.events.on({

"featureselected": featureSelected

});

var hover = new OpenLayers.Control.SelectFeature(vectors,{

hover: true,

highlightOnly: true,

eventListeners: {

featurehighlighted: featureHover,

featureunhighlighted: featureUnHover

}

});

map.addControl(hover);

hover.activate();

网友答案:

I only had written the code in wrong order. The map.addControl(hover); hover.activate(); should be before

map.addControl(selectCtrl);
 selectCtrl.activate();
 vectors.events.on({
     "featureselected": featureSelected
 });

So, now it works!

网友答案:

Does this really work because the way I see it, once you have 2 map.addControl created with OpenLayers.Control.SelectFeature there is 1 that doesn't work. In fact, I have 2 layers, a bus layer and a bus-stops layer. When I activate them all, only the hover on the bus layer works. When I do not activate the controle of the buslayer, then suddently the bus-stops layer works. So they both work individually. Your problem here is about the only one I found here with 2 addControl lines. I also tried to use autoActivate but same result.

update

It's actually intented not to work anymore, apparently you cannot create 2 Control features. Instead you need to create one and apply this to both layers. The support for this was removed in 2.7 as stated in the docs. There is a good example of how to do this with 2 different Vector Layers. This took me days to realize. You just need a way in your popup to determine what layer you are on and act accordingly so it seems.

分享给朋友:
您可能感兴趣的文章:
随机阅读: