cartodb-4.42/lib/assets/javascripts/cdb/examples/leaflet_hover_features.html
2024-04-06 05:25:13 +00:00

109 lines
3.2 KiB
HTML

<!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="https://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" />
</head>
<body>
<div id="map"></div>
<!-- include cartodb.js library -->
<script src="https://libs.cartocdn.com/cartodb.js/v3/3.15/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.getQuery() + ") 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, latlng, pos, 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>