Javascript 无法以编程方式将要素添加到图层
我的地图上附有一个点击事件。在这个点击事件中,我触发了一个函数,该函数应该向地图中添加一个特征,但现在什么也没发生。我这样试过:Javascript 无法以编程方式将要素添加到图层,javascript,openlayers-3,Javascript,Openlayers 3,我的地图上附有一个点击事件。在这个点击事件中,我触发了一个函数,该函数应该向地图中添加一个特征,但现在什么也没发生。我这样试过: function boo (map, layer){ var source = layer.getSource(); var thing = new ol.geom.Polygon( [[ ol.proj.transform([-16,-22], 'EPSG:4326', 'EPSG:3857'), ol.proj.tran
function boo (map, layer){
var source = layer.getSource();
var thing = new ol.geom.Polygon( [[
ol.proj.transform([-16,-22], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([-44,-55], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([-88,75], 'EPSG:4326', 'EPSG:3857')
]]);
var featurething = new ol.Feature({
name: "Thing",
geometry: thing,
style: function() {
console.log("Never see this text");
return new ol.style.Style({
fill: new ol.style.Fill({
color: "rgba(192,192,192,1)"
}),
stroke: new ol.style.Stroke({
color: "rgba(192,192,192,1)",
width: 10
})
})
}
});
source.addFeature( featurething );
// see no error messages, but still no feature is added to the map
}
这是一个OL3错误
别那么快
函数的第一个参数应该是click
事件。另一个错误:构造函数中没有style
参数
创建要素样式后设置要素样式。因此:
featureuring.setStyle(某些样式或函数);
顺便说一句,我检查了source.getFeatures()
,发现这个新功能在那里,并且有一些真实的坐标,但由于某种原因它仍然没有显示在地图上。这是一个OL3错误。由于某些原因,像我这样设置特征样式是行不通的。并且应该使用feature.setStyle(…)单独设置