forked from miccferr/turf-tutorial-parchimetri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
185 lines (156 loc) · 5.54 KB
/
index.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<!-- jquery -->
<script src='bower_components/jquery/dist/jquery.min.js'></script>
<!-- mapbox -->
<script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.6/mapbox.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.1.6/mapbox.css' rel='stylesheet' />
<!-- bootstrap -->
<!-- <script src='bower_components/bootstrap/dist/js/bootstrap.min.js'></script> -->
<link href='bower_components/bootstrap/dist/css/bootstrap.min.css' rel='stylesheet' />
<!-- leaflet -->
<!-- <script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" /> -->
<!-- turf -->
<script src="http://api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js" /></script>
<!-- ominvore -->
<script src='script/leaflet-omnivore.min.js'></script>
<!-- topojson -->
<!-- <script src="topojson.js"></script> -->
<style type="text/css">
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
#attiva-filtro {
position: absolute;
top: 20px;
right: 10px;
z-index: 1;
background-color:#ffb2c2;
color:#fff;
font-size:1.2em;
}
#attiva-filtro:hover,
#attiva-filtro a:active {
position: absolute;
top: 20px;
right: 10px;
z-index: 1;
background-color: #ff325e;
color:#fff;
font-size:1.2em;
}
</style>
</head>
<body>
<a id='attiva-filtro' type="button" class="btn btn-default">Filtro</a>
<div id="map"></div>
<script>
// Provide your access token: grazie Zedda!
L.mapbox.accessToken = 'pk.eyJ1IjoiYW5kcmlhIiwiYSI6InVkRWQtdWMifQ.PptXHNGHheaDE-EM-Lob9g';
var map = L.map('map').setView([44.4948,11.3441], 16);
// Attribution Link
mapLink =
'<a href="http://openstreetmap.org">OpenStreetMap</a>';
L.tileLayer(
'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© ' + mapLink + ' Contributors',
maxZoom: 18,
}).addTo(map);
// definiamo dei layer vuoti
var bufferLayer = L.mapbox.featureLayer();
var nearest_fc = L.mapbox.featureLayer();
var fc
// creo un marker
var marker = L.marker(new L.LatLng( 44.49528086931113,11.347181797027588), {
icon: L.mapbox.marker.icon({
'marker-color': 'ff8888',
'marker-symbol':'pitch'
}),
draggable:'true'
});
// per cambiare eventualmente icona al marker
// marker.setIcon( L.icon({'iconUrl': 'data/SOD_14_pasta.png'}))
// carico i dati
var rastrelliere = omnivore.kml('data/rastrelliere.kml').addTo(map);
// Buffer dinamico attorno al marker
function pointBuffer (pt, radius, units, resolution) {
var ring = []
var resMultiple = 360/resolution;
for(var i = 0; i < resolution; i++) {
var spoke = turf.destination(pt, radius, i*resMultiple, units);
ring.push(spoke.geometry.coordinates);
}
if((ring[0][0] !== ring[ring.length-1][0]) && (ring[0][1] != ring[ring.length-1][1])) {
ring.push([ring[0][0], ring[0][1]]);
}
return turf.polygon([ring])
}
// get position, draw buffer, find within, find nearest, add to map
function updateVenues(fc){
// converto in GeoJSON il livello
fc= fc.toGeoJSON()
// prendo la posizione
var position=marker.getLatLng();
// creo un oggetto turf con la posizione
var point=turf.point([position.lng, position.lat]);
// calcolo un buffer attorno al punto
var buffer = pointBuffer(point, 1, 'kilometers', 120);
buffer.properties = {
"fill": "#00704A",
"fill-opacity":0.1,
"stroke": "#00704A",
"stroke-width": 2,
"stroke-opacity": 0.5
};
// creo il livello con il buffer
bufferLayer.setGeoJSON(buffer);
// filtro le features (rastrelliere delle biciclette)
var within = turf.featurecollection(fc.features.filter(function (shop){
if (turf.distance(shop, point, 'kilometers') <= 1) return true;
})
);
// coloro le features
within.features.forEach(function(feature){
feature.properties["marker-color"] = "C73E3E";
feature.properties["title"] = '<br>'+feature.properties["name"]+'<br>Bikes: '+feature.properties["nbBikes"]+'<br>Empty Docks: '+feature.properties["nbEmptyDocks"];
feature.properties["marker-size"] = "small";
feature.properties["marker-symbol"] = "bicycle";
});
// calcolo la feature più vicina
nearest = turf.nearest(point, within);
// coloro la feature più vicina
nearest.properties["marker-color"] = "3ad78d";
nearest.properties["title"] = '<br>'+nearest.properties["name"]+'<br>Bikes: '+nearest.properties["nbBikes"]+'<br>Empty Docks: '+nearest.properties["nbEmptyDocks"];
nearest.properties["marker-size"] = "large";
nearest.properties["marker-symbol"] = "bicycle";
// creo un unica collezione di feature colorate (la più vicina + le rimanenti)
nearest_fc.setGeoJSON(turf.featurecollection([within, nearest])).addTo(map);
};
// Funzione per gestire l'evento 'clicca' sul bottone 'Filtra'
$('#attiva-filtro').click(function () {
if ($(this).hasClass('active')){
console.log('spens');
$(this).removeClass('active');
map.removeLayer(marker);
map.removeLayer(bufferLayer);
map.removeLayer(nearest_fc);
rastrelliere.addTo(map);
}else{
$(this).addClass('active');
updateVenues(rastrelliere);
marker.addTo(map);
bufferLayer.addTo(map);
map.removeLayer(rastrelliere);
}
});
// ogni volta che muovo il marker lancio la funzione per calcolare/colorare ecc...
marker.on('drag', function(){
updateVenues(rastrelliere);
});
</script>
</body>
</html>