-
-
Save ivanmalagon/f35d350606bbe460cd9ae984469a6cf0 to your computer and use it in GitHub Desktop.
Feature click on multilayer
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>Multilayer | CARTO</title> | |
<meta name="viewport" content="initial-scale=1.0"> | |
<meta charset="utf-8"> | |
<!-- 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://cartodb-libs.global.ssl.fastly.net/carto.js/v4.0.0-beta.25/carto.min.js"></script> | |
<style> | |
body { margin: 0; padding: 0; } | |
#map { position: absolute; height: 100%; width: 100%; } | |
</style> | |
</head> | |
<body> | |
<div id="map"></div> | |
<script> | |
const map = L.map('map').setView([40, 0], 5); | |
L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager_nolabels/{z}/{x}/{y}.png', { | |
maxZoom: 18 | |
}).addTo(map); | |
const client = new carto.Client({ | |
apiKey: 'YOUR_API_KEY', | |
username: 'cartojs-test' | |
}); | |
const spainCitiesSource = new carto.source.SQL(` | |
SELECT * | |
FROM ne_10m_populated_places_simple | |
WHERE adm0name = \'Spain\' | |
`); | |
const spainCitiesStyle = new carto.style.CartoCSS(` | |
#layer { | |
marker-width: 16; | |
marker-fill: #EE4D5A; | |
marker-line-color: #FFFFFF; | |
} | |
`); | |
const spainCitiesLayer = new carto.layer.Layer(spainCitiesSource, spainCitiesStyle, { | |
featureClickColumns: ['name'] | |
}); | |
const europeCountriesSource = new carto.source.Dataset(` | |
ne_adm0_europe | |
`); | |
const europeCountriesStyle = new carto.style.CartoCSS(` | |
#layer { | |
polygon-fill: #826DBA; | |
polygon-opacity: 0.8; | |
::outline { | |
line-width: 1; | |
line-color: #FFFFFF; | |
line-opacity: 0.8; | |
} | |
} | |
`); | |
const europeCountriesLayer = new carto.layer.Layer(europeCountriesSource, europeCountriesStyle, { | |
featureClickColumns: ['admin'] | |
}); | |
client.addLayers([europeCountriesLayer, spainCitiesLayer]); | |
client.getLeafletLayer().addTo(map); | |
// Interactivity | |
europeCountriesLayer.on('featureClicked', function (event) { | |
console.log('Country:', event.data.admin); | |
}); | |
spainCitiesLayer.on('featureClicked', function (event) { | |
console.log('City:', event.data.name); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment