Skip to content

Instantly share code, notes, and snippets.

@Xatpy
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save Xatpy/2bed06d2a65d85422339 to your computer and use it in GitHub Desktop.

Select an option

Save Xatpy/2bed06d2a65d85422339 to your computer and use it in GitHub Desktop.
Close infowindows clicking on the basemap
<!DOCTYPE html>
<html>
<head>
<title>Close infowindow clicking on the basemap | 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;
}
/* end */
</style>
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/themes/css/cartodb.css" />
</head>
<body>
<!-- html -->
<div id="map"></div>
<!-- end -->
<!-- include cartodb.js library -->
<script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.uncompressed.js"></script>
<script>
var infowindow;
var infowindowOpened = false;
// js
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 0 is the base layer, layer 1 is cartodb layer
var layer = layers[1];
infowindow = layer.infowindow;
infowindowOpened = false;
$('#map').click(closeInfowindow);
layer.on('featureClick',function(e, latlng, pos, data, subLayerIndex) {
infowindowOpened = true;
});
})
.error(function(err) {
console.log(err);
});
}
function closeInfowindow() {
setTimeout(function() {
if (!infowindowOpened) {
infowindow.set('visibility', false);
}
infowindowOpened = false;
}, 250);
}
window.onload = main;
// end
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment