Skip to content

Instantly share code, notes, and snippets.

@elenatorro
Created April 2, 2019 14:57
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 elenatorro/39851c455aaadfe745cec864b6cdd94f to your computer and use it in GitHub Desktop.
Save elenatorro/39851c455aaadfe745cec864b6cdd94f to your computer and use it in GitHub Desktop.
Radius Filter Example
<!DOCTYPE html>
<html>
<head>
<title>Add layer | CARTO</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
<!-- Include CARTO VL JS -->
<script src="https://libs.cartocdn.com/carto-vl/v1.2.4/carto-vl.min.js"></script>
<script src="https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.0/mapbox-gl.js"></script>
<link href="https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.0/mapbox-gl.css" rel="stylesheet" />
<style>
html,
body {
margin: 0;
}
#map {
position: absolute;
width: 100%;
height: 100%;
z-index: 1;
}
aside.toolbox {
background-color: rgba(255, 255, 255, 0.9);
border-radius: 5px;
display: block;
font-family: 'Roboto';
height: auto;
padding: 0 24px 24px 24px;
position: absolute;
right: 12px;
top: 12px;
width: 250px;
font-size: 12px;
z-index: 2;
}
</style>
</head>
<body>
<div id="map"></div>
<aside class="toolbox">
<div class="box">
<header>
<h1>Radius</h1>
</header>
<section>
<input class="white-thumb" type="range" id="js-radius-range" min="1" max="90" step="0.1">
<p id="js-radius"></p>
</section>
</div>
</aside>
<div id="loader">
<div class="CDB-LoaderIcon CDB-LoaderIcon--big">
<svg class="CDB-LoaderIcon-spinner" viewBox="0 0 50 50">
<circle class="CDB-LoaderIcon-path" cx="25" cy="25" r="20" fill="none"></circle>
</svg>
</div>
</div>
<script>
const map = new mapboxgl.Map({
container: 'map',
style: carto.basemaps.voyager,
center: [-39.913015089260284, 12.52009632002347],
zoom: 1.2,
scrollZoom: false
});
const nav = new mapboxgl.NavigationControl({
showCompass: false
});
map.addControl(nav, 'top-left');
// Define user
carto.setDefaultAuth({
username: 'cartovl',
apiKey: 'default_public'
});
const source = new carto.source.Dataset('populated_places');
const viz = new carto.Viz(`
@latitude: 12.52009632002347
@longitude: -39.913015089260284
@radius: 20
color: ramp(linear($latitude), sunset)
filter: lt(sqrt(add(abs(pow(sub($latitude, @latitude), 2)), abs(pow(sub($longitude, @longitude), 2)))), @radius)
`);
const layer = new carto.Layer('layer', source, viz);
layer.addTo(map, 'watername_ocean');
layer.on('loaded', hideLoader);
const $radiusRange = document.getElementById('js-radius-range');
const $radiusText = document.getElementById('js-radius');
$radiusRange.addEventListener('input', () => {
const radius = parseInt($radiusRange.value, 10);
viz.variables.radius = $radiusText.innerHTML = radius;
});
function hideLoader() {
document.getElementById('loader').style.opacity = '0';
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment