Last active
March 4, 2022 05:17
-
-
Save malwoodsantoro/4ee53544ba34480a2f9a7297cf2a73e6 to your computer and use it in GitHub Desktop.
Swap icon image on hover
This file contains hidden or 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>Swap icon-image on hover</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.49.0/mapbox-gl.js'></script> | |
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.49.0/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> | |
<div class='filter-ctrl'> | |
<input id='filter-input' type='text' name='filter' placeholder='Filter by name' /> | |
</div> | |
<script> | |
mapboxgl.accessToken = 'pk.eyJ1IjoibWFsLXdvb2QiLCJhIjoiY2oyZ2t2em50MDAyMzJ3cnltMDFhb2NzdiJ9.X-D4Wvo5E5QxeP7K_I3O8w'; | |
var places = { | |
"type": "FeatureCollection", | |
"features": [{ | |
"type": "Feature", | |
"properties": { | |
"number": "one" | |
}, | |
"geometry": { | |
"type": "Point", | |
"coordinates": [-77.038659, 38.931567] | |
} | |
}, { | |
"type": "Feature", | |
"properties": { | |
"number": "two" | |
}, | |
"geometry": { | |
"type": "Point", | |
"coordinates": [-77.003168, 38.894651] | |
} | |
}, { | |
"type": "Feature", | |
"properties": { | |
"number": "three" | |
}, | |
"geometry": { | |
"type": "Point", | |
"coordinates": [-77.090372, 38.881189] | |
} | |
}, { | |
"type": "Feature", | |
"properties": { | |
"number": "four" | |
}, | |
"geometry": { | |
"type": "Point", | |
"coordinates": [-77.052477, 38.943951] | |
} | |
}, { | |
"type": "Feature", | |
"properties": { | |
"number": "five" | |
}, | |
"geometry": { | |
"type": "Point", | |
"coordinates": [-77.031706, 38.914581] | |
} | |
}, { | |
"type": "Feature", | |
"properties": { | |
"number": "six" | |
}, | |
"geometry": { | |
"type": "Point", | |
"coordinates": [-77.020945, 38.878241] | |
} | |
}, { | |
"type": "Feature", | |
"properties": { | |
"number": "seven" | |
}, | |
"geometry": { | |
"type": "Point", | |
"coordinates": [-77.007481, 38.876516] | |
} | |
}] | |
}; | |
var map = new mapboxgl.Map({ | |
container: 'map', | |
style: 'mapbox://styles/mal-wood/cjm6pndwdd5dp2rn6alme8vmj', | |
center: [-77.04, 38.907], | |
zoom: 11.15 | |
}); | |
map.on('load', function() { | |
// Add a GeoJSON source containing place coordinates and information. | |
map.addSource('places', { | |
"type": "geojson", | |
"data": places | |
}); | |
map.addLayer({ | |
"id": "airplanes", | |
"type": "symbol", | |
"source": "places", | |
"layout": { | |
"icon-image": "airport-15", | |
"icon-allow-overlap": true, | |
"icon-size": 2 | |
}, | |
"paint": {} | |
}); | |
map.addLayer({ | |
"id": "airplanes-hover", | |
"type": "symbol", | |
"filter": ["==", "number", ""], | |
"source": "places", | |
"layout": { | |
"icon-image": "airport-16", | |
"icon-allow-overlap": true, | |
"icon-size": 2 | |
}, | |
"paint": {} | |
}); | |
map.on('mouseenter', 'airplanes', function(e){ | |
console.log(e.features) | |
map.setFilter('airplanes-hover', ['==', 'number', e.features[0].properties.number]) | |
}); | |
map.on('mouseleave', 'airplanes', function(e){ | |
map.setFilter('airplanes-hover', ['==', 'number', '']) | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment