MapSnippets logo MapSnippets logo

Load GeoJSON in OpenLayers

OpenLayers

Parse GeoJSON in OpenLayers with ol.format.GeoJSON readFeatures() and render the features on an ol.layer.Vector styled per geometry type.

progress_activity Loading map...
var geojsonData = {
  type: 'FeatureCollection',
  features: [
    { type: 'Feature', properties: { name: 'Louvre Museum' }, geometry: { type: 'Point', coordinates: [2.3376, 48.8606] } },
    { type: 'Feature', properties: { name: 'Walking Path' }, geometry: { type: 'LineString', coordinates: [[2.3376, 48.8606], [2.3272, 48.8600], [2.3200, 48.8580]] } },
    { type: 'Feature', properties: { name: 'Park Area' }, geometry: { type: 'Polygon', coordinates: [[[2.3272, 48.8620], [2.3340, 48.8620], [2.3340, 48.8590], [2.3272, 48.8590], [2.3272, 48.8620]]] } }
  ]
};
var vectorSource = new ol.source.Vector({
  features: new ol.format.GeoJSON().readFeatures(geojsonData, { featureProjection: 'EPSG:3857' })
});
var vectorLayer = new ol.layer.Vector({
  source: vectorSource,
  style: function(feature) {
    var type = feature.getGeometry().getType();
    if (type === 'Point') return new ol.style.Style({ image: new ol.style.Circle({ radius: 7, fill: new ol.style.Fill({ color: '#e63946' }), stroke: new ol.style.Stroke({ color: '#fff', width: 2 }) }) });
    if (type === 'LineString') return new ol.style.Style({ stroke: new ol.style.Stroke({ color: '#457b9d', width: 3 }) });
    return new ol.style.Style({ fill: new ol.style.Fill({ color: 'rgba(42, 157, 143, 0.3)' }), stroke: new ol.style.Stroke({ color: '#2a9d8f', width: 2 }) });
  }
});
var map = new ol.Map({
  target: 'map',
  layers: [
    new ol.layer.Tile({ source: new ol.source.XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', attributions: '&copy; <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
    vectorLayer
  ],
  view: new ol.View({ center: ol.proj.fromLonLat([2.328, 48.860]), zoom: 15 })
});

How to load and display GeoJSON in OpenLayers

ol.format.GeoJSON parses GeoJSON data into OpenLayers features. The readFeatures() method accepts a GeoJSON object or string, and featureProjection: 'EPSG:3857' converts coordinates from WGS84 to the map’s projection.

The style function checks each feature’s geometry type and applies appropriate styling: circle markers for points, stroked lines for LineStrings, and filled polygons for Polygon features. This handles mixed-geometry GeoJSON collections.

Key APIs

API Description Docs
ol.format.GeoJSON Parse and write GeoJSON data openlayers.org
readFeatures() Convert GeoJSON to OL features openlayers.org
feature.getGeometry().getType() Get geometry type string openlayers.org

FAQ

How do I load GeoJSON from a URL?

Use new ol.source.Vector({ url: 'https://example.com/data.geojson', format: new ol.format.GeoJSON() }). The source handles fetching and parsing automatically.

Can I export drawn features back to GeoJSON?

Yes. Use new ol.format.GeoJSON().writeFeatures(source.getFeatures(), { featureProjection: 'EPSG:3857' }) to get a GeoJSON string.

Common mistakes

  • Missing featureProjection: 'EPSG:3857', which makes features appear near null island (0, 0) instead of their actual locations
  • Using readFeature() (singular) for a FeatureCollection instead of readFeatures() (plural)
  • Not handling mixed geometry types in the style function, resulting in invisible features

Build maps using OpenLayers with AI

Generate code like this load geojson in openlayers example using the OpenLayers skill in your IDE.