hover features in leaflet
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> | |
<title>Leaflet 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="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" /> | |
<!--[if lte IE 8]> | |
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/themes/css/cartodb.ie.css" /> | |
<![endif]--> | |
</head> | |
<body> | |
<div id="map"></div> | |
<!-- include cartodb.js library --> | |
<script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.js"></script> | |
<script> | |
function main() { | |
var map = new L.Map('map', { | |
zoomControl: false, | |
center: [43, 0], | |
zoom: 3 | |
}); | |
var polygons = {}; | |
L.tileLayer('http://tile.stamen.com/toner/{z}/{x}/{y}.png', { | |
attribution: 'Stamen' | |
}).addTo(map); | |
function geometryHover(username, map, layer, options) { | |
options = options || {} | |
var HIGHLIGHT_STYLE = { | |
weight: 3, | |
color: '#FFFFFF', | |
opacity: 1, | |
fillColor: '#FFFFFF', | |
fillOpacity: 0.3 | |
}; | |
style = options.style || HIGHLIGHT_STYLE; | |
var polygonsHighlighted = []; | |
// fetch the geometry | |
var sql = new cartodb.SQL({ user: username, format: 'geojson' }); | |
sql.execute("select cartodb_id, ST_Simplify(the_geom, 0.1) as the_geom from (" + layer.getSQL() + ") as _wrap").done(function(geojson) { | |
var features = geojson.features; | |
for(var i = 0; i < features.length; ++i) { | |
var f = geojson.features[i]; | |
var key = f.properties.cartodb_id | |
// generate geometry | |
var geo = L.GeoJSON.geometryToLayer(features[i].geometry); | |
geo.setStyle(style); | |
// add to polygons | |
polygons[key] = polygons[key] || []; | |
polygons[key].push(geo); | |
} | |
}); | |
function featureOver(e, pos, latlng, data) { | |
featureOut(); | |
var pol = polygons[data.cartodb_id] || []; | |
for(var i = 0; i < pol.length; ++i) { | |
map.addLayer(pol[i]); | |
polygonsHighlighted.push(pol[i]); | |
} | |
} | |
function featureOut() { | |
var pol = polygonsHighlighted; | |
for(var i = 0; i < pol.length; ++i) { | |
map.removeLayer(pol[i]); | |
} | |
polygonsHighlighted = []; | |
} | |
layer.on('featureOver', featureOver); | |
layer.on('featureOut', featureOut); | |
layer.setInteraction(true); | |
} | |
cartodb.createLayer(map, 'http://documentation.cartodb.com/api/v2/viz/2b13c956-e7c1-11e2-806b-5404a6a683d5/viz.json') | |
.addTo(map) | |
.on('done', function(layer) { | |
geometryHover('documentation', map, layer.getSubLayer(0)); | |
}).on('error', function() { | |
cartodb.log.log("some error occurred"); | |
}); | |
} | |
// you could use $(window).load(main); | |
window.onload = main; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You may want to add this CSS to allow the Geojson layer to propagate click events so that infowindows are shown when you click on a highlighted country: