forked from CartoDB/carto.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet_hover_features.html
111 lines (92 loc) · 3.34 KB
/
leaflet_hover_features.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<!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>