In this guide, you will learn the basics of visualizing data with CARTO VL on top of a world basemap.There are no previous requirements to complete this guide, but a basic knowledge of HTML, CSS and JavaScript would be helpful. In order to start creating maps, you will need text editor and an internet connection.
After completing this guide, you will have your first CARTO VL map!
The most straight-forward way to use CARTO VL is to include the required files from our CDN as seen in the code below. If you prefer to use the minified file, which is lighter and therefore loads faster, you can use https://libs.cartocdn.com/carto-vl/v0.10.0/carto-vl.min.js
. You will also need to add Mapbox GL JavaScript and CSS files. This enables you to use both carto
and mapboxgl
in your code.
1
2
3
4
5
6
7
8
<head>
<!-- Include CARTO VL JS from the CARTO CDN-->
<script src="https://libs.cartocdn.com/carto-vl/v0.10.0/carto-vl.js"></script>
<!-- Include Mapbox GL from the Mapbox CDN-->
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.css' rel='stylesheet' />
</head>
Note:
CARTO VL is not compatible with every Mapbox GL version. We recommend using the same version that we use in the examples. However, every version from version 0.50.0
should work. Historically, we provided with patched MGL bundles, but this is no longer required.
Next, you need to create a div
where the map will be drawn:
1
<div id="map"></div>
Style the map div
to ensure the map displays properly:
1
2
3
4
5
#map {
position: absolute;
height: 100%;
width: 100%;
}
Once you have a div
for your map, you have to use the mapboxgl
object to initialize it using the following parameters:
container
indicates where the map is going to be placedstyle
sets the basemap style to usecenter
sets the opening extent of the basemapzoom
sets the default zoom level of the basemapdragRotate
disables map rotation1
2
3
4
5
6
const map = new mapboxgl.Map({
container: 'map',
style: carto.basemaps.voyager,
center: [0, 30],
zoom: 2,
});
For the basemap style
parameter, you can add either Mapbox custom styles or one of the three predefined styles offered by CARTO:
carto.basemaps.voyager
https://basemaps.cartocdn.com/gl/voyager-gl-style/style.jsoncarto.basemaps.positron
https://basemaps.cartocdn.com/gl/positron-gl-style/style.jsoncarto.basemaps.darkmatter
https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.jsonAt this point you will have a basic map with Voyager as the base, that opens at zoom level 2 and centered on the world:
In order to render data from CARTO you need to create a CARTO account and then get the necessary credentials.
The first thing you need to do is authenticate the client with your username
and apiKey
. For guides and examples, we provide a public CARTO account that you can use to try out the library:
1
2
3
4
carto.setDefaultAuth({
username: 'cartovl',
apiKey: 'default_public'
});
The next step is to define the source
from your account to be displayed on the map. In the example below, the source
is a dataset named populated_places
with all the populated places around the world from Natural Earth.
1
const source = new carto.source.Dataset('populated_places');
A Viz object
is one of the core elements of CARTO VL. It defines how the data will be styled and displayed on your map.
Create an empty Viz object that uses the default CARTO VL styling:
1
const viz = new carto.Viz();
Now that you have defined a source
and a Viz object
, you need to create a new layer
that can be added to the map.
1
const layer = new carto.Layer('layer', source, viz);
Once you have defined the layer
, you need to use the addTo
method to add it to the map.
1
layer.addTo(map);
Using the Viz object you created in a previous step, override the default by adding styling for the color
and size
of the points on your map:
1
2
3
4
const viz = new carto.Viz(`
color: red
width: 10
`);
For more information about styling, check out the guide Introduction to Styling.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://libs.cartocdn.com/carto-vl/v0.10.0/carto-vl.js"></script>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.css' rel='stylesheet' />
<link href="https://carto.com/developers/carto-vl/v0.10.0/examples/maps/style.css" rel="stylesheet">
</head>
<body>
<!-- Add map container -->
<div id="map"></div>
<script>
// Add basemap and set properties
const map = new mapboxgl.Map({
container: 'map',
style: carto.basemaps.voyager,
center: [0, 30],
zoom: 2,
dragRotate: false,
});
//** CARTO VL functionality begins here **//
// Define user
carto.setDefaultAuth({
username: 'cartovl',
apiKey: 'default_public'
});
// Define source
const source = new carto.source.Dataset('populated_places');
// Define Viz object and custom style
const viz = new carto.Viz(`
color: purple
width: 5
`);
// Define map layer
const layer = new carto.Layer('layer', source, viz);
// Add map layer
layer.addTo(map);
</script>
</body>
</html>