2

我有一个 OpenLayers.Layer.Vector 层,如下所示:

layer = new OpenLayers.Layer.Vector("zzzzz", {
  strategies: [
    new OpenLayers.Strategy.Fixed(),
  ],
  // styleMap: new OpenLayers.StyleMap({
  //   'default': { .. layer styles here ..}
  //   'select': { .. selected styles here ..}
  // }),
  protocol: new OpenLayers.Protocol.HTTP({
    url: "/kml/zzzzzz.kml",
    renderers: ['Canvas','SVG'],
    format: new OpenLayers.Format.KML({
      extractStyles: false, 
      extractAttributes: true,
      maxDepth: 0
    })
  })
});

然后我有一个 OpenLayers.Controls.SelectFeature 控件,如下所示:

selectFeature = new OpenLayers.Control.SelectFeature(
    [layer],
    {
      renderIntent: "select",
      clickout: true,
      toggle: true
    }
);

然后,我将它添加到地图中,如下所示:

map = new OpenLayers.Map('map');
map.addLayer(layer);
map.addControl(selectFeature);
selectFeature.activate();

现在,在我取消注释这些 styleMap 行的那一刻,我的所有功能都变得不可点击且无响应不仅样式不反映状态,而且状态似乎根本没有改变。

我需要做什么来维护自定义样式并使图层可悬停和/或可点击?

4

1 回答 1

1

你是如何设计意图的?你的符号哈希值是多少'default': { .. layer styles here ..}???

我认为您正在创建一个“空”对象并仅为您的默认和选择样式设置一些属性,例如:

default: {
    strokeColor: "red"
}

这会产生一种只有一个属性且没有 lineWidth、strokeOpacity 等的样式。

下一个代码对我来说很好。另请注意,renderers必须在层中而不是在协议中指定。

    var map = new OpenLayers.Map("map");

    // Create an OpenStreeMap raster layer and add to the map
    var osm = new OpenLayers.Layer.OSM();
    map.addLayer(osm);

    // Set view to zoom maximum map extent
    map.zoomToMaxExtent();

    // Create symbolizers inherited from the predefined styles and change only some properties.
    var defaultStyleSymbolizer = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
    defaultStyleSymbolizer.strokeColor = "green";
    var selectStyleSymbolizer = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['select']);
    selectStyleSymbolizer.strokeColor = "red";

    // Create a vector layer
    var layer = new OpenLayers.Layer.Vector("zzzzz", {
      strategies: [
        new OpenLayers.Strategy.Fixed(),
      ],
      // Set the renderers
      renderers: ['Canvas','SVG'],
      // Apply the style map
      styleMap: new OpenLayers.StyleMap({
        'default': defaultStyleSymbolizer,
        'select': selectStyleSymbolizer
      }),
      protocol: new OpenLayers.Protocol.HTTP({
        url: "./global_undersea.kml",
        format: new OpenLayers.Format.KML({
          extractStyles: false, 
          extractAttributes: true,
          maxDepth: 0
        })
      })
    });

    // The select control
    var selectFeature = new OpenLayers.Control.SelectFeature([layer], {
      renderIntent: "select",
      clickout: true,
      toggle: true
    });

    // Add layer and control
    map.addLayer(layer);
    map.addControl(selectFeature);
    selectFeature.activate();
于 2013-06-17T20:23:20.157 回答