Skip to content

Instantly share code, notes, and snippets.

@Xatpy
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Xatpy/84b0ad64dc2ce7a4fcfd to your computer and use it in GitHub Desktop.
Save Xatpy/84b0ad64dc2ce7a4fcfd to your computer and use it in GitHub Desktop.
Infowindows with several layers - CartoDB.js
<!DOCTYPE html>
<html>
<head>
<title>Infowindows several layers ||| CartoDB </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="http://cartodb.com/assets/favicon.ico" />
<style>
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
</style>
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.11/themes/css/cartodb.css" />
</head>
<body>
<div id="map"></div>
<!-- include cartodb.js library -->
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.12/cartodb.js"></script>
<script type="infowindow/html" id="infowindow_template">
<div class="cartodb-popup">
<a href="#close" class="cartodb-popup-close-button close">x</a>
<div class="cartodb-popup-content-wrapper">
<div class="cartodb-popup-content">
<ul id="mylist">
</ul>
</div>
</div>
<div class="cartodb-popup-tip-container"></div>
</div>
</script>
<script>
function main() {
var country, area;
var url = 'http://xatpy.cartodb.com/api/v2/sql?q=SELECT%20st_x(the_geom),%20ST_Y(the_geom),%20name%20from%20layer_1%20';
var radius = 0.5;
cartodb.createVis('map', 'http://team.cartodb.com/api/v2/viz/93a84c42-cc00-11e4-b3de-0e018d66dc29/viz.json', {
shareable: true,
title: true,
description: true,
search: true,
tiles_loader: true
})
.done(function(vis, layers) {
var infowindow = layers[1].getSubLayer(0).infowindow;
infowindow.set('template', _.debounce(function(data) {
var clickPosLatLng = this.model.get('latlng');
country = data.name;
area = data.area;
var url_query = "http://xatpy.cartodb.com/api/v2/sql?q=SELECT%20st_x(the_geom),%20ST_Y(the_geom),%20name%20from%20places%20where%20st_dWithin(the_geom,'SRID=4326;POINT(" + clickPosLatLng[1] + "%20" + clickPosLatLng[0] + ")%27,%20"+radius+"%20" +'%29';
$.get(url_query, function (data){
debugger
console.log(data)
var em = $('<div>');
_.map(data.rows, function(r) {
var element = $('<li>' + r.name + '</li><li>'+country+'</li><li>'+area+'</li>');
em.append(element);
});
$('#mylist').append(em);
});
return $('#infowindow_template').html();
}, 10));
})
.error(function(err) {
console.log(err);
});
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment