Last active
March 16, 2017 13:00
-
-
Save rochoa/42eb2dcb48a7805c6920 to your computer and use it in GitHub Desktop.
[CARTO] Named maps viewer
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
document.getElementById('map').style.height = window.innerHeight + 'px'; | |
var map = L.map('map', { | |
scrollWheelZoom: false, | |
center: [45, 0], | |
zoom: 3 | |
}); | |
L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', { | |
attribution: '<a href="http://cartodb.com">CartoDB</a> © 2014', | |
maxZoom: 18 | |
}).addTo(map); | |
function updateMap() { | |
if (!configChanged()) { | |
return; | |
} | |
var jsonpLayergroupElement = document.getElementById('jsonpLayergroup'); | |
if (jsonpLayergroupElement) { | |
jsonpLayergroupElement.parentNode.removeChild(jsonpLayergroupElement); | |
} | |
var jsonpScriptElement = document.createElement('script'); | |
jsonpScriptElement.id = 'jsonpLayergroup'; | |
jsonpScriptElement.src = layergroupJsonpUrl(); | |
document.getElementsByTagName('head')[0].appendChild(jsonpScriptElement); | |
updateLastConfig(); | |
} | |
var tiledLayer = null; | |
function layergroupCallback(layergroup) { | |
if (layergroup.error) { | |
return; | |
} | |
if (tiledLayer) { | |
map.removeLayer(tiledLayer); | |
} | |
var tilesEndpoint = currentEndpoint() + '/' + layergroup.layergroupid + '/{z}/{x}/{y}.png'; | |
var protocol = 'https:' == document.location.protocol ? 'https' : 'http'; | |
if (layergroup.cdn_url && layergroup.cdn_url[protocol]) { | |
var domain = layergroup.cdn_url[protocol]; | |
if ('http' === protocol) { | |
domain = '{s}.' + domain; | |
} | |
tilesEndpoint = protocol + '://' + domain + '/' + currentUsername() + '/api/v1/map/' + layergroup.layergroupid + '/{z}/{x}/{y}.png'; | |
} | |
tiledLayer = L.tileLayer(tilesEndpoint, { | |
maxZoom: 18 | |
}).addTo(map); | |
} | |
function currentEndpoint() { | |
return [ | |
'https://', | |
currentUsername(), | |
'.cartodb.com/api/v1/map' | |
].join(''); | |
} | |
function currentUsername() { | |
return inputValue('username'); | |
} | |
function currentNamedMap() { | |
return inputValue('namedmap'); | |
} | |
function layergroupJsonpUrl() { | |
return [ | |
currentEndpoint(), | |
'/named/', | |
currentUsername() + '@' + currentNamedMap(), | |
'/jsonp?callback=layergroupCallback' | |
].join(''); | |
} | |
function inputValue(elementId) { | |
return document.getElementById(elementId).value; | |
} | |
var lastUsername, lastNamedmap; | |
function configChanged() { | |
return lastUsername !== currentUsername() | |
|| lastNamedmap !== currentNamedMap(); | |
} | |
function updateLastConfig() { | |
lastUsername = currentUsername(); | |
lastNamedmap = currentNamedMap(); | |
} | |
['username', 'namedmap'].forEach(function(elementId) { | |
var inputElement = document.getElementById(elementId); | |
if (inputElement) { | |
inputElement.addEventListener('keyup', function(event) { | |
if (event.keyCode === 13) { | |
updateMap(); | |
} | |
}, false); | |
} | |
}); | |
document.getElementById('load_namedmap').addEventListener('click', function(event) { | |
event.preventDefault(); | |
updateMap(); | |
return false; | |
}, false); | |
updateMap(); |
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 lang="en"> | |
<meta charset="UTF-8"> | |
<title></title> | |
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" /> | |
<style> | |
body { | |
margin: 0; | |
padding: 0; | |
border: 0; | |
font-family: "Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial, sans-serif; | |
} | |
textarea { | |
padding: 0; | |
margin: 0; | |
border: solid 1px #999; | |
height: 64px; | |
} | |
.wrap { | |
width: 100%; | |
position: fixed; | |
top: 0; | |
background-color: rgba(204, 204, 204, 0.7); | |
} | |
.config { | |
padding: 16px 0; | |
text-align: center; | |
} | |
.config input { | |
margin: 0 24px; | |
width: 160px; | |
} | |
#map { | |
width: 100%; | |
height: 480px; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="map"></div> | |
<div class="wrap"> | |
<form class="config"> | |
<label for="username">User</label> | |
<input type="text" name="username" id="username" value="rochoa"> | |
<label for="namedmap">Named map</label> | |
<input type="text" name="namedmap" id="namedmap" value="world_borders"> | |
<button id="load_namedmap">Apply</button> | |
</form> | |
</div> | |
</body> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script> | |
<script src="app.js"></script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment