OpenLayers 无法在地图上绘制 geoJSON
OpenLayers can't draw geoJSON on map
我正在尝试使用 OpenLayers 3 在 Open Street Map 上绘制一些简单的形状(主要是线条)。我使用的代码几乎是直接从他们网站上的 example 复制的,但是它似乎对我不起作用。
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(arr[i].geoJSON)
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: styleFunction
});
map.addLayer(vectorLayer);
其中styleFunction
与示例中的函数相同,arr[i].geoJSON
是一个完全有效的geoJSON对象。
问题是,它不绘制任何东西。我做错了什么?
您可能需要为 readFeatures
方法提供选项。 GeoJSON 投影通常使用 4326,而大多数网络地图通常使用 3857。
尝试调用:
(new ol.format.GeoJSON()).readFeatures(arr[i].geoJSON,{
featureProjection:"EPSG:3857"
});
我正在尝试使用 OpenLayers 3 在 Open Street Map 上绘制一些简单的形状(主要是线条)。我使用的代码几乎是直接从他们网站上的 example 复制的,但是它似乎对我不起作用。
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(arr[i].geoJSON)
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: styleFunction
});
map.addLayer(vectorLayer);
其中styleFunction
与示例中的函数相同,arr[i].geoJSON
是一个完全有效的geoJSON对象。
问题是,它不绘制任何东西。我做错了什么?
您可能需要为 readFeatures
方法提供选项。 GeoJSON 投影通常使用 4326,而大多数网络地图通常使用 3857。
尝试调用:
(new ol.format.GeoJSON()).readFeatures(arr[i].geoJSON,{
featureProjection:"EPSG:3857"
});