Built with blockbuilder.org
Created
November 2, 2018 09:49
-
-
Save mastersigat/f85f2a942927e3f9fcee067026355bd9 to your computer and use it in GitHub Desktop.
#Mapbox GL + Turf.js
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.50.0/mapbox-gl.js'></script> | |
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.css' rel='stylesheet' /> | |
<script src='https://api.mapbox.com/mapbox.js/plugins/turf/v3.0.11/turf.min.js'></script> | |
<style> | |
#map { position:absolute; top:0; bottom:0; width:100%; } | |
</style> | |
</head> | |
<body> | |
<div id='map'></div> | |
<script> | |
// AccesToken | |
mapboxgl.accessToken = 'pk.eyJ1IjoibWFzdGVyc2lnYXQiLCJhIjoiY2loNG9mamxwMHp2dHgxbTBjY2hlb2RteiJ9.dDYKXX9907pbT6sTAJ4fvA'; | |
// Configuration de la carte | |
var map = new mapboxgl.Map({ | |
container: 'map', | |
style: 'mapbox://styles/mapbox/light-v9', | |
center: [-1.68, 48.12], // lat/long | |
zoom: 15, // zoom | |
pitch: 50, // Inclinaison | |
bearing: -10 // Rotation | |
}); | |
// Ajout de données OSM | |
map.on('load', function () { | |
map.addSource('Arrets', { | |
type: 'vector', | |
url: 'mapbox://ninanoun.7mtp5buo'}); | |
map.addLayer({ | |
"id": "Arrets", | |
"type": "symbol", | |
"source": "Arrets", | |
"source-layer": "topologie-des-points-darret-d-9ya955", | |
"layout": { "icon-image": "bus-15", | |
"icon-size": 1.5, 'icon-allow-overlap': true}, paint: { } | |
}); | |
map.addSource('orga', { | |
type: 'vector', | |
url: 'mapbox://ninanoun.3kota0ts'}); | |
map.addLayer({ | |
"id": "orga", | |
"type": "symbol", | |
"source": "orga", | |
"source-layer": "base-orga-var-1wgahn", | |
"layout": { "icon-image": "marker-15", | |
"icon-size": 1, 'icon-allow-overlap': true}, paint: { } | |
}); | |
}); | |
map.on('click', function(e) { | |
var features = map.queryRenderedFeatures(e.point, { | |
layers: ['Arrets', 'orga'] }); | |
if (!features.length) { | |
return; | |
} | |
var feature = features[0]; | |
var popup = new mapboxgl.Popup({ offset: [0, -15] }) | |
.setLngLat(feature.geometry.coordinates) | |
.setHTML('<h3>' + feature.properties.nom + '</h3><p>' + feature.properties.nom + '</p>') | |
.setLngLat(feature.geometry.coordinates) | |
.addTo(map); | |
map.getCanvas().style.cursor = (features.length) ? 'pointer' : ''; | |
}); | |
map.on('click', function(e){ | |
var libraryFeatures = map.queryRenderedFeatures(e.point, { layers: ['Arrets'] }); | |
if (!libraryFeatures.length) { | |
return; | |
} | |
var libraryFeature = libraryFeatures[0]; | |
var nearestHospital = turf.nearest(libraryFeature, orga); | |
if (nearestHospital != null) { | |
map.getSource('nearest-hospital').setData({ | |
'type': 'FeatureCollection', | |
'features': [ | |
nearestHospital | |
] | |
} | |
); | |
map.addLayer({ | |
id: 'nearest-hospital', | |
type: 'circle', | |
source: 'nearest-hospital', | |
paint: { | |
'circle-radius': 12, | |
'circle-color': '#486DE0' | |
} | |
}, 'orga'); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment