code Snippetlanguage CDNpackage_2 NPM
content_copy Copyopen_in_new Open
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ - 100 , 40 ],
zoom: 3
});
map. on ( 'load' , async () => {
const res = await fetch ( 'https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_admin_1_states_provinces_shp.geojson' );
const data = await res. json ();
map. addSource ( 'states' , { type: 'geojson' , data });
const labels = map. getStyle ().layers. find ( l => l.type === 'symbol' && / label / . test (l.id));
map. addLayer ({ id: 'states-fill' , type: 'fill' , source: 'states' ,
paint: { 'fill-color' : '#6366f1' , 'fill-opacity' : 0.15 }
}, labels ? labels.id : undefined );
map. addLayer ({ id: 'states-border' , type: 'line' , source: 'states' ,
paint: { 'line-color' : '#6366f1' , 'line-width' : 1 } });
map. on ( 'click' , 'states-fill' , ( e ) => {
const f = e.features[ 0 ];
new maplibregl. Popup (). setLngLat (e.lngLat). setHTML ( `<b>${ f . properties . name || 'Unknown'}</b>` ). addTo (map);
});
map. on ( 'mouseenter' , 'states-fill' , () => { map. getCanvas ().style.cursor = 'pointer' ; });
map. on ( 'mouseleave' , 'states-fill' , () => { map. getCanvas ().style.cursor = '' ; });
}); content_copy Copyopen_in_new Open
<! DOCTYPE html >
< html >
< head >
< meta charset = "utf-8" />
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" />
< link rel = "stylesheet" href = "https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.css" />
< style >
body { margin : 0 ; padding : 0 ; }
#map { width : 100 % ; height : 100 vh ; }
</ style >
</ head >
< body >
< div id = "map" ></ div >
< script type = "module" >
import * as maplibregl from 'https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.mjs' ;
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ - 100 , 40 ],
zoom: 3
});
map. on ( 'load' , async () => {
const res = await fetch ( 'https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_admin_1_states_provinces_shp.geojson' );
const data = await res. json ();
map. addSource ( 'states' , { type: 'geojson' , data });
const labels = map. getStyle ().layers. find ( l => l.type === 'symbol' && / label / . test (l.id));
map. addLayer ({ id: 'states-fill' , type: 'fill' , source: 'states' ,
paint: { 'fill-color' : '#6366f1' , 'fill-opacity' : 0.15 }
}, labels ? labels.id : undefined );
map. addLayer ({ id: 'states-border' , type: 'line' , source: 'states' ,
paint: { 'line-color' : '#6366f1' , 'line-width' : 1 } });
map. on ( 'click' , 'states-fill' , ( e ) => {
const f = e.features[ 0 ];
new maplibregl. Popup (). setLngLat (e.lngLat). setHTML ( `<b>${ f . properties . name || 'Unknown'}</b>` ). addTo (map);
});
map. on ( 'mouseenter' , 'states-fill' , () => { map. getCanvas ().style.cursor = 'pointer' ; });
map. on ( 'mouseleave' , 'states-fill' , () => { map. getCanvas ().style.cursor = '' ; });
});
</ script >
</ body >
</ html > content_copy Copyopen_in_new Open
import maplibregl from 'maplibre-gl' ;
import 'maplibre-gl/dist/maplibre-gl.css' ;
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ - 100 , 40 ],
zoom: 3
});
map. on ( 'load' , async () => {
const res = await fetch ( 'https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_admin_1_states_provinces_shp.geojson' );
const data = await res. json ();
map. addSource ( 'states' , { type: 'geojson' , data });
const labels = map. getStyle ().layers. find ( l => l.type === 'symbol' && / label / . test (l.id));
map. addLayer ({ id: 'states-fill' , type: 'fill' , source: 'states' ,
paint: { 'fill-color' : '#6366f1' , 'fill-opacity' : 0.15 }
}, labels ? labels.id : undefined );
map. addLayer ({ id: 'states-border' , type: 'line' , source: 'states' ,
paint: { 'line-color' : '#6366f1' , 'line-width' : 1 } });
map. on ( 'click' , 'states-fill' , ( e ) => {
const f = e.features[ 0 ];
new maplibregl. Popup (). setLngLat (e.lngLat). setHTML ( `<b>${ f . properties . name || 'Unknown'}</b>` ). addTo (map);
});
map. on ( 'mouseenter' , 'states-fill' , () => { map. getCanvas ().style.cursor = 'pointer' ; });
map. on ( 'mouseleave' , 'states-fill' , () => { map. getCanvas ().style.cursor = '' ; });
});
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [-100, 40],
zoom: 3
});
map.on('load', async () => {
const res = await fetch('https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_admin_1_states_provinces_shp.geojson');
const data = await res.json();
map.addSource('states', { type: 'geojson', data });
const labels = map.getStyle().layers.find(l => l.type === 'symbol' && /label/.test(l.id));
map.addLayer({ id: 'states-fill', type: 'fill', source: 'states',
paint: { 'fill-color': '#6366f1', 'fill-opacity': 0.15 }
}, labels ? labels.id : undefined);
map.addLayer({ id: 'states-border', type: 'line', source: 'states',
paint: { 'line-color': '#6366f1', 'line-width': 1 } });
map.on('click', 'states-fill', (e) => {
const f = e.features[0];
new maplibregl.Popup().setLngLat(e.lngLat).setHTML(`<b>${f.properties.name || 'Unknown'}</b>`).addTo(map);
});
map.on('mouseenter', 'states-fill', () => { map.getCanvas().style.cursor = 'pointer'; });
map.on('mouseleave', 'states-fill', () => { map.getCanvas().style.cursor = ''; });
}); <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.css" />
<style>
body { margin: 0; padding: 0; }
#map { width: 100%; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<script type="module">
import * as maplibregl from 'https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.mjs';
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [-100, 40],
zoom: 3
});
map.on('load', async () => {
const res = await fetch('https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_admin_1_states_provinces_shp.geojson');
const data = await res.json();
map.addSource('states', { type: 'geojson', data });
const labels = map.getStyle().layers.find(l => l.type === 'symbol' && /label/.test(l.id));
map.addLayer({ id: 'states-fill', type: 'fill', source: 'states',
paint: { 'fill-color': '#6366f1', 'fill-opacity': 0.15 }
}, labels ? labels.id : undefined);
map.addLayer({ id: 'states-border', type: 'line', source: 'states',
paint: { 'line-color': '#6366f1', 'line-width': 1 } });
map.on('click', 'states-fill', (e) => {
const f = e.features[0];
new maplibregl.Popup().setLngLat(e.lngLat).setHTML(`<b>${f.properties.name || 'Unknown'}</b>`).addTo(map);
});
map.on('mouseenter', 'states-fill', () => { map.getCanvas().style.cursor = 'pointer'; });
map.on('mouseleave', 'states-fill', () => { map.getCanvas().style.cursor = ''; });
});
</script>
</body>
</html> import maplibregl from 'maplibre-gl';
import 'maplibre-gl/dist/maplibre-gl.css';
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [-100, 40],
zoom: 3
});
map.on('load', async () => {
const res = await fetch('https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_admin_1_states_provinces_shp.geojson');
const data = await res.json();
map.addSource('states', { type: 'geojson', data });
const labels = map.getStyle().layers.find(l => l.type === 'symbol' && /label/.test(l.id));
map.addLayer({ id: 'states-fill', type: 'fill', source: 'states',
paint: { 'fill-color': '#6366f1', 'fill-opacity': 0.15 }
}, labels ? labels.id : undefined);
map.addLayer({ id: 'states-border', type: 'line', source: 'states',
paint: { 'line-color': '#6366f1', 'line-width': 1 } });
map.on('click', 'states-fill', (e) => {
const f = e.features[0];
new maplibregl.Popup().setLngLat(e.lngLat).setHTML(`<b>${f.properties.name || 'Unknown'}</b>`).addTo(map);
});
map.on('mouseenter', 'states-fill', () => { map.getCanvas().style.cursor = 'pointer'; });
map.on('mouseleave', 'states-fill', () => { map.getCanvas().style.cursor = ''; });
});