index.html
<!DOCTYPE html>
<html>
<head>
<title>Layer selector example | 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="//cartodb.com/assets/favicon.ico" />
<style>
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
background: black;
}
</style>
<link rel="stylesheet" href="//libs.cartocdn.com/cartodb.js/v3/themes/css/cartodb.css" />
</head>
<body>
<div id="map"></div>
<script src="//libs.cartocdn.com/cartodb.js/v3/cartodb.js"></script>
<script>
var map;
function featureClick(e, pos, latlng, data){
console.log(data)
if(map.getZoom() < 7){
map.setView([data.lat,data.lng],7)
} else {
var url = "//osm.cartodb.com/api/v2/sql?format=shp&q=select the_geom,hstore_to_json(tags) as tags from osm_all WHERE the_geom %26%26 ST_SetSRID('"+data.bbox+"'::geometry, 4326)";
window.location = url;
}
}
function main() {
selectedStyle = $('li.selected').attr('id');
cartodb.createVis('map','//osm2.cartodb.com/api/v2/viz/cf26d670-009b-11e4-830e-0e73339ffa50/viz.json')
.done(function(vis, layers) {
map = vis.getNativeMap();
layer = layers[1].getSubLayer(0);
layer.set({sql: 'SELECT cartodb_id, the_geom, area_sqkm, the_geom_webmercator, ST_ASText(the_geom) bbox, ST_X(ST_Centroid(the_geom)) lng, ST_Y(ST_Centroid(the_geom)) lat FROM ne_10m_urban_areas_bbox ORDER BY area_sqkm DESC',interactivity: 'cartodb_id, bbox, lat, lng'})
layer.setInteraction(true);
layer.on('featureClick', featureClick);
})
.error(function(err) {
console.log(err);
});
}
window.onload = main;
</script>
</body>
</html>