Skip to content

Instantly share code, notes, and snippets.

@oriolbx
Last active October 16, 2018 15:06
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/f932726b627ed5656bb268ad6d6d031c to your computer and use it in GitHub Desktop.
Save oriolbx/f932726b627ed5656bb268ad6d6d031c to your computer and use it in GitHub Desktop.
CARTO.js v4: Change zoom level based on dropdown menu
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<style>
html,
body,
#map {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
#selectDrop {
background-color: #d2eaef;
opacity: 0.8;
position: absolute;
top: 10px;
left: 50px;
width: auto;
height: auto;
padding: 10px;
display: block;
z-index: 9000;
}
#selectDrop input {
width: 200px;
}
div#results {
background: #FFF;
}
</style>
<!-- Include Carto.js -->
<script src="https://libs.cartocdn.com/carto.js/v4.1.5/carto.min.js"></script>
<!-- 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">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div id="map"></div>
<div id="searchbox">
<select id="selectDrop">
<option selected value="">Please Select</option>
</select>
<script>
let layer;
let input;
let map;
function main() {
let map = L.map('map', {
zoomControl: false,
center: [41.390205, 2.154007],
zoom: 4
});
L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager_nolabels/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CartoDB</a>'
}).addTo(map);
// define client
const client = new carto.Client({
apiKey: 'default_public',
username: 'oboix'
});
const source = new carto.source.SQL(`
SELECT * FROM world_table_2
`);
// define CartoCSS code to style data on map
const style = new carto.style.CartoCSS(`
#world_table_2 {
polygon-fill: #3E7BB6;
polygon-opacity: 0.7;
line-color: #FFF;
line-width: 0.5;
line-opacity: 1;}
`);
// create CARTO layer from source and style variables
const Cartolayer = new carto.layer.Layer(source, style);
// add CARTO layer to the client
client.addLayer(Cartolayer);
// get tile from client and add them to the map object
client.getLeafletLayer().addTo(map);
$(function(){
$.ajax({
type: "GET",
url: `https://oboix.carto.com/api/v2/sql?format=geojson&q=SELECT the_geom, name FROM world_table_2`,
dataType: 'json',
success: function (response) {
response['features'].map(function(feature){
$("<option />")
.attr("value", feature.properties.name)
.html(feature.properties.name)
.appendTo("#selectDrop");
});
}
});
});
$('#selectDrop').change(function (e) {
input = e.currentTarget.selectedOptions[0].attributes[0].value;
$.ajax({
type: "GET",
url: `https://oboix.carto.com/api/v2/sql?format=geojson&q=SELECT * FROM world_table_2 where name Ilike '${input}'`,
dataType: 'json',
success: function (response) {
geojsonLayer = L.geoJson(response)
map.fitBounds(geojsonLayer.getBounds());
}});
})
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment