Files
cartodb/carto.js/examples/public/layers/layer-with-aggregation-leaflet.html
2020-06-15 10:58:47 +08:00

74 lines
2.3 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>Layer with aggregation | CARTO</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,600,700|Open+Sans:300,400,600" rel="stylesheet">
<!-- Include Leaflet -->
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<link href="https://unpkg.com/leaflet/dist/leaflet.css" rel="stylesheet">
<!-- Include CARTO.js -->
<script src="../../../dist/public/carto.js"></script>
<link href="../style.css" rel="stylesheet">
</head>
<body>
<div id="map">
</div>
<!-- Description -->
<aside class="toolbox">
<div class="box">
<header>
<h1>Server tile aggregation</h1>
<button class="github-logo js-source-link"></button>
</header>
<section>
<p class="description open-sans">This map has smart backend aggregation applied. See source code for details.</p>
</section>
<footer class="js-footer"></footer>
</div>
</aside>
<script>
const map = L.map('map').setView([30, 0], 3);
map.scrollWheelZoom.disable();
L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager_nolabels/{z}/{x}/{y}.png', {
maxZoom: 18
}).addTo(map);
const client = new carto.Client({
apiKey: 'default_public',
username: 'cartojs-test'
});
const source = new carto.source.Dataset('ne_10m_populated_places_simple');
const style = new carto.style.CartoCSS(`
#layer {
marker-width: 7;
marker-fill: ramp([population], (#ecda9a, #f7945d, #ee4d5a), jenks());
marker-line-color: #FFFFFF;
}
`);
// Aggregation option
const aggregation = new carto.layer.Aggregation({
threshold: 1,
resolution: 4,
placement: carto.layer.Aggregation.placement.SAMPLE,
columns: {
population: {
aggregateFunction: carto.layer.Aggregation.operation.SUM,
aggregatedColumn: 'pop_max'
}
}
});
const layer = new carto.layer.Layer(source, style, { aggregation });
client.addLayer(layer);
client.getLeafletLayer().addTo(map);
</script>
</body>
</html>