Last active
May 12, 2017 14:12
-
-
Save ramiroaznar/a0d1fbb0a757b5a45856cfed23962997 to your computer and use it in GitHub Desktop.
CARTO Locations Training Sample
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>CARTO Locations | Training Sample</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="https://cartodb.com/assets/favicon.ico" /> | |
<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> | |
<style> | |
html, body, #map { | |
height: 100%; | |
padding: 0; | |
margin: 0; | |
} | |
button { | |
top: 20px; | |
bottom: auto; | |
position: absolute; | |
z-index: 100; | |
left: 20px; | |
background-color: white; | |
width: 120px; | |
height: 60px; | |
border-radius: 5px; | |
} | |
</style> | |
</head> | |
<body> | |
<!-- map div --> | |
<div id="map"></div> | |
<!-- define a button object--> | |
<div id="button"> | |
<button>Bubbles!</button> | |
</div> | |
<!-- infowindow div --> | |
<script type="infowindow/html" id="infowindow_template"> | |
<div class="cartodb-popup v2"> | |
<a href="#close" class="cartodb-popup-close-button close">x</a> | |
<div class="cartodb-popup-content-wrapper"> | |
<div class="cartodb-popup-content"> | |
<h3>{{name}}</h3> | |
<h4>{{adm0name}}</h4> | |
<p>{{pop_max}} inhabitants</p> | |
</div> | |
<div class="cartodb-popup-tip-container"></div> | |
</div> | |
</script> | |
<!-- legend div --> | |
<div class='cartodb-legend category'> | |
<div class="legend-title" style="color:#284a59">Cities</div> | |
<ul> | |
<li><div class="bullet" style="background-color:#5F4690"></div>Populated Place</li> | |
<li><div class="bullet" style="background-color:#1D6996"></div>Admin. 1 Capital</li> | |
<li><div class="bullet" style="background-color:#38A6A5"></div>Admin. 0 Capital</li> | |
<li><div class="bullet" style="background-color:#0F8554"></div>Admin. 1 region Capital</li> | |
<li><div class="bullet" style="background-color:#73AF48"></div>Scientific Station</li> | |
<li><div class="bullet" style="background-color:#EDAD08"></div>Admin. O region Capital </li> | |
<li><div class="bullet" style="background-color:#E17C05"></div>Admin. 0 Capital Alt.</li> | |
<li><div class="bullet" style="background-color:#CC503E"></div>Historic Place</li> | |
<li><div class="bullet" style="background-color:#94346E"></div>Meteorological Station</li> | |
</ul> | |
</div> | |
<script type="text/cartocss" id="style"> | |
#layer { | |
marker-width: 10; | |
marker-fill: ramp([featurecla], (#5F4690, #1D6996, #38A6A5, #0F8554, #73AF48, #EDAD08, #E17C05, #CC503E, #94346E), ("Populated place", "Admin-1 capital", "Admin-0 capital", "Admin-1 region capital", "Scientific station", "Admin-0 region capital", "Admin-0 capital alt", "Historic place", "Meteorological Station"), "="); | |
marker-fill-opacity: 0.7; | |
marker-allow-overlap: true; | |
marker-line-width: 0; | |
} | |
</script> | |
<script type="text/sql" id="query"> | |
SELECT | |
* | |
FROM | |
populated_places WHERE pop_max > 700000 | |
</script> | |
<script> | |
function main() { | |
// get styles & query | |
var style = $("#style").text(), | |
query = $("#query").text(), | |
// declare map variable | |
map = L.map('map', { | |
zoomControl: false, | |
center: [41, -3], | |
zoom: 3 | |
}); | |
// add basemap | |
L.tileLayer('http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png', {attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="http://cartodb.com/attributions">CARTO</a>'}).addTo(map); | |
// add cartodb layer | |
cartodb.createLayer(map, { | |
user_name: 'ramirocartodb', | |
type: 'cartodb', | |
sublayers: [{ | |
sql: query, | |
cartocss: style, | |
}] | |
}).addTo(map) | |
.done(function(layer){ | |
// declare sublayer variable | |
var cityLayer = layer.getSubLayer(0); | |
// set layer interactivity | |
cityLayer.setInteractivity("name, adm0name, pop_max"); | |
cityLayer.setInteraction(true); | |
// add popup | |
cdb.vis.Vis.addInfowindow( | |
map, layer, ["name", "adm0name", "pop_max"], | |
{ | |
infowindowTemplate: $('#infowindow_template').html() | |
}); | |
// add tooltip | |
var tooltip = layer.leafletMap.viz.addOverlay({ | |
type: 'tooltip', | |
layer: layer, | |
template: '<div class="cartodb-tooltip-content-wrapper"><div class="cartodb-tooltip-content"><h4>{{name}}</h4></div></div>', | |
width: 200, | |
position: 'bottom|right', | |
fields: [{ name: 'name' }] | |
}); | |
$('body').append(tooltip.render().el); | |
// add button action | |
$('button').on('click', function() { | |
var new_style = style.replace('marker-width: 10;','marker-width: ramp([pop_max], range(3,25), quantiles(7));'); | |
cityLayer.setCartoCSS(new_style); | |
}); | |
}); | |
} | |
window.onload = main; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment