Skip to content

Instantly share code, notes, and snippets.

@rochoa
Last active March 16, 2017 12:59
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 rochoa/779532bf9e5360645332 to your computer and use it in GitHub Desktop.
Save rochoa/779532bf9e5360645332 to your computer and use it in GitHub Desktop.
[CARTO] Filter labels for different zoom levels
<!DOCTYPE html>
<html>
<head>
<title>Leaflet multilayer example | CartoDB.js</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />
<style>
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
</style>
<style>
/* This will apply to all markers */
#ne_10m_populated_places_simple_4{
marker-fill-opacity: 1;
marker-width: 0;
marker-line-width: 0;
marker-fill: #000000;
marker-allow-overlap: true;
}
#ne_10m_populated_places_simple_4::labels {
text-name: "";
text-face-name: "DejaVu Sans Book";
text-size: 10;
text-label-position-tolerance: 0;
text-fill: #000;
text-halo-fill: #FFF;
text-halo-radius: 1;
text-dy: -4;
text-allow-overlap: true;
text-placement: point;
text-placement-type: dummy;
}
/* This will ONLY apply if rank_max>12 condition is true */
[rank_max>12] {
#ne_10m_populated_places_simple_4{
marker-width: 10;
marker-fill: #FF6600;
}
#ne_10m_populated_places_simple_4::labels {
text-name: [name];
text-size: 14;
text-dy: -8;
}
}
/* This will apply for zoom higher or equal to 4 but ONLY when rank_max is 11 or 12 */
[zoom>=4][rank_max>10][rank_max<=12]{
#ne_10m_populated_places_simple_4{
marker-width: 6;
marker-fill: #FFA300;
}
#ne_10m_populated_places_simple_4::labels {
text-name: [name];
text-size: 12;
text-dy: -6;
}
}
/* This will apply for zoom higher or equal to 8 but ONLY for rank_max bellow 11 */
[zoom>=8][rank_max>4][rank_max<=10] {
#ne_10m_populated_places_simple_4{
marker-width: 4;
marker-fill: #229A00;
}
#ne_10m_populated_places_simple_4::labels {
text-name: [name];
text-size: 10;
text-dy: -4;
}
}
</style>
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/themes/css/cartodb.css" />
</head>
<body>
<div id="map"></div>
<!-- include cartodb.js library -->
<script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.js"></script>
<script>
function main() {
// create leaflet map
var map = L.map('map', {
zoomControl: false,
center: [43, 0],
zoom: 3
})
var style = [
'#ne_10m_populated_places_simple_4{',
' marker-fill-opacity: 1;',
' marker-width: 0;',
' marker-line-width: 0;',
' marker-fill: #000000;',
' marker-allow-overlap: true;',
'}',
'#ne_10m_populated_places_simple_4::labels {',
' text-name: "";',
' text-face-name: "DejaVu Sans Book";',
' text-size: 10;',
' text-label-position-tolerance: 0;',
' text-fill: #000;',
' text-halo-fill: #FFF;',
' text-halo-radius: 1;',
' text-dy: -4;',
' text-allow-overlap: true;',
' text-placement: point;',
' text-placement-type: dummy;',
'}',
'',
'[rank_max>12] {',
' #ne_10m_populated_places_simple_4{',
' marker-width: 10;',
' marker-fill: #FF6600;',
' }',
' #ne_10m_populated_places_simple_4::labels {',
' text-name: [name];',
' text-size: 14;',
' text-dy: -8;',
' }',
'}',
'',
'[zoom>=4][rank_max>10][rank_max<=12]{',
' #ne_10m_populated_places_simple_4{',
' marker-width: 6;',
' marker-fill: #FFA300;',
' }',
' #ne_10m_populated_places_simple_4::labels {',
' text-name: [name];',
' text-size: 12;',
' text-dy: -6;',
' }',
'}',
'',
'[zoom>=8][rank_max>4][rank_max<=10] {',
' #ne_10m_populated_places_simple_4{',
' marker-width: 4;',
' marker-fill: #229A00;',
' }',
' #ne_10m_populated_places_simple_4::labels {',
' text-name: [name];',
' text-size: 10;',
' text-dy: -4;',
' }',
'}'
].join('\n');
// add a base layer
L.tileLayer('http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png',{
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CartoDB</a>'
}).addTo(map);
// add cartodb layer with one sublayer
cartodb.createLayer(map, {
user_name: 'rochoa',
type: 'cartodb',
sublayers: [{
sql: "SELECT * FROM ne_populated_places",
cartocss: style,
cartocss_version:"2.3.0",
}]
})
.addTo(map)
.done(function(layer) {
});
}
// you could use $(window).load(main);
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment