Skip to content

Instantly share code, notes, and snippets.

@ramiroaznar
Last active May 29, 2019 08:19
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 ramiroaznar/44eb18664a8f3f97d9c3a3242736b38b to your computer and use it in GitHub Desktop.
Save ramiroaznar/44eb18664a8f3f97d9c3a3242736b38b to your computer and use it in GitHub Desktop.
CARTO JS & Planet Basemaps
<!DOCTYPE html>
<html>
<head>
<title>CARTO JS + Planet Basemaps</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- Include Airship -->
<link rel="stylesheet" href="https://libs.cartocdn.com/airship-style/v2.0.5/airship.css">
<link rel="stylesheet" href="https://libs.cartocdn.com/airship-icons/v2.0.5/icons.css">
<script src="https://libs.cartocdn.com/airship-components/v2.0.5/airship.js"></script>
<!-- Include Leaflet -->
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"></script>
<link href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" rel="stylesheet">
<!-- Include CARTO.js -->
<script src="https://libs.cartocdn.com/carto.js/v4.1.11/carto.min.js"></script>
<link href="main.css" rel="stylesheet">
</head>
<body id="app" class="as-app-body as-app">
<div class="as-content">
<main class="as-main">
<div class="as-map-area">
<div id="map"></div>
<div class="as-map-panels">
<div class="as-panel as-panel--top as-panel--right">
<div class="as-panel__element as-p--32">
<h4 class="as-subheader as-font--medium">CARTO JS & Planet Basemaps</h4>
<span class="as-caption">Insert Planet API Key</span>
<input id='input-form' class="as-input" type="text" placeholder="PL_API_KEY">
</div>
</div>
</div>
</div>
</main>
</div>
<script src="main.js" type="text/javascript"></script>
</body>
</html>
* {
box-sizing: border-box;
}
body, *{ margin: 0; padding: 0; }
#map {
position: absolute;
height: 100%;
width: 100%;
z-index: 0;
}
// add Leaflet basemap
const map = L.map('map').setView([30, 0], 3);
map.scrollWheelZoom.disable();
const client = new carto.Client({
apiKey: 'default_public',
username: 'cartojs-test'
});
const form = document.getElementById('input-form');
form.addEventListener('change', (e) => {
// get Planet API Key
let PL_API_KEY = e.srcElement.value;
// generate Planet basemap
let mosaic = `https://tiles1.planet.com/basemaps/v1/planet-tiles/global_monthly_2018_07_mosaic/gmap/{z}/{x}/{y}.png?api_key=${PL_API_KEY}`
// add Planet Basemap to Leaflet map
L.tileLayer(mosaic, {
maxZoom: 18
}).addTo(map);
// add CARTO VL layer
const source = new carto.source.Dataset('ne_10m_populated_places_simple');
const style = new carto.style.CartoCSS(`
#layer {
marker-width: 7;
marker-fill: #EE4D5A;
marker-line-color: #FFFFFF;
}
`);
const layer = new carto.layer.Layer(source, style);
client.addLayer(layer);
client.getLeafletLayer().addTo(map);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment