Skip to content

Instantly share code, notes, and snippets.

@oriolbx
Created July 2, 2018 08:46
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 oriolbx/b621783152f2dbca17a9d6f4ff325b94 to your computer and use it in GitHub Desktop.
Save oriolbx/b621783152f2dbca17a9d6f4ff325b94 to your computer and use it in GitHub Desktop.
CARTO.js v4: Wrap carto layer
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<style>
html,body,#map {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
</style>
<!-- Include Carto.js -->
<script src="https://cartodb-libs.global.ssl.fastly.net/carto.js/v4.0.9/carto.min.js"></script>
<!-- Include Leaflet -->
<script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js"></script>
<link href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" rel="stylesheet">
</head>
<body>
<div id="map"></div>
<script>
let layer;
let input;
let map;
function main() {
let map = L.map('map', {
zoomControl: false,
center: [0, 0],
zoom: 0
});
L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager_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>',
noWrap: true
}).addTo(map);
// define client
const client = new carto.Client({
apiKey: 'default_public',
username: 'oboix'
});
const source = new carto.source.SQL(`
SELECT * FROM world_table_2
`);
// define CartoCSS code to style data on map
const style = new carto.style.CartoCSS(`
#world_table_2 {
polygon-fill: #3E7BB6;
polygon-opacity: 0.7;
line-color: #FFF;
line-width: 0.5;
line-opacity: 1;}
`);
// create CARTO layer from source and style variables
const Cartolayer = new carto.layer.Layer(source, style);
// add CARTO layer to the client
client.addLayer(Cartolayer);
// get tile from client and add them to the map object
client.getLeafletLayer({
noWrap: true,
bounds: [
[-90, -180],
[90, 180]
]
}).addTo(map);
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment