Javascript 加载后打开图层可以';t画
我做了一个函数,它绘制并保存到GeoJson。我当时得到: 接下来,当我加载特征时,可以在需要绘制时计时,我得到错误:“layer.getSource().getFeaturesCollection()”==null,但我使用featureCollection类型保存了特征。当我使用getFeatures()时,它返回特性数组 开始加载层:Javascript 加载后打开图层可以';t画,javascript,jquery,openlayers,Javascript,Jquery,Openlayers,我做了一个函数,它绘制并保存到GeoJson。我当时得到: 接下来,当我加载特征时,可以在需要绘制时计时,我得到错误:“layer.getSource().getFeaturesCollection()”==null,但我使用featureCollection类型保存了特征。当我使用getFeatures()时,它返回特性数组 开始加载层: if(map_info != "") { var layer_area = new ol.layer.Vector({
if(map_info != "") {
var layer_area = new ol.layer.Vector({
source: new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(map_info['layers'][0])
}),
});
var layer_branch = new ol.layer.Vector({
source: new ol.source.Vector({ features: (new ol.format.GeoJSON()).readFeatures(map_info['layers'][1]) })
});
var layer_region = new ol.layer.Vector({
source: new ol.source.Vector({ features: (new ol.format.GeoJSON()).readFeatures(map_info['layers'][2]) })
});
var layer_ambon = new ol.layer.Vector({
source: new ol.source.Vector({ features: (new ol.format.GeoJSON()).readFeatures(map_info['layers'][3]) })
});
var view=new ol.View({
center: [{{map.pos_x}}, {{map.pos_y}}],
zoom: {{map.zoom}}
});
}else {
var layer_area = new ol.layer.Vector({
source: new ol.source.Vector({ features: new ol.Collection() })
});
var layer_branch = new ol.layer.Vector({
source: new ol.source.Vector({ features: new ol.Collection() })
});
var layer_region = new ol.layer.Vector({
source: new ol.source.Vector({ features: new ol.Collection() })
});
var layer_ambon = new ol.layer.Vector({
source: new ol.source.Vector({ features: new ol.Collection() })
});
var view=new ol.View({
center: [2097096.387839827, 6887410.172738342],
zoom: 5
});
}
绘图功能:
$("#draw_ambon").on("click", function(){
var layer=manager["ambon"];
map.removeInteraction(draw);
select !== null ? map.removeInteraction(select) : 0;
setInteraction(layer.getSource().getFeaturesCollection(), "Point");
layer.setStyle(pointStyleFunction);
layer.setMap(map);
draw.on("drawend", function(e){
layer.getSource().addFeatures(e.feature);
e.feature.setProperties({
'id': layer.getSource().getFeaturesCollection().getLength(),
'type': "ambona",
'name': ''
})
})
});
我解决了。
只需将类别集合添加到源中的“功能”选项:
source: new ol.source.Vector({ features: new ol.Collection(new ol.format.GeoJSON()).readFeatures(map_info['layers'][1])) })