Last active
July 12, 2018 11:25
-
-
Save ramiroaznar/cd367e4fe8dd11d8d87d9d20cfffca08 to your computer and use it in GitHub Desktop.
Feature click + 2 layers | CARTO.js v4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Feature click + 2 layers | CARTO.js v4</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@1.2.0/dist/leaflet.js"></script> | |
<link href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" rel="stylesheet"> | |
<!-- Include CARTO.js --> | |
<script src="https://cartodb-libs.global.ssl.fastly.net/carto.js/v4.0.0-beta.20/carto.min.js"></script> | |
<style> | |
* { | |
box-sizing: border-box; | |
} | |
body, *{ margin: 0; padding: 0; } | |
#map { | |
position: absolute; | |
height: 100%; | |
width: 100%; | |
z-index: 0; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="map"></div> | |
<script> | |
const map = L.map('map').setView([30, 0], 3); | |
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: 'ramirocartodb' | |
}); | |
const citySource = new carto.source.Dataset(` | |
ne_10m_populated_places_simple | |
`); | |
const cityStyle = new carto.style.CartoCSS(` | |
#layer { | |
marker-width: 7; | |
marker-fill: #EE4D5A; | |
marker-line-color: #FFFFFF; | |
} | |
`); | |
const cityLayer = new carto.layer.Layer(citySource, cityStyle, { | |
featureClickColumns: ['name', 'pop_max'] | |
}); | |
const worldSource = new carto.source.Dataset(` | |
world_borders | |
`); | |
const worldStyle = new carto.style.CartoCSS(` | |
#layer { | |
polygon-fill: #826DBA; | |
polygon-opacity: 0.8; | |
::outline { | |
line-width: 1; | |
line-color: #FFFFFF; | |
line-opacity: 0.8; | |
} | |
} | |
`); | |
const worldLayer = new carto.layer.Layer(worldSource, worldStyle, { | |
featureClickColumns: ['name', 'pop2005'] | |
}); | |
client.addLayers([worldLayer, cityLayer]); | |
client.getLeafletLayer().addTo(map); | |
const cityPopup = L.popup({ closeButton: false }); | |
cityLayer.on('featureClicked', featureEvent => { | |
cityPopup.setLatLng(featureEvent.latLng); | |
let city = featureEvent.data.name; | |
let pop_max = featureEvent.data.pop_max; | |
cityPopup.setContent(` | |
<h2>${city}</h2> | |
<h3>${pop_max} inhab.</h3> | |
`); | |
cityPopup.openOn(map); | |
}); | |
const worldPopup = L.popup({ closeButton: false }); | |
worldLayer.on('featureClicked', featureEvent => { | |
worldPopup.setLatLng(featureEvent.latLng); | |
let country = featureEvent.data.name; | |
let pop2005 = featureEvent.data.pop2005; | |
worldPopup.setContent(` | |
<h2>${country}</h2> | |
<h3>${pop2005} inhab.</h3> | |
`); | |
worldPopup.openOn(map); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment