View local GeoJSON

Download the example Average Wind Speeds GeoJSON. Instead of uploading the file to a server and reading it from there, the browser directly accesses the file locally, eliminating the need for any network transfer. If you wish to write to the file that has been opened, you can refer to the following example that utilizes the File System Access API: View local GeoJSON (experimental).

NPM module setup


npm install --save @maptiler/sdk
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
    container: 'map',
    style: MapStyle.STREETS, 
    center: [-8.3226655, 53.7654751],
    zoom: 8
});

function handleFileSelect(evt) {
    const file = evt.target.files[0]; // Read first selected file

    const reader = new FileReader();

    reader.onload = function (theFile) {
        // Parse as (geo)JSON
        const geoJSONcontent = JSON.parse(theFile.target.result);

        // Add as source to the map
        map.addSource('uploaded-source', {
            'type': 'geojson',
            'data': geoJSONcontent
        });

        map.addLayer({
            'id': 'uploaded-polygons',
            'type': 'fill',
            'source': 'uploaded-source',
            'paint': {
                'fill-color': '#888888',
                'fill-outline-color': 'red',
                'fill-opacity': 0.4
            },
            // filter for (multi)polygons; for also displaying linestrings
            // or points add more layers with different filters
            'filter': ['==', '$type', 'Polygon']
        });
    };

    // Read the GeoJSON as text
    reader.readAsText(file, 'UTF-8');
}

document
    .getElementById('file')
    .addEventListener('change', handleFileSelect, false);
View local GeoJSON (experimental)

View local GeoJSON (experimental)

Examples

View local GeoJSON with experimental File System Access API.

Show multiGeometry data from GeoJSON on the map

GeoJSON multiGeometry layer

Tutorials

Add a GeoJSON MultiGeometry overlay to your map application.

Add GeoJSON to AR maps

Add GeoJSON to AR maps

Examples

Display GeoJSON biking or hiking routes in AR maps.

Add a vector tile source

Add a vector tile source

Examples

Add and display custom vector tile sources on maps.

Was this helpful?