Skip to content

Instantly share code, notes, and snippets.

@ramiroaznar
Last active July 28, 2017 13:15
Show Gist options
  • Save ramiroaznar/ece4946e1ad024713688c36a51632cfb to your computer and use it in GitHub Desktop.
Save ramiroaznar/ece4946e1ad024713688c36a51632cfb to your computer and use it in GitHub Desktop.
CARTO.js | Show data from overlapping points
<!DOCTYPE html>
<html>
<head>
<title>CARTO.js | Show data from overlapping points</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" href="https://cartodb.com/assets/favicon.ico" />
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans" />
<link rel="stylesheet" href="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/themes/css/cartodb.css" />
<script src="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.js"></script>
<style>
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
#infobox{
width: 200px;
height: 150px;
position: absolute;
top: 20px;
left: 20px;
background-color: white;
opacity: 0.6;
text-align: center;
font-family: "Open Sans";
}
</style>
</head>
<body>
<!-- map div -->
<div id="map"></div>
<!-- infobox div -->
<div id="infobox">
<h4>Click on a point</h4>
<p id="location">Location</p>
<p id="number">Number of employees</p>
</div>
<script type="text/cartocss" id="style">
#layer {
marker-width: 9;
marker-fill: #FFB927;
marker-fill-opacity: 0.9;
marker-allow-overlap: true;
marker-line-width: 0;
marker-comp-op: multiply;
}
</script>
<script type="text/sql" id="query">
SELECT
*
FROM
carto_employees
</script>
<script>
function main() {
// get styles & query
var style = $("#style").text(),
query = $("#query").text(),
// declare map variable
map = L.map('map', {
zoomControl: false,
center: [41, -3],
zoom: 3
});
// add basemap
L.tileLayer('http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png', {attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CARTO</a>'}).addTo(map);
// add cartodb layer
cartodb.createLayer(map, {
user_name: 'ramirocdb',
type: 'cartodb',
sublayers: [{
sql: query,
cartocss: style,
}]
}).addTo(map)
.done(function(layer){
var subLayer = layer.getSubLayer(0);
// set layer interactivity
subLayer.setInteraction(true);
subLayer.setInteractivity(["cartodb_id"]);
// change cursor to pointer when hovering over a feature
subLayer.on('mouseover',function(){
$('#map').css({"cursor":"pointer"})
});
// get long lat coordinates
subLayer.on('featureClick', function(e, latlng, pos, data) {
console.log(latlng);
console.log(latlng[0]);
console.log(latlng[1]);
var latitude = latlng[0],
longitude = latlng[1];
var sql = new cartodb.SQL({ user: 'ramirocdb'});
// get data sharing the same geometry
sql.execute(
"SELECT * FROM carto_employees b WHERE ST_DWithin(cdb_latlng(" + latitude + "," + longitude + ")::geometry, the_geom::geometry, 1)")
.done(function(data) {
// get and show location
console.log(data.rows[0].location);
var location = data.rows[0].location;
$('#location').text(location);
// get and show number of points per location
console.log(data.rows.length)
var number = data.rows.length;
$('#number').text(number);
})
.error(function(errors) {
// errors contains a list of errors
console.log("errors:" + errors);
})
});
});
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment