Built with blockbuilder.org
Last active
February 3, 2021 12:14
-
-
Save mastersigat/661db2568719947ca84d7fdae25c0bc5 to your computer and use it in GitHub Desktop.
#Mapbox GL / Carte basée sur Maplibre
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /> | |
<script src="https://cdn.maptiler.com/maplibre-gl-js/v1.13.0-rc.4/mapbox-gl.js"></script> | |
<link href="https://cdn.maptiler.com/maplibre-gl-js/v1.13.0-rc.4/mapbox-gl.css" rel="stylesheet" /> | |
<style> | |
#map {position: absolute; top: 0; right: 0; bottom: 0; left: 0;} | |
.Mypopup .mapboxgl-popup-content { | |
background-color: black; | |
color : white; | |
opacity : 0.7; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="map"></div> | |
<script> | |
//Appel de la carte | |
var map = new mapboxgl.Map({ | |
container: 'map', | |
style: 'https://geoserveis.icgc.cat/contextmaps/positron.json', | |
zoom: 12.5, | |
center: [-1.67, 48.106], | |
pitch: 40 | |
}); | |
// Ajout des données | |
map.on('load', function () { | |
// Ajout lignes de metros | |
map.addSource('lignes', { | |
type: 'geojson', | |
data: 'https://raw.githubusercontent.com/mastersigat/data/main/metro-du-reseau-star-traces-de-laxe-des-lignes.geojson' | |
}); | |
map.addLayer({ | |
'id': 'region', | |
'type': 'line', | |
'source': 'lignes', | |
'paint': {'line-opacity': 0.7, 'line-width': 3.3, | |
'line-color': '#0074D9'} | |
}); | |
// Ajout stations de metros | |
map.addSource('Stations', { | |
type: 'geojson', | |
data: 'https://raw.githubusercontent.com/mastersigat/data/main/metro-du-reseau-star-localisation-des-stations.geojson' | |
}); | |
map.addLayer({ | |
'id': 'Stations', | |
'type': 'circle', | |
'source': 'Stations', | |
'paint': {'circle-stroke-color': 'white' , | |
'circle-stroke-width': 3 , | |
'circle-radius' : 5, | |
'circle-color': 'blue' | |
}} | |
); | |
}); | |
//Interactivité HOVER | |
var popup = new mapboxgl.Popup({ | |
className: "Mypopup", | |
closeButton: false, | |
closeOnClick: false }); | |
map.on('mousemove', function(e) { | |
var features = map.queryRenderedFeatures(e.point, { layers: ['Stations'] }); | |
// Change the cursor style as a UI indicator. | |
map.getCanvas().style.cursor = (features.length) ? 'pointer' : ''; | |
if (!features.length) { | |
popup.remove(); | |
return; } | |
var feature = features[0]; | |
popup.setLngLat(feature.geometry.coordinates) | |
.setHTML('<b>'+ feature.properties.nom + '</b>') | |
.addTo(map); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment