Multiple Geometries from One Source
A single GeoJSON source can hold mixed geometry types. Add one layer per geometry type and filter on $type so polygons, lines, and points each render with their own paint.
import { MaptoolkitLogoControl } from 'https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-gl-logo/+esm';
const map = new maplibregl.Map({ container: 'map', attributionControl: { customAttribution: '' }, style: 'https://styles.maptoolkit.org/summer.json', center: [9, 49], zoom: 4.3 });
const fc = { type: 'FeatureCollection', features: [
{ type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[[11.35, 48.25], [11.8, 48.25], [11.8, 48.0], [11.35, 48.0], [11.35, 48.25]]] } }, // Munich
{ type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[[2.2, 48.95], [2.5, 48.95], [2.5, 48.78], [2.2, 48.78], [2.2, 48.95]]] } }, // Paris
{ type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: [[2.3522, 48.8566], [13.405, 52.52]] } }, // Paris - Berlin
{ type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: [[4.9041, 52.3676], [8.5417, 47.3769]] } }, // Amsterdam - Zurich
{ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [8.5417, 47.3769] } }, // Zurich
{ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [14.4378, 50.0755] } }, // Prague
{ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [9.19, 45.4642] } }, // Milan
{ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [16.3738, 48.2082] } } // Vienna
] };
map.on('load', function () {
map.addSource('mix', { type: 'geojson', data: fc });
map.addLayer({ id: 'mix-poly', type: 'fill', source: 'mix', filter: ['==', '$type', 'Polygon'], paint: { 'fill-color': '#1a73e8', 'fill-opacity': 0.3 } });
map.addLayer({ id: 'mix-poly-outline', type: 'line', source: 'mix', filter: ['==', '$type', 'Polygon'], paint: { 'line-color': '#1a73e8', 'line-width': 1.5 } });
map.addLayer({ id: 'mix-line', type: 'line', source: 'mix', filter: ['==', '$type', 'LineString'], paint: { 'line-color': '#e8551a', 'line-width': 3 } });
map.addLayer({ id: 'mix-point', type: 'circle', source: 'mix', filter: ['==', '$type', 'Point'], paint: { 'circle-radius': 7, 'circle-color': '#0a8754', 'circle-stroke-width': 2, 'circle-stroke-color': '#fff' } });
});
map.addControl(new MaptoolkitLogoControl());<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.css" />
<style>
html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
#map { width: 100%; height: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script type="module">
import { MaptoolkitLogoControl } from 'https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-gl-logo/+esm';
const map = new maplibregl.Map({ container: 'map', attributionControl: { customAttribution: '' }, style: 'https://styles.maptoolkit.org/summer.json', center: [9, 49], zoom: 4.3 });
const fc = { type: 'FeatureCollection', features: [
{ type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[[11.35, 48.25], [11.8, 48.25], [11.8, 48.0], [11.35, 48.0], [11.35, 48.25]]] } }, // Munich
{ type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[[2.2, 48.95], [2.5, 48.95], [2.5, 48.78], [2.2, 48.78], [2.2, 48.95]]] } }, // Paris
{ type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: [[2.3522, 48.8566], [13.405, 52.52]] } }, // Paris - Berlin
{ type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: [[4.9041, 52.3676], [8.5417, 47.3769]] } }, // Amsterdam - Zurich
{ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [8.5417, 47.3769] } }, // Zurich
{ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [14.4378, 50.0755] } }, // Prague
{ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [9.19, 45.4642] } }, // Milan
{ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [16.3738, 48.2082] } } // Vienna
] };
map.on('load', function () {
map.addSource('mix', { type: 'geojson', data: fc });
map.addLayer({ id: 'mix-poly', type: 'fill', source: 'mix', filter: ['==', '$type', 'Polygon'], paint: { 'fill-color': '#1a73e8', 'fill-opacity': 0.3 } });
map.addLayer({ id: 'mix-poly-outline', type: 'line', source: 'mix', filter: ['==', '$type', 'Polygon'], paint: { 'line-color': '#1a73e8', 'line-width': 1.5 } });
map.addLayer({ id: 'mix-line', type: 'line', source: 'mix', filter: ['==', '$type', 'LineString'], paint: { 'line-color': '#e8551a', 'line-width': 3 } });
map.addLayer({ id: 'mix-point', type: 'circle', source: 'mix', filter: ['==', '$type', 'Point'], paint: { 'circle-radius': 7, 'circle-color': '#0a8754', 'circle-stroke-width': 2, 'circle-stroke-color': '#fff' } });
});
map.addControl(new MaptoolkitLogoControl());
</script>
</body>
</html>