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

GeoJSON projection of external file vs. internal object

我有这个外部 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'}),