Skip to content

Instantly share code, notes, and snippets.

@Xatpy
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save Xatpy/f9cdabd598800538d9c2 to your computer and use it in GitHub Desktop.

Select an option

Save Xatpy/f9cdabd598800538d9c2 to your computer and use it in GitHub Desktop.
Read data from infowindow
<!DOCTYPE html>
<html>
<head>
<title>Read data from infowindow | CartoDB.js</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: 90%;
padding: 0;
margin: 0;
text-align: center;
}
</style>
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.12/themes/css/cartodb.css" />
</head>
<body>
<div id="map"></div>
<br>
<form>
<h2>Country:</h2>
<input id="inpCountry"></input>
<h2>Population 2005:</h2>
<input id="inpPop"></input>
<br> <br>
<button>Do something!</button>
</form>
<!-- include cartodb.js library -->
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.12/cartodb.js"></script>
<script>
var lyr;
var country;
var pop2005;
function main() {
cartodb.createVis('map', 'http://documentation.cartodb.com/api/v2/viz/2b13c956-e7c1-11e2-806b-5404a6a683d5/viz.json', {
shareable: true,
title: true,
description: true,
search: true,
tiles_loader: true,
center_lat: 60,
center_lon: 10,
zoom: 3
})
.done(function(vis, layers) {
lyr = layers[1];
lyr.infowindow.bind('change', function() {
if ((lyr.infowindow.attributes.content.fields !== undefined) && (lyr.infowindow.attributes.content.fields[4] !== undefined)) {
country = lyr.infowindow.attributes.content.fields[4].value;
pop2005 = lyr.infowindow.attributes.content.fields[7].value;
console.log(country);
console.log(pop2005);
document.getElementById("inpCountry").value = country;
document.getElementById("inpPop").value = pop2005;
}
});
})
.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