Last active
April 5, 2016 13:39
-
-
Save ramiroaznar/8f732220f7626850ca7143917d141728 to your computer and use it in GitHub Desktop.
Points to Line with CartoDB.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Points to Line with CartoDB.js and Leaflet</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="http://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" /> | |
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script> | |
<style> | |
html, body,#map { | |
width:100%; | |
height:100%; | |
padding: 0; | |
margin: 0; | |
} | |
</style> | |
</head> | |
<body> | |
<div id='map'></div> | |
<script type="text/javascript"> | |
// declare geom & lines variables | |
var geom; | |
var lines; | |
function main() { | |
// center and zoom level | |
var options = { | |
center: [59.5,-151.5], | |
zoom: 10, | |
}; | |
// declare a map object | |
var map_object = new L.Map('map', options); | |
// add basemap | |
L.tileLayer('http://{s}.tile.opentopomap.org/{z}/{x}/{y}.png', { | |
attribution: 'OpenStreetMaps' | |
}).addTo(map_object); | |
cartodb.createLayer(map_object,{ | |
user_name: 'ramirocartodb', | |
type: 'cartodb', | |
sublayers: [ | |
{ | |
sql: 'SELECT * FROM kachemakbaysp_track_points', | |
cartocss: '#kachemakbaysp_track_points{marker-fill: #FF6600;marker-allow-overlap: true;marker-opacity:0.5}' | |
} | |
]}).addTo(map_object) // add points layer | |
.done(function(layer){ | |
// remove points layer | |
layer.remove(); | |
// cartodb sql api | |
var sql = new cartodb.SQL({ user: 'ramirocartodb'}); | |
// select the attribute tables to extract from CartoDB table | |
sql.execute("SELECT ST_asGeoJSON(ST_MakeLine(the_geom ORDER BY track_seg_id ASC)) AS geom, track_fid FROM kachemakbaysp_track_points GROUP BY track_fid") | |
.done(function(data){ | |
for(i = 0; i < data.total_rows; i++){ | |
geom = data.rows[i].geom; // get geom from row | |
lines = L.geoJson(JSON.parse(geom),{ // style lines | |
style:{ | |
color: "red", | |
weight: 5, | |
opacity: 0.5 | |
} | |
}); | |
map_object.addLayer(lines) | |
} | |
}); // end sql api call | |
}); // end createLayer | |
} | |
window.onload = main; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment