Skip to content

Instantly share code, notes, and snippets.

@ramiroaznar
Last active February 14, 2017 11:54
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 ramiroaznar/c1abf35e97281bca0551e93762779286 to your computer and use it in GitHub Desktop.
Save ramiroaznar/c1abf35e97281bca0551e93762779286 to your computer and use it in GitHub Desktop.
Datepicker + CARTO.js

Choose a date with the date picker between 31 Dec 2009 and 31 Jan 2014.

<!DOCTYPE html>
<html>
<head>
<title>Datepicker with CARTO.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" />
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
<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>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<style>
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
</style>
<!--
<link rel="stylesheet" href="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/themes/css/cartodb.css" />
-->
</head>
<body>
<div id="map"></div>
<div id='date-picker' style="top: 20px; left: 75px; bottom: auto;position: absolute">
<p>Date: <input type="text" id="datepicker"></p>
</div>
<!-- include cartodb.js library -->
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script>
<!--
<script src="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.js"></script>
-->
<!-- Drop your code between the script tags below! -->
<script>
function main() {
mapUrl='https://team.carto.com/u/ramirocartodb/api/v2/viz/0c4b0aaa-58c2-11e6-8d48-0e3ebc282e83/viz.json'
cartodb.createVis('map', mapUrl)
.on('done', function(vis,layers) {
var sublayers = layers[1],
sublayer1 = sublayers.getSubLayer(0);
// date picker function
$ ( function() {
$("#datepicker").datepicker(
{
onSelect: function() {
var dateObject = $(this).datepicker('getDate');
console.log(dateObject);
var d = dateObject,
day = d.getDate(),
month = d.getMonth() + 1,
year = d.getFullYear(),
date = year + '-' + month + '-' + day;
console.log(date);
var query = "SELECT * FROM railroad_data WHERE date = '" + date + "&'";
console.log(query);
sublayer1.setSQL(query);
}
});
});
}).on('error', function() {
console.log("some error occurred");
});
}
// load main() function
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment