繁体   English   中英

OpenLayers GeoJSON 图层投影不起作用

[英]OpenLayers GeoJSON Layer projection not working

使用下面的代码,我无法让我的单个 GeoJSON 点特征出现在 map 上。 或者,如果它确实出现在 map 上的正确位置。 它出现在德国,而它应该出现在澳大利亚的塔斯马尼亚。 如果我根本不提供投影,它会出现在相同的位置。 因此,我设置投影的方式似乎存在一些问题。

我有一种感觉,我错误地使用了defaultDataProjection和/或featureProjection ,但我发现文档对于如何使用它们相当不清楚。

相同的投影适用于我的其他(非 GeoJSON)图层。

如何更正我的代码以使其正常工作?

var geojsonObject = {"type":"FeatureCollection","features":[{"geometry":{"type":"Point","coordinates":[503619.026899999939,5420925.347199999727]},"properties":{"label":{}},"type":"Feature"}]};

proj4.defs('layerProj', '+proj=utm +zone=55 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs ')

var vectorSource = new ol.source.Vector({
    features: (new ol.format.GeoJSON({
        defaultDataProjection: 'layerProj',
    }).readFeatures(geojsonObject, { featureProjection: 'layerProj' }))
})

var vectorLayer = new ol.layer.Vector({
  source: vectorSource,
  style: new ol.style.Style({image: new ol.style.Circle({fill: new ol.style.Fill({color: '#8888FF'}), radius: 10, points: 0})}),
});
    
map.addLayer(vectorLayer);

您的format:应该是features: OpenLayers 4 中 GeoJSON 选项中的defaultDataProjection也更改为更高版本中的dataProjection ,因此最好在readFeatures选项中指定dataProjection ,其中名称在所有版本中都相同。 featureProjection应始终设置为视图投影

        features: new ol.format.GeoJSON().readFeatures(geojsonObject, { 
          dataProjection: 'layerProj',
          featureProjection: map.getView().getProjection()
        })

如果您使用的是 OpenLayers 5 或更高版本,您还需要在 layerproj 定义之后注册layerproj

ol.proj.proj4.register(proj4);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM