GeoJSONFormat
Quick answer
GeoJSONFormat is ol.format.GeoJSON, a reader and writer converting between GeoJSON text and OpenLayers features. Set dataProjection to the source projection, EPSG:4326 by default, and featureProjection to the map projection so coordinates transform on read and write. Use readFeatures, writeFeatures or geometryName to control parsing and serialization.
new ol.format.GeoJSON( options )
Key points
- readFeatures always returns an array, even for a single Feature input; use readFeature for one.
- writeFeatures returns a JSON string, while writeFeaturesObject returns a manipulable object.
- The feature id is written to the top-level GeoJSON id field, properties keep business attributes.
Example
var format = new ol.format.GeoJSON({ featureProjection: 'EPSG:3857' });
var features = format.readFeatures({
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: { name: '监测点 A', level: 3 },
geometry: { type: 'Point', coordinates: [116.391263, 39.907359] }
}]
});
console.log(features[0].getGeometry().getCoordinates()); // 米制坐标
var text = format.writeFeatures(features, {
featureProjection: 'EPSG:3857',
dataProjection: 'EPSG:4326'
});
console.log(text.slice(0, 60));
Notes
- Omitting featureProjection leaves coordinates unchanged, so EPSG:4326 data lands millions of meters off in a 3857 view.
- Relying on default projections when writing can emit wrong coordinates after the map view changes.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). GeoJSONFormat. https://www.jingweizhi.com/en/openlayers/api/geojson-format.html (accessed 2026-11-03).