如何在 openlayers3 中指定 GeoJSON 的投影?
How to specify the projection for GeoJSON in openlayers3?
我的目的是加载 GeoJSON 数据并将其显示在地图上。 GeoJSON 中指定的特征坐标是正常的lon/lat。出于某种原因,openlayers 使用地图使用的投影渲染它们,而不转换它们。
// Underlying sat layer.
var world = new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'sat'})
});
// GeoJSON data.
var geojsonObject = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': {
'coordinates': [ 50.07539747, 19.76809501 ],
'type': 'Point'
},
}
]
};
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
// Map.
map = new ol.Map({
target: 'map',
layers: [world, vectorLayer],
view: new ol.View({
center: ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857'),
zoom: 2
})
});
该点正在地图中间呈现。通过放置多个点,我确定它们实际上相对于彼此移动了一点,这让我相信出于某种原因,地图为它们使用了不同的坐标系。
我尝试了什么:
在 GeoJSON 中设置 crs
,提供 defaultDataProjection
选项来格式化。我使用 openlayers v3.8.2,我在网上找到的所有解决方案都非常过时(据我所知,API 曾经更好,也许我应该切换到旧版本)。
只需使用 featureProjection
即可阅读以下功能:
var vectorSource = new ol.source.Vector({
features: new ol.format.GeoJSON().readFeatures(geojsonObject,{
featureProjection: 'EPSG:3857'
})
});
更新:
当从 url 读取特征更容易时,OL 会在内部为您进行转换:
var geojson_layer = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'file.geojson',
format: new ol.format.GeoJSON()
})
});
我的目的是加载 GeoJSON 数据并将其显示在地图上。 GeoJSON 中指定的特征坐标是正常的lon/lat。出于某种原因,openlayers 使用地图使用的投影渲染它们,而不转换它们。
// Underlying sat layer.
var world = new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'sat'})
});
// GeoJSON data.
var geojsonObject = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': {
'coordinates': [ 50.07539747, 19.76809501 ],
'type': 'Point'
},
}
]
};
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
// Map.
map = new ol.Map({
target: 'map',
layers: [world, vectorLayer],
view: new ol.View({
center: ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857'),
zoom: 2
})
});
该点正在地图中间呈现。通过放置多个点,我确定它们实际上相对于彼此移动了一点,这让我相信出于某种原因,地图为它们使用了不同的坐标系。
我尝试了什么:
在 GeoJSON 中设置 crs
,提供 defaultDataProjection
选项来格式化。我使用 openlayers v3.8.2,我在网上找到的所有解决方案都非常过时(据我所知,API 曾经更好,也许我应该切换到旧版本)。
只需使用 featureProjection
即可阅读以下功能:
var vectorSource = new ol.source.Vector({
features: new ol.format.GeoJSON().readFeatures(geojsonObject,{
featureProjection: 'EPSG:3857'
})
});
更新: 当从 url 读取特征更容易时,OL 会在内部为您进行转换:
var geojson_layer = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'file.geojson',
format: new ol.format.GeoJSON()
})
});