简体   繁体   English

OpenLayers:点击后无法选择功能

[英]OpenLayers: Can't select feature after clickout

I'm trying a simple test where I have a base image with draggable features on top. 我正在尝试一个简单的测试,其中我有一个基本图像,顶部有可拖动的功能。 These point features have associated external graphics (I'd prefer to use polygons with associated graphics but that's a whole other question). 这些点特征具有相关的外部图形(我更喜欢使用具有相关图形的多边形,但这是另一个问题)。

I have a hover control that works perfectly. 我有一个完美的悬停控件。 I also have a select control that almost works. 我也有一个几乎可以工作的选择控件。 However I cannot reselect a feature once I have clicked out once. 但是,一旦点击一下,我就无法重新选择一个功能。 Similarly, I cannot select features if I click anywhere outside of the features before trying to select one. 同样,如果我在尝试选择一个功能之前单击功能外的任何位置,我就无法选择功能。

I hope that's clear - but if not then this example should be (and you can also see the whole code) http://sigfrid.co.uk/oltest/simple.html 我希望这很清楚 - 但如果不是那么这个例子应该是(你也可以看到整个代码) http://sigfrid.co.uk/oltest/simple.html

I'll put what I think are the key bits of code below.... 我会把我认为是下面代码的关键部分......

Create a map 创建一个地图

var map = new OpenLayers.Map({
    div:'map',
});

Add a base layer 添加基础层

    var base = new OpenLayers.Layer.Image(
    'Base level',
    'img/base.png',
    new OpenLayers.Bounds(-1000, -1000, 1000, 1000),
    new OpenLayers.Size(864,864),
    options
);
map.addLayer(base);

Add styles 添加样式

var markerStyleMap = new OpenLayers.StyleMap 
({
    "default": new OpenLayers.Style(template, {context: context}),
    "hover": new OpenLayers.Style ({graphicOpacity:0.5}),
    "select": new OpenLayers.Style ({graphicOpacity:0.1})});

Add points 添加积分

pt1 = new OpenLayers.Geometry.Point(0,0);
pt1Feature = new OpenLayers.Feature.Vector(pt1);
...
markerLayer.addFeatures([pt1Feature,pt2Feature,pt3Feature]);

Add unique lookup for the different marker images 为不同的标记图像添加唯一查找

var lookup = {
    "f1": {externalGraphic:"img/f1.png"},
    "f2": {externalGraphic:"img/f2.png"},
    "f3": {externalGraphic:"img/f3.png"},
}

markerStyleMap.addUniqueValueRules("default", "type", lookup);
markerLayer.styleMap = markerStyleMap;
markerLayer.features[0].attributes.type = "f1";
    ...

Add controls 添加控件

var dragControl = new OpenLayers.Control.DragFeature(markerLayer) 
map.addControl(dragControl);
dragControl.activate();

var highlightCtrl = new OpenLayers.Control.SelectFeature(markerLayer, {
    hover: true,
    highlightOnly: true,
    renderIntent: "hover",
});
map.addControl(highlightCtrl);
highlightCtrl.activate();


var selectCtrl = new OpenLayers.Control.SelectFeature(markerLayer, {
    clickout: true,        
    renderIntent: "select",
});
map.addControl(selectCtrl);
selectCtrl.activate();

Thanks for any help, 谢谢你的帮助,

Nick 缺口

Turns out drag and select are not compatible. 结果拖动和选择不兼容。 However, there are some work arounds such as 但是,有一些工作,如

http://fastr.wordpress.com/2012/04/17/openlayers-selectfeature-where-did-i-clicked-2/ http://fastr.wordpress.com/2012/04/17/openlayers-selectfeature-where-did-i-clicked-2/

although this does add some event problems... 虽然这确实增加了一些事件问​​题......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM