Built with blockbuilder.org
Created
February 11, 2018 11:53
-
-
Save anonymous/32ab9ab043074bd5cb38fb153237ef2e to your computer and use it in GitHub Desktop.
#MapboxGL / Interactivité avec les données (hover/click)
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset='utf-8' /> | |
<title>MapboxGL</title> | |
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.0/mapbox-gl.js'></script> | |
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.0/mapbox-gl.css' rel='stylesheet' /> | |
<style> | |
#map { position:absolute; top:0; bottom:0; width:100%; } | |
</style> | |
</head> | |
<body> | |
<div id='map'></div> | |
<script> | |
// AccesToken | |
mapboxgl.accessToken = 'pk.eyJ1IjoibmluYW5vdW4iLCJhIjoiY2pjdHBoZGlzMnV4dDJxcGc5azJkbWRiYSJ9.o4dZRrdHcgVEKCveOXG1YQ'; | |
// Configuration de la carte | |
var map = new mapboxgl.Map({ | |
container: 'map', | |
style: 'mapbox://styles/mapbox/dark-v9', | |
center: [-1.68, 48.12], // lat/long | |
zoom: 17, // zoom | |
pitch: 50, // Inclinaison | |
bearing: -10 // Rotation | |
}); | |
map.on('load', function () { | |
// Ajout routes OSM | |
map.addSource('mapbox-streets-v7', { | |
type: 'vector', | |
url: 'mapbox://mapbox.mapbox-streets-v7'}); | |
map.addLayer({ | |
"id": "routes", | |
"type": "line", | |
"source": "mapbox-streets-v7", | |
"layout": {'visibility': 'visible'}, | |
"source-layer": "road", | |
"paint": {"line-color": "#FF7F50", "line-width": 1} | |
}); | |
// Ajout arrets de bus | |
map.addSource('Arrets', { | |
type: 'vector', | |
url: 'mapbox://ninanoun.58widelk'}); | |
map.addLayer({ | |
'id': 'Arrets', | |
'type': 'circle', | |
'source': 'Arrets', | |
'source-layer': 'Bus-5ypx1k', | |
'layout': {'visibility': 'visible'}, | |
'paint': {'circle-radius': 7, 'circle-color': '#f5f60d'} | |
}); | |
// Ajout couche équipements | |
map.addSource('Equipements', { | |
type: 'vector', | |
url: 'mapbox://ninanoun.4xcn5ude'}); | |
map.addLayer({ | |
'id': 'Equipements', | |
'type': 'circle', | |
'source': 'Equipements', | |
'source-layer': 'base-orga-var-6k0zky', | |
'layout': {'visibility': 'visible'}, | |
'paint': {'circle-radius': 6, 'circle-color': '#3399ff'} | |
}); | |
//Limites de proprietes | |
map.addSource('Proprietes', { | |
type: 'vector', | |
url: 'mapbox://ninanoun.a4kdgiot' | |
}); | |
map.addLayer({ | |
'id': 'Proprietes', | |
'type': 'line', | |
'source': 'Proprietes', | |
'source-layer': 'limites_proprietes', | |
'layout': {'visibility': 'visible', | |
'line-join': 'round','line-cap': 'round'}, | |
'paint': {'line-color': '#FFFFFF', 'line-width': 1.5} | |
}); | |
// Ajout batiments 3D | |
map.addLayer({ | |
'id': 'Batimenst_3D', | |
'source': 'composite', | |
'source-layer': 'building', | |
'filter': ['==', 'extrude', 'true'], | |
'type': 'fill-extrusion', | |
'minzoom': 15, | |
'paint': {'fill-extrusion-color': '#FFFFFF', 'fill-extrusion-height': | |
{'type': 'identity','property': 'height'}, | |
'fill-extrusion-base': {'type': 'identity','property': 'min_height'}, | |
'fill-extrusion-opacity': 0.9 | |
} | |
}); | |
}); | |
// Interactivité Hover | |
//Interactivité HOVER | |
//Interactivité HOVER | |
var popup = new mapboxgl.Popup({ | |
closeButton: false, | |
closeOnClick: false }); | |
map.on('mousemove', function(e) { | |
var features = map.queryRenderedFeatures(e.point, { layers: ['Equipements'] }); | |
// 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(feature.properties.organom) | |
.addTo(map); | |
}); | |
//Interactivité CLICK | |
map.on('click', function (e) { | |
var features = map.queryRenderedFeatures(e.point, { layers: ['Arrets'] }); | |
if (!features.length) { | |
return; | |
} | |
var feature = features[0]; | |
var popup = new mapboxgl.Popup({ offset: [0, -15] }) | |
.setLngLat(feature.geometry.coordinates) | |
.setHTML('<h2>' + feature.properties.nom + '</h2><h3>' | |
+"Mobilier : " + feature.properties.mobilier + '</h3><p>' | |
+"Accessibilité PMR : " + feature.properties.estaccessiblepmr + '</p>' ) | |
.addTo(map); | |
}); | |
map.on('mousemove', function (e) { | |
var features = map.queryRenderedFeatures(e.point, { layers: ['Arrets'] }); | |
map.getCanvas().style.cursor = (features.length) ? 'pointer' : ''; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment