Gis Openlayers 3.5和GeoJSON
我对OpenLayers 3.5有问题。我试图使用一次性加载策略从GeoJSON文件中获取功能。我正在向已经实例化的地图添加一个新图层。我的代码如下所示:Gis Openlayers 3.5和GeoJSON,gis,geojson,openlayers-3,Gis,Geojson,Openlayers 3,我对OpenLayers 3.5有问题。我试图使用一次性加载策略从GeoJSON文件中获取功能。我正在向已经实例化的地图添加一个新图层。我的代码如下所示: var vectorSource=new ol.source.Vector({ url:layerInfo.url, 格式:new ol.format.GeoJSON() }); var pointsLayer=new ol.layer.Vector({ 来源:矢量源, 样式:styleFunc }); 那就是.map.addLayer(po
var vectorSource=new ol.source.Vector({
url:layerInfo.url,
格式:new ol.format.GeoJSON()
});
var pointsLayer=new ol.layer.Vector({
来源:矢量源,
样式:styleFunc
});
那就是.map.addLayer(pointsLayer);
pointsLayer.setVisible(true);
但是,没有显示任何内容,当我检查pointsLayer.getSource().getFeatures()
时,我发现实际上没有加载任何功能
因此,现在我尝试以不同的方式加载功能:
var=this;
$.get(layerInfo.url,函数(响应){
var vectorSource=新的ol.source.Vector({
特性:(新建ol.format.GeoJSON()).readFeatures(响应)
});
var pointsLayer=new ol.layer.Vector({
来源:矢量源,
样式:styleFunc
});
那就是.map.addLayer(pointsLayer);
pointsLayer.setVisible(true);
});
这确实有效。我的头撞在墙上了。有人有什么想法吗?非常感谢 这就是我现在加载数据的方式,“数据”是我的儿子
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');
}
根据Juan Carlos的回答,并供将来参考,下面是一个函数,它使用Angular的
$http
服务创建了一个基于GeoJSON的层:
function buildStationsLayer() {
var geoJsonUrl = config.stationsGeoDataUrl /*+ some parameters....*/ ;
var source = new ol.source.Vector({});
var format = new ol.format.GeoJSON();
var stationsLayer = new ol.layer.Vector({
source: source,
style: stationStyleFunction //function that styles conditionally depending on resolution
});
//manually load the GeoJSON features into the layer
$http.get(geoJsonUrl).success(function(data){
for(var i=0; i<data.features.length; i++){
var feature = format.readFeature(data.features[i]);
source.addFeature(feature);
}
});
return stationsLayer;
}
函数buildStationsLayer(){
var geoJsonUrl=config.stationsGeoDataUrl/*+一些参数….*/;
var source=新的ol.source.Vector({});
var format=new ol.format.GeoJSON();
var stationsLayer=新ol.layer.Vector({
资料来源:资料来源,
style:stationStyleFunction//根据分辨率有条件地设置样式的函数
});
//手动将GeoJSON功能加载到层中
$http.get(geoJsonUrl).success(函数(数据){
对于(var i=0;我请求layerInfo.url
甚至出现在浏览器的“网络”选项卡中?症状看起来有点像以同步方式使用异步代码时发生的情况,但我没有足够的OL3经验来进一步提供帮助。如果我遇到问题,我会在浏览器的调试器中设置一些断点,并尝试跟踪请求但奇怪的是,我在互联网上完全遵循了所有的示例(这是非常奇怪的,因为当我尝试加载由有效的示例代码提供的示例GeoJSON时,加载这些功能并没有问题)这对我来说可能是我的GeoJSON有点问题,除了当我尝试以自己的方式手动加载它时,它工作了。我的头很痛……谢谢你的回答!这可能也是一个投影问题。仍然很恼火,因为它没有以文档中的方式工作,但这个解决方案对我来说非常有效。谢谢!没关系,很高兴能帮到你。让我投票给你:)我也有同样的问题。不知何故,ReadFeatures一次阅读对我来说也不管用。
var vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'data/geojson/countries.geojson',
format: new ol.format.GeoJSON()
}),
style: function(feature, resolution) {
style.getText().setText(resolution < 5000 ? feature.get('name') : '');
return styles;
}
});