OpenLayers不能在map上绘制geoJSON

OpenLayers can't draw geoJSON on map

本文关键字:绘制 geoJSON map 不能 OpenLayers      更新时间:2023-09-26

我试图在使用OpenLayers 3的开放街道地图上绘制一些简单的形状(主要是线)。我使用的代码几乎是直接从他们的网站上的一个例子复制的,但它似乎不适合我。

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,而大多数web地图一般使用3857。

尝试呼叫:

(new ol.format.GeoJSON()).readFeatures(arr[i].geoJSON,{
    featureProjection:"EPSG:3857"
});