Skip to content

Instantly share code, notes, and snippets.

@oriolbx
Created February 5, 2019 11:20
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 oriolbx/d4fb119c2d2b101694ddf3a1040ab19d to your computer and use it in GitHub Desktop.
Save oriolbx/d4fb119c2d2b101694ddf3a1040ab19d to your computer and use it in GitHub Desktop.
open popup from dropdown menu
<!DOCTYPE html>
<html>
<head>
<title>Pop-ups opened programmatically | CARTO</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">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>
<!-- 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.10/carto.min.js"></script>
<link href="https://carto.com/developers/carto-js/examples/maps/public/style.css" rel="stylesheet">
</head>
<body>
<div id="map"></div>
<aside class="toolbox">
<div class="box">
<header>
<h1>Pop-ups opened programmatically</h1>
<button class="github-logo js-source-link"></button>
</header>
<section>
<p class="description open-sans">Open pop-up programmatically</p>
</section>
<br/>
<section>
<div id="searchbox">
<select id="selectDrop">
<option selected value="">Please Select</option>
</select>
</div>
</section>
<footer class="js-footer"></footer>
</div>
</aside>
<script>
const map = L.map('map').setView([0, 0], 3);
map.scrollWheelZoom.disable();
populateDrowpDown()
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: 'cartojs-test'
});
const cartoSource = new carto.source.Dataset(`
ne_10m_populated_places_simple
`);
const cartoStyle = new carto.style.CartoCSS(`
#layer {
marker-width: 20;
marker-fill: #EE4D5A;
marker-line-color: #FFFFFF;
}
`);
const cartoLayer = new carto.layer.Layer(cartoSource, cartoStyle, {
featureClickColumns: ['name', 'pop_max', 'pop_min']
});
client.addLayer(cartoLayer);
client.getLeafletLayer().addTo(map);
const popup = L.popup({ closeButton: false });
function openPopup(data) {
lon = data.longitude;
lat = data.latitude;
let content = '<div class="widget">';
content += `<ul>`;
for (let value in data){
content += `<li><h3>${value}</h3><p class="open-sans">${data[value]}</p></li>`;
}
content += `</ul>`;
content += `</div>`;
popup.setContent(content);
popup.setLatLng(L.latLng(lat, lon));
if (!popup.isOpen()) {
popup.openOn(map);
}
}
function closePopUp(featureEvent) {
// remove popup from map object
popup.removeFrom(map)
}
// function to get list of city names to populate dropdown menu
function populateDrowpDown(){
return fetch(
`https://cartojs-test.carto.com/api/v2/sql?format=geojson&q=SELECT the_geom, name FROM ne_10m_populated_places_simple ORDER BY name ASC`
).then((resp) => resp.json())
.then((response) => {
return response['features'].map(function(feature){
option = document.createElement("option")
option.setAttribute("value", feature.properties.name)
option.textContent = feature.properties.name
document.getElementById("selectDrop").appendChild(option);
});
}).catch((error) => {
console.log(error)
})
}
// when select option from downdown menu, get all fields from dataset and execute openPopup functions
document.getElementById('selectDrop').addEventListener("change", function (e) {
input = e.currentTarget.selectedOptions[0].attributes[0].value;
return fetch(`https://cartojs-test.carto.com/api/v2/sql?format=geojson&q=SELECT * FROM ne_10m_populated_places_simple where name Ilike '${input}'`)
.then((resp) => resp.json())
.then((response) => {
openPopup(response.features[0].properties)
})
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment