【发布时间】:2015-06-05 01:42:52
【问题描述】:
我在使用 OpenLayers 3.5 时遇到问题。我正在尝试使用一次性加载策略从 GeoJSON 文件中获取功能。我正在向已经实例化的地图添加一个新图层。我的代码如下所示:
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);
});
这确实有效。我在这里用头撞墙。有没有人有任何想法?非常感谢!
【问题讨论】:
-
对
layerInfo.url的请求是否甚至出现在浏览器的网络选项卡中?这些症状看起来有点像以同步方式使用异步代码时发生的情况,但我没有足够的 OL3 经验来提供进一步的帮助。如果我遇到问题,我会在浏览器的调试器中设置一些断点,并尝试将请求一直跟踪到 OL 的内部。 -
一个请求确实出去了,它又成功回来了。奇怪的是,我已经完全按照互联网上的所有示例进行操作(这很奇怪,因为当我尝试加载由有效的示例代码提供的示例 GeoJSON 时,加载没有问题功能。)这会向我暗示我的 GeoJSON 可能有问题,除了当我尝试以自己的方式手动加载它时,它可以工作。我的头很痛...感谢您的回复!
-
也可能是投影问题。
标签: gis geojson openlayers-3