Using Mapbox GL JS to simulate a radar blip.
Created
May 2, 2018 20:22
-
-
Save geografa/27b8a4972cd878c09544613fd6208ef8 to your computer and use it in GitHub Desktop.
Mapbox GL JS Marker Blip
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
<div id='map'></div> |
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
// DID YOU FORK THIS EXAMPLE? | |
// Enter your access token below | |
// and uncomment the line to keep your | |
// project online! | |
// Need a token? Create free account | |
// mapbox.com/signup | |
// *********************************************** | |
// mapboxgl.accessToken = 'YOUR-ACCESS-TOKEN-HERE'; | |
// *********************************************** | |
var map = new mapboxgl.Map({ | |
container: 'map', | |
style: 'mapbox://styles/mapbox/streets-v8', | |
center: [0, 0], | |
zoom: 2 | |
}); | |
var framesPerSecond = 15; | |
var initialOpacity = 1 | |
var opacity = initialOpacity; | |
var initialRadius = 6; | |
var radius = initialRadius; | |
var maxRadius = 18; | |
map.on('load', function () { | |
// Add a source and layer displaying a point which will be animated in a circle. | |
map.addSource('point', { | |
"type": "geojson", | |
"data": { | |
"type": "Point", | |
"coordinates": [ | |
0, 0 | |
] | |
} | |
}); | |
map.addLayer({ | |
"id": "point", | |
"source": "point", | |
"type": "circle", | |
"paint": { | |
"circle-radius": initialRadius, | |
"circle-radius-transition": {duration: 0}, | |
"circle-opacity-transition": {duration: 0}, | |
"circle-color": "#007cbf" | |
} | |
}); | |
map.addLayer({ | |
"id": "point1", | |
"source": "point", | |
"type": "circle", | |
"paint": { | |
"circle-radius": initialRadius, | |
"circle-color": "#007cbf" | |
} | |
}); | |
function animateMarker(timestamp) { | |
setTimeout(function(){ | |
requestAnimationFrame(animateMarker); | |
radius += (maxRadius - radius) / framesPerSecond; | |
opacity -= ( .9 / framesPerSecond ); | |
map.setPaintProperty('point', 'circle-radius', radius); | |
map.setPaintProperty('point', 'circle-opacity', opacity); | |
if (opacity <= 0) { | |
radius = initialRadius; | |
opacity = initialOpacity; | |
} | |
}, 1000 / framesPerSecond); | |
} | |
// Start the animation. | |
animateMarker(0); | |
}); |
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
<script src="https://www.mapbox.com/help/data/codepen-token.js"></script> |
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
body { | |
margin: 0; | |
padding: 0; | |
} | |
#map { | |
position: absolute; | |
top: 0; | |
bottom: 0; | |
width: 100%; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment