在openlayers中更改feature.vector的外观

Yun*_*una 3 openlayers

我正在将我的代码从使用标记层转换为矢量图层.我在地图上显示标记作为Feature.Vector类时遇到问题(在我使用Feature类之前).

具体来说,我无法弄清楚如何使用我选择的独特图像显示每个功能.我已经看到了一些如何使用样式图改变一般特征样式的例子,但我还没有看到直接设置特征图像的方法.

这似乎是一个简单的问题,但我还没有找到解决方案.我目前正在尝试使用样式属性:

var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(ll.lon, ll.lat), {externalGraphic: "http://www.openlayers.org/dev/img/marker.png"}); 
Run Code Online (Sandbox Code Playgroud)

但是,这只会产生正常的默认图标(橙色圆圈).

设置功能的data.icon和创建标记同样不起作用.

提前致谢!

Ili*_*oly 7

您将占位符放在由features属性填充的图层样式中.

var style = OpenLayers.Util.extend({
    externalGraphic : "${icon}",
    pointRadius     : 15
}, OpenLayers.Feature.Vector.style['default']);

var layer   = new OpenLayers.Layer.Vector("Simple Geometry", { style: style }),
    point   = new OpenLayers.Geometry.Point(ll.lon, ll.lat),
    feature = new OpenLayers.Feature.Vector(point, { icon: "icon.png" });

layer.addFeature(feature);
Run Code Online (Sandbox Code Playgroud)

  • 如果忽略PointRadius,则外部图形根本不会显示.救了我,救了我.UPVOTED! (3认同)