MapSnippets logo MapSnippets logo

Load GeoJSON with MapTiler SDK

MapTiler SDK

Load a remote GeoJSON file into MapTiler SDK with map.addSource(), style it as a circle layer, and open a maptilersdk.Popup when a feature is clicked.

progress_activity Loading map...
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY';
const map = new maptilersdk.Map({
  container: 'map',
  style: maptilersdk.MapStyle.STREETS,
  center: [-98, 39],
  zoom: 3
});
map.on('load', () => {
  map.addSource('earthquakes', {
    type: 'geojson',
    data: 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.geojson'
  });
  map.addLayer({
    id: 'quake-points', type: 'circle', source: 'earthquakes',
    paint: {
      'circle-radius': 8,
      'circle-color': '#9C27B0',
      'circle-stroke-width': 2,
      'circle-stroke-color': '#e0e0e0'
    }
  });
  map.on('click', 'quake-points', (e) => {
    const props = e.features[0].properties;
    new maptilersdk.Popup().setLngLat(e.lngLat).setHTML(`<b>${props.title}</b>`).addTo(map);
  });
});

How to load a GeoJSON file with MapTiler SDK

Pass a URL to the data property of a GeoJSON source and the SDK fetches it automatically. Any valid GeoJSON URL or inline object works.

A circle layer renders each point feature. The click handler reads properties from the clicked feature and displays them in a popup.

For large datasets, enable clustering on the source or use a vector tile source for better performance.

Key APIs

API Description Docs
map.addSource() Add a GeoJSON source (URL or object) docs.maptiler.com/sdk-js/api/
map.addLayer() Style and render the data docs.maptiler.com/sdk-js/api/
map.on('click', layer) Click handler for a specific layer docs.maptiler.com/sdk-js/api/

FAQ

Can I load a local file?

Not directly from the filesystem. Use an <input type="file"> with FileReader to parse the file, then pass the parsed object as data.

How do I update the data dynamically?

Call map.getSource('earthquakes').setData(newGeoJSON) with a new GeoJSON object or URL.

Common mistakes

  • Loading data before the load event. The source cannot be added until the style is ready
  • Assuming GeoJSON features always have the same properties. Check for missing properties before accessing them
  • Not URL-encoding the data URL if it has query parameters, since MapTiler SDK passes it directly to fetch()

Build maps using MapTiler SDK with AI

Generate code like this load geojson with maptiler sdk example using the MapTiler SDK skill in your IDE.