gpt4 book ai didi

javascript - 外部文件与内部对象的 GeoJSON 投影

转载 作者:行者123 更新时间:2023-12-03 08:54:09 27 4
gpt4 key购买 nike

我有这个外部 GeoJSON 文件:

{"type": "FeatureCollection", "features": [ {"type":"Feature", "id":382, "properties":{"name":"Foo","description":"Bar"}, "geometry":{"type":"MultiPolygon","coordinates":[[[[100.51731551305,14.018177133438],[100.517959243205,14.0188303173272],[100.517133122834,14.0194652831494],[100.516628867536,14.0198920624699],[100.51755154744,14.0206831634993],[100.521199351693,14.0200794287498],[100.518087989239,14.0167692686143],[100.517798310678,14.0169176022848],[100.51731551305,14.018177133438]]]]}} ] }

如果我像这样使用它,它的位置正确:

var vectorSource = new ol.source.Vector({
url: 'data.geojson',
format: new ol.format.GeoJSON(),
projection : 'EPSG:4326',
});

var vectorLayer = new ol.layer.Vector({
source: vectorSource
});

var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'sat'})
}),
vectorLayer
],
view: new ol.View({
center: ol.proj.transform([100.514,14.012], 'EPSG:4326', 'EPSG:3857'),
zoom: 11
})
});

但是,如果我尝试直接将 GeoJSON 作为对象插入,该图层将在位置 0, 0 处显示为一个小点:

var geojsonObject = {"type": "FeatureCollection", "features": [ {"type":"Feature", "id":382, "properties":{"name":"Foo","description":"Bar"}, "geometry":{"type":"MultiPolygon","coordinates":[[[[100.51731551305,14.018177133438],[100.517959243205,14.0188303173272],[100.517133122834,14.0194652831494],[100.516628867536,14.0198920624699],[100.51755154744,14.0206831634993],[100.521199351693,14.0200794287498],[100.518087989239,14.0167692686143],[100.517798310678,14.0169176022848],[100.51731551305,14.018177133438]]]]}} ] };

var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(geojsonObject),
projection : 'EPSG:4326'
});

var vectorLayer = new ol.layer.Vector({
source: vectorSource
});

var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'sat'})
}),
vectorLayer
],
view: new ol.View({
center: ol.proj.transform([100.514,14.012], 'EPSG:4326', 'EPSG:3857'),
zoom: 11
})
});

这里出了什么问题?

最佳答案

在第二个示例中,将投影选项添加到 readFeatures 调用中:

features: (new ol.format.GeoJSON()).readFeatures(geojsonObject, 
{dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857'}),

关于javascript - 外部文件与内部对象的 GeoJSON 投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32580796/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com