2012-05-01 145 views
4

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

理想情况下,我想将我的图片按照类别分组(每层都是一个类别),因此我可以将每个类别作为一个整体进行显示和隐藏,而不是显示/隐藏每张图片。

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

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

在此先感谢!

回答

1

为了把多张图片在同一层,你可以创建一个StyleMap设定这样

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

其中“$ {}图标”是要素的属性。

在下面的例子中,我用2个图像 “明星” 和 “家”

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]);