OpenLayers中每层多个图像

时间:2012-05-01 16:23:06

标签: image openlayers layer

是否可以在OpenLayers中的图层中放置多个图像?

理想情况下,我想将我的图片分组(每一层都是一个类别),这样我就可以整体显示和隐藏每个类别,而不是显示/隐藏每一张图片。

这可能吗?我发现了几个使用OpenLayers图像层(似乎只支持一个图像)或带有StyleMap的Vector层(也似乎只允许一个外部图像)的例子。

我是否忽略了某些内容或者需要付出更多努力(即创建自定义图层类型)?

提前致谢!

1 个答案:

答案 0 :(得分:1)

要将多个图像放在同一图层中,您可以像这样创建一个styleMap

var style = new OpenLayers.StyleMap({
    default :new OpenLayers.Style({
          'pointRadius': 10,
          'externalGraphic': '/images/${icon}.png'
    })
})

其中“$ {icon}”是该功能的属性。

在下一个例子中,我使用2个图像“star”和“home”

var path = new OpenLayers.Layer.Vector( "images" );
//set the styleMap
path.styleMap = style;
map.addLayers([path]);

//create a new feature
var pointHome = new OpenLayers.Geometry.Point(-57.533832,-25.33963);
var featureHome = new OpenLayers.Feature.Vector(pointHome);
//set the icon of the feature
featureHome.attributes["icon"] ="home";

var pointStar = new OpenLayers.Geometry.Point(-57.533371,-25.338946);
var featureStar = new OpenLayers.Feature.Vector(pointStar);
//set the icon of the feature
featureStar.attributes["icon"] ="star";

path.addFeatures([featureHome, featureStar]);
相关问题