Skip to content

Instantly share code, notes, and snippets.

@ernesmb
Last active October 23, 2017 19:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ernesmb/f5c7bffe033e898bf9f2 to your computer and use it in GitHub Desktop.
Save ernesmb/f5c7bffe033e898bf9f2 to your computer and use it in GitHub Desktop.
Named Maps Test

Authenticated call to create a template

curl -X POST -H 'Content-Type: application/json' -d @template_test.json 'https://ernestomb.cartodb.com/api/v1/map/named?api_key=API_KEY'

Authenticated call to update a template

curl -X PUT -H 'Content-Type: application/json' -d @template_test.json 'https://ernestomb.cartodb.com/api/v1/map/named/template_test?api_key=API_KEY'
<!DOCTYPE html>
<html>
<head>
<title>Named Maps test | CartoDB</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;
position:relative;
}
#bar{
position:absolute;
z-index: 11;
top:0;
right:0;
margin: 4px;
}
</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="bar">
<button id="blue">blue</button>
<button id="green">green</button>
<button id="red">red</button>
<button id="megacity">only megacities</button>
<button id="nomegacity">non-megacities</button>
<button id="tog">layer on/off</button>
</div>
<!-- include cartodb.js library -->
<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 type="infowindow/html" id="infowindow_template">
<div class="cartodb-popup">
<a href="#close" class="cartodb-popup-close-button close">x</a>
<div class="cartodb-popup-content-wrapper">
<div class="cartodb-popup-content">
<h4>this is</h4>
<p>{{content.data.name}}</p>
<h4>has a Population of</h4>
<p>{{content.data.pop_max}}</p>
</div>
</div>
<div class="cartodb-popup-tip-container"></div>
</div>
</script>
<script>
var map=L.map('map', {center: [36,0],zoom:2});
function main(){
L.tileLayer('http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png', {
attribution:'Stamen and CartoDB attribution'
}).addTo(map);
cartodb.createLayer(map, {
user_name:'ernestomb',
type:'namedmap',
named_map:{
name: 'template_test',
layers:[{
layer_name:'l',
interactivity: "cartodb_id, megacity, name, pop_max"
}
]
}
},
{https:true})
.on('done', function(layer){
layer.setAuthToken('ernesto');
sublayer=layer.getSubLayer(0);
$('#red').click(function(){
layer.setParams('color','red');
});
$('#blue').click(function(){
layer.setParams('color','blue');
});
$('#green').click(function(){
layer.setParams('color','green');
});
$('#megacity').click(function(){
layer.setParams('megacity',1);
});
$('#nomegacity').click(function(){
layer.setParams('megacity',0);
});
$('#tog').click(function(){
layer.toggle();
});
cdb.vis.Vis.addInfowindow(map, layer.getSubLayer(0), ['cartodb_id','name','pop_max'],{infowindowTemplate: $('#infowindow_template').html()});
})
.addTo(map);
}
window.onload = main;
</script>
</body>
</html>
{
"version": "0.0.1",
"name": "template_test",
"auth": {
"method": "token",
"valid_tokens": [
"ernesto"
]
},
"placeholders": {
"color": {
"type": "css_color",
"default": "red"
},
"megacity": {
"type": "number",
"default": 1
}
},
"layergroup": {
"version": "1.0.1",
"layers": [
{
"type": "cartodb",
"options": {
"cartocss_version": "2.1.1",
"cartocss": "#layer { marker-fill: <%= color %>; }",
"sql": "select * from populated_places_named WHERE megacity = <%= megacity %>",
"interactivity" : ["cartodb_id","name","pop_max"]
}
}
]
},
"view": {
"zoom": 3,
"center": {
"lng": 0,
"lat": 36
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment