Skip to content

Instantly share code, notes, and snippets.

Last active October 11, 2017 11:09
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chriswhong/2695b75fd1936bd034df83c91738648d to your computer and use it in GitHub Desktop.



This is a simple example of consuming Mapnik Vector Tiles (MVT) from a Carto account in Mapbox GL.

Carto raster tiles follow a {z}/{x}/{y}.png template, and the equivalent vector tile is available at the same template URL by using the file extension .mvt The mvt template can be used to create a new vector tile source for your mapboxGL map.

The easiest way to get at them is to create a map in your Carto account and build a tile template URL based on the map's id. (This will only work for named maps, or maps that are public but based on private data. The process is slightly more complicated for anonymous maps , as you must first initialize the map before you can build the tile template URL)

So, the raster tile URL

has an equivalent vector tile url of

##Some Notes

It can help keep the size down if you only include attribute data that you plan on using for rendering or infowindows. At a bare minimum you need the_geom_webmercator in the query for your map.

Carto MVT tiles appear to only work if the map has one layer.

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<link href='' rel='stylesheet' />
body {
margin: 0;
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
<div id='map'></div>
<script src=''></script>
<script src=""></script>
//initialize MapboxGL map
mapboxgl.accessToken = 'pk.eyJ1IjoiY3dob25nbnljIiwiYSI6ImNpczF1MXdrdjA4MXcycXA4ZGtyN2x5YXIifQ.3HGyME8tBs6BnljzUVIt4Q';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
zoom: 1,
center: [0,20]
map.addControl(new mapboxgl.NavigationControl());'style.load', function() {
//this is id of the map in my carto account. You can see that it is similar to the id shown in the url of the public map:
//When you create a public map based on private data, you are actually creating a 'named map' in Carto. You can actually build a z/x/y tile template directly from a named map's name, you don't need to 'initialize' the map
//This is the same maps template that would generate a raster tile, but it uses .mvt instead of .png. Go ahead, load '' and see the equivalent raster tile!
var mapName = 'tpl_36dba776_9746_11e6_9d0c_0e05a8b3e3d7'
var tiles = [
'' + mapName + '/mapnik/{z}/{x}/{y}.mvt'
//add a mapboxGL source
map.addSource("points", {
"type": "vector",
"tiles": tiles
//add a layer based on the source that was just added
"id": "points",
"source": 'points',
"source-layer": "layer0",
"type": "circle",
"paint": {
"circle-radius": {
"stops": [
"circle-color": {
property: 'mag',
stops: [
[0, '#eff3ff'],
[1, '#c6dbef'],
[2, '#9ecae1'],
[3, '#6baed6'],
[4, '#4292c6'],
[5, '#2171b5'],
[6, '#084594']
"circle-opacity": 0.75
//add a popup
var popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
map.on('mousemove', function(e) {
var features = map.queryRenderedFeatures(e.point, { layers: ['points'] })
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = (features.length) ? 'pointer' : ''
if (!features.length) {
var feature = features[0]
var content = 'Place: ' + + '<br/>'
content += 'Time: ' + + '<br/>'
content += 'Magnitude: ' + + '<br/>'
content += 'Depth: ' + + '<br/>'
// Populate the popup and set its coordinates
// based on the feature found.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment