OpenLayers无法在地图上绘制geoJSON

时间:2015-07-20 19:53:03

标签: javascript openstreetmap openlayers-3

我试图使用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对象。

问题是,它没有任何吸引力。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您可能需要为readFeatures方法提供选项。 GeoJSON投影通常使用4326,而大多数网络地图通常使用3857。

尝试致电:

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