Skip to content

Instantly share code, notes, and snippets.

@tristen
Created February 24, 2017 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tristen/863dfc36e3e7b38059f0bc20ef54e9fa to your computer and use it in GitHub Desktop.
Save tristen/863dfc36e3e7b38059f0bc20ef54e9fa to your computer and use it in GitHub Desktop.
Marker hover style
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.32.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.32.1/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoidHJpc3RlbiIsImEiOiJiUzBYOEJzIn0.VyXs9qNWgTfABLzSI3YcrQ';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9',
center: [-97, 39],
zoom: 4
});
map.on('load', function () {
map.addSource('marker', {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: [{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [-97, 39]
}
}]
}
});
map.addSource('marker-hover', {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: []
}
});
map.addLayer({
id: 'marker',
source: 'marker',
type: 'circle',
paint: {
'circle-color': '#3ca0d3',
'circle-stroke-width': 4,
'circle-stroke-color': '#fff',
'circle-radius': 20
}
});
map.addLayer({
id: 'marker-hover',
source: 'marker-hover',
type: 'circle',
paint: {
'circle-color': '#F86767',
'circle-stroke-width': 4,
'circle-stroke-color': '#fff',
'circle-radius': 20
}
});
map.on('mousemove', function(e) {
var features = map.queryRenderedFeatures(e.point, { layers: ['marker'] });
map.getCanvas().style.cursor = (features.length) ? 'pointer' : '';
var geojson = {
type: 'FeatureCollection',
features: []
};
if (!features.length) {
map.getSource('marker-hover').setData(geojson);
return;
}
geojson.features.push(features[0]);
map.getSource('marker-hover').setData(geojson);
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment