Last active
April 12, 2019 09:28
-
-
Save oriolbx/e468b48087630a651de47bea0b86cdba to your computer and use it in GitHub Desktop.
CARTOVL: Hide/show layer depending on zoom level
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> | |
<title>Add multiple layers | CARTO</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta charset="UTF-8"> | |
<script src="https://libs.cartocdn.com/carto-vl/v1.1.1/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" /> | |
<link href="https://carto.com/developers/carto-vl/v1.1.1/examples/maps/style.css" rel="stylesheet"> | |
</head> | |
<body> | |
<div id="map"></div> | |
<aside class="toolbox"> | |
<div class="box"> | |
<header> | |
<h1>Multiple layers</h1> | |
</header> | |
<section> | |
<p class="description open-sans">Add multiple layers to your map</p> | |
</section> | |
<footer class="js-footer"></footer> | |
</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: [0, 43], | |
zoom: 4, | |
scrollZoom: true | |
}); | |
const zoomLimit = 10; | |
const zoomOverlap = 1; | |
const nav = new mapboxgl.NavigationControl({ | |
showCompass: false | |
}); | |
map.addControl(nav, 'top-left'); | |
// Define user | |
carto.setDefaultAuth({ | |
username: 'cartovl', | |
apiKey: 'default_public' | |
}); | |
// Define layers | |
const citiesSource = new carto.source.SQL(` | |
SELECT * | |
FROM populated_places | |
WHERE adm0name IN ('Spain','France','Italy','Switzerland','Portugal') | |
`); | |
const citiesViz = new carto.Viz(` | |
width: 200 | |
`); | |
const citiesLayer = new carto.Layer('citiesLayer', citiesSource, citiesViz); | |
const countriesSource = new carto.source.SQL(` | |
SELECT * | |
FROM ne_adm0_europe | |
WHERE admin IN ('Spain','France','Italy','Switzerland','Portugal') | |
`); | |
const countriesViz = new carto.Viz(); | |
const countriesLayer = new carto.Layer('countriesLayer', countriesSource, countriesViz); | |
citiesLayer.addTo(map, 'watername_ocean'); | |
countriesLayer.addTo(map, 'citiesLayer'); | |
countriesLayer.on('loaded', hideLoader); | |
function hideLoader() { | |
document.getElementById('loader').style.opacity = '0'; | |
} | |
///// Set map events //////// | |
map.on('load', () => { | |
showHideLayers() | |
}); | |
map.on('moveend', () => { | |
showHideLayers() | |
if (citiesLayer.visible === true){ | |
console.log('Do somthing') | |
} | |
}); | |
function showHideLayers(){ | |
/** | |
* Hide or show a CARTO layer depending on the zoom level | |
*/ | |
const zoom = map.getZoom(); | |
if (zoom >= zoomLimit) { | |
citiesLayer.show(); | |
if (zoom > zoomLimit + zoomOverlap) { | |
countriesLayer.hide(); | |
} | |
} else { | |
countriesLayer.show(); | |
if (zoom < zoomLimit - zoomOverlap) { | |
citiesLayer.hide(); | |
} | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment