Created
September 2, 2013 07:11
-
-
Save javisantana/6409970 to your computer and use it in GitHub Desktop.
create a custom legend without CartoDB UI
This file contains hidden or 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>Leaflet multilayer example | 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: 100%; | |
padding: 0; | |
margin: 0; | |
} | |
</style> | |
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/themes/css/cartodb.css" /> | |
<!--[if lte IE 8]> | |
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/themes/css/cartodb.ie.css" /> | |
<![endif]--> | |
</head> | |
<body> | |
<div id="map"></div> | |
<!-- include cartodb.js library --> | |
<script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.js"></script> | |
<script> | |
function main() { | |
// create leaflet map | |
var map = L.map('map', { | |
zoomControl: false, | |
center: [43, 0], | |
zoom: 3 | |
}) | |
// add a base layer | |
L.tileLayer('http://tile.stamen.com/toner/{z}/{x}/{y}.png', { | |
attribution: 'Stamen' | |
}).addTo(map); | |
// add cartodb layer with one sublayer | |
cartodb.createLayer(map, { | |
user_name: 'examples', | |
type: 'cartodb', | |
sublayers: [{ | |
sql: 'select * from country_boundaries', | |
cartocss: '#layer { polygon-fill: #F00; polygon-opacity: 0.3; line-color: #F00; }' | |
}] | |
}) | |
.addTo(map) | |
.done(function(layer) { | |
var legend = new cdb.geo.ui.Legend({ | |
type: "custom", | |
data: [ | |
{ name: "Category 1", value: "#FFC926" }, | |
{ name: "Category 2", value: "#76EC00" }, | |
{ name: "Category 3", value: "#00BAF8" }, | |
{ name: "Category 4", value: "#D04CFD" } | |
] | |
}); | |
$('#map').append(legend.render().el); | |
}); | |
} | |
// you could use $(window).load(main); | |
window.onload = main;//function() {cdb.load('../src/', main) } | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this still applicable for the new cartodb version? I would like to render my layer legends outside of the map in a separate element. Can I just take the layer.layers[0].legend object that is returned by createLayer() and render it?