我在使用 OpenLayers 3.5 时遇到问题。我正在尝试使用一次性加载策略从 GeoJSON 文件中获取特征。我正在向已经实例化的 map 添加一个新图层。我的代码如下所示:
var vectorSource = new ol.source.Vector({
url: layerInfo.url,
format: new ol.format.GeoJSON()
});
var pointsLayer = new ol.layer.Vector({
source: vectorSource,
style: styleFunc
});
that.map.addLayer(pointsLayer);
pointsLayer.setVisible(true);
但是,没有任何显示,当我检查
pointsLayer.getSource().getFeatures()
时,我发现实际上没有加载任何功能。所以,现在我尝试以不同的方式加载功能:
var that = this;
$.get(layerInfo.url, function(response) {
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(response)
});
var pointsLayer = new ol.layer.Vector({
source: vectorSource,
style: styleFunc
});
that.map.addLayer(pointsLayer);
pointsLayer.setVisible(true);
});
这确实有效。我正在用头撞墙。有没有人有任何想法?非常感谢!
最佳答案
这就是我现在加载数据的方式,“数据”是我的 GJson
var wktTraffic = new ol.source.Vector({
});
var trafficLayer = new ol.layer.Vector({
source: wktTraffic,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'blue',
width: 5
})
})
});
function showData(data) {
var format = new ol.format.WKT();
var feature;
$.each(data, function (i, link) {
feature = format.readFeature(link.geom);
wktTraffic.addFeature(feature);
})
console.log('done load map');
}
关于gis - Openlayers 3.5 和 GeoJSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30657032/