我正在重新解析已经加载到 map 上的 KML,类似于此处的示例: http://openlayers.org/dev/examples/sundials.html并将其变成一个可点击的列表,将 map 集中在点击的点上,并显示它的弹出窗口。
这在 Google map 中确实很容易做到,但我找不到任何类似的 Openlayers 示例。有没有更简单的方法来做到这一点?我缺少什么内置的东西?
HTML:
<ul id="locationTable">
</ul>
JS:
htmlRows = "";
for(var feat in features) {
// Build details table
featId = features[feat].id; // determine the feature ID
title = jQuery(f).filter('[name=TITLE]').text();
htmlRow = "<li><a href="javascript:selectFeature('"+featId+"');\">"+title+"</a></li>";
htmlRows = htmlRows + htmlRow;
}
jQuery('#locationTable').append(htmlRows);
然后对于 selectFeature 函数:
function selectFeature(fid) {
for(var i = 0; i<kml.features.length;++i) {
if (kml.features[i].id == fid)
{
selected = new OpenLayers.Control.SelectFeature(kml.features[i]);
selected.clickFeature(); // make call to simulate Click event of feature
break;
}
}
}
最佳答案
我认为您应该删除“selected.clickFeature”调用,而是为要素图层中的“featureselected”事件创建一个事件监听器:
如果您在该事件中显示弹出窗口,您只需找到它并使用现有代码选择它,然后删除该行
selected.clickFeature();
旁注:您的特征服务器可以提供其他格式的数据吗?例如 WFS?不需要解析 KML 数据。
关于javascript - 在 Openlayers 中单击 map 外部时如何激活功能 + 弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4370993/