Skip to content

Instantly share code, notes, and snippets.

@xavijam
Created October 14, 2014 21:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xavijam/57f1c141bff4990b598f to your computer and use it in GitHub Desktop.
Save xavijam/57f1c141bff4990b598f to your computer and use it in GitHub Desktop.
Change layer properties from an infowindow link
<!DOCTYPE html>
<html>
<head>
<title>Change layer things from infowindow links | 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" />
</head>
<body>
<div id="map"></div>
<!-- include cartodb.js library -->
<script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.uncompressed.js"></script>
<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">
<a href="#/open-something" id="link">Change points color</a>
</div>
</div>
<div class="cartodb-popup-tip-container"></div>
</div>
</script>
<script>
var layer;
var colors = ['red', 'blue', 'yellow', 'brown', 'green', 'orange'];
// Custom infowindow
_.extend(cdb.geo.ui.Infowindow.prototype, {
events: function(){
return _.extend({},cdb.geo.ui.Infowindow.prototype.events,{
'click a#link': '_onLinkClick'
});
},
_onLinkClick: function(e) {
console.log(e, this.model, layer);
var i = Math.floor(Math.random() * 6);
layer.setCartoCSS('#layer { marker-fill: ' + colors[i] + ' }')
}
});
function main() {
cartodb.createVis('map', 'http://documentation.cartodb.com/api/v2/viz/7eb2096a-51d9-11e3-89a7-5404a6a683d5/viz.json', {
shareable: false,
title: false,
description: false,
search: false,
tiles_loader: true,
center_lat: 42,
center_lon: -3,
zoom: 6,
legends: false
})
.done(function(vis, layers) {
layer = layers[1];
var infowindow = layer.getSubLayer(0).infowindow;
infowindow.set('template', function() {
return $('#infowindow_template').html();
});
})
.error(function(err) {
console.log(err);
});
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment