cartodb/lib/assets/javascripts/cdb/examples/leaflet_multilayer.html
zhongjin a96ef233c9 cdb
2020-06-15 12:07:54 +08:00

92 lines
2.7 KiB
HTML

<!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>
<link rel="stylesheet" href="https://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" />
</head>
<body>
<div id="map"></div>
<!-- include cartodb.js library -->
<script src="https://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script>
<script>
function main() {
// create leaflet map
var map = L.map('map', {
zoomControl: false,
center: [43, 0],
zoom: 3
})
// add a base layer
L.tileLayer('http://tile.stamen.com/toner/{z}/{x}/{y}.png', {
attribution: 'Stamen'
}).addTo(map);
// add cartodb layer with one sublayer
cartodb.createLayer(map, {
user_name: 'examples',
type: 'cartodb',
sublayers: [{
sql: 'select * from country_boundaries',
cartocss: '#layer { polygon-fill: #F00; polygon-opacity: 0.3; line-color: #F00; }'
}]
})
.addTo(map)
.done(function(layer) {
var population;
// wait a little bit and add the most populated places
setTimeout(function() {
population = layer.createSubLayer({
sql: 'select * from ne_10m_populated_p_2',
cartocss: '#layer { marker-fill: black; line-color: #333; marker-width: 3 }',
interactivity: 'name'
});
// print on the console the place name on hover
population.on('featureOver', function(e, pos, pixel, data) {
console.log(data.name);
});
population.setInteraction(true);
}, 3000);
// show population only in USA
setTimeout(function() {
population.setSQL("select * from ne_10m_populated_p_2 where adm0name = 'United States of America'")
}, 6000);
// bigger points in USA
setTimeout(function() {
population.setCartoCSS( '#layer { marker-fill: black; line-color: #333; marker-width: 10; marker-opacity: 0.6; }' );
}, 8000);
// remove the countries layer
setTimeout(function() {
layer.getSubLayer(0).remove();
}, 10000);
});
}
// you could use $(window).load(main);
window.onload = main;
</script>
</body>
</html>