forked from jcontini/facebook-friends-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate-map.html
254 lines (218 loc) · 6.86 KB
/
template-map.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Friends on a map</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<!-- Credit to https://docs.mapbox.com/mapbox-gl-js/example/filter-features-within-map-view/ for this code -->
<style>
#map {
position:absolute;
left:25%;
top:0;
bottom:0;
width: 75%;
}
.map-overlay {
position: absolute;
width: 25%;
top: 0;
bottom: 0;
left: 0;
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
background-color: #fff;
max-height: 100%;
overflow: hidden;
}
.map-overlay fieldset {
display: none;
background: #ddd;
border: none;
padding: 10px;
margin: 0;
}
.map-overlay input {
display: block;
border: none;
width: 100%;
border-radius: 3px;
padding: 10px;
margin: 0;
}
.map-overlay .listing {
overflow: auto;
max-height: 100%;
}
.map-overlay .listing > * {
display: block;
padding: 5px 10px;
margin: 0;
}
.map-overlay .listing a {
border-bottom: 1px solid rgba(0, 0, 0, 0.1);
color: black;
text-decoration: none;
}
.map-overlay .listing a:last-child {
border: none;
}
.map-overlay .listing a:hover {
background: #f0f0f0;
}
</style>
<div id='map'></div>
<div class='map-overlay'>
<fieldset>
<input id='feature-filter' type='text' placeholder='Filter results by name' />
</fieldset>
<div id='feature-listing' class='listing'></div>
</div>
<script>
mapboxgl.accessToken = '{{mapbox_token}}';
var map = new mapboxgl.Map({
container: 'map',
attributionControl: false,
style: 'mapbox://styles/mapbox/light-v10?optimize=true',
center: [-95, 40],
zoom: 3,
pitch: 0,
bearing: 0,
scrollZoom: true,
touchZoom: true,
doubleClickZoom: true,
boxZoom: true,
preserveDrawingBuffer: false
});
// Holds visible friend features for filtering
var friends = [];
// Create a popup, but don't add it to the map yet.
var popup = new mapboxgl.Popup({
closeButton: false
});
var filterEl = document.getElementById('feature-filter');
var listingEl = document.getElementById('feature-listing');
function renderListings(features) {
// Clear any existing listings
listingEl.innerHTML = '';
if (features.length) {
features.forEach(function(feature) {
var prop = feature.properties;
var item = document.createElement('a');
item.href = 'https://www.facebook.com/profile.php?id=' + prop.id;
item.target = '_blank';
item.textContent = prop.name + ' (' + prop.location + ')';
item.addEventListener('mouseover', function() {
// Highlight corresponding feature on the map
popup.setLngLat(feature.geometry.coordinates)
.setText(feature.properties.name + ' (' + feature.properties.location + ')')
.addTo(map);
});
listingEl.appendChild(item);
});
// Show the filter input
filterEl.parentNode.style.display = 'block';
} else {
var empty = document.createElement('p');
empty.textContent = 'Drag the map to populate results';
listingEl.appendChild(empty);
// Hide the filter input
filterEl.parentNode.style.display = 'none';
// remove features filter
map.setFilter('friend', ['has', 'location']);
}
}
function normalize(string) {
return string.trim().toLowerCase();
}
function getUniqueFeatures(array, comparatorProperty) {
var existingFeatureKeys = {};
// Because features come from tiled vector data, feature geometries may be split
// or duplicated across tile boundaries and, as a result, features may appear
// multiple times in query results.
var uniqueFeatures = array.filter(function(el) {
if (existingFeatureKeys[el.properties[comparatorProperty]]) {
return false;
} else {
existingFeatureKeys[el.properties[comparatorProperty]] = true;
return true;
}
});
return uniqueFeatures;
}
map.on('style.load', function() {
map.addSource("data", {
"type": "geojson",
"data": datapoints,
"buffer": 1,
"maxzoom": 14,
"generateId": true
});
map.addLayer({
"id": "friend",
"source": "data",
"type": "circle",
"paint": {
"circle-color": "blue",
"circle-radius": 5,
"circle-opacity": 1,
}
});
map.on('moveend', function() {
var features = map.queryRenderedFeatures({layers:['friend']});
if (features) {
var uniqueFeatures = getUniqueFeatures(features, "id");
// Populate features for the listing overlay.
renderListings(uniqueFeatures);
// Clear the input container
filterEl.value = '';
// Store the current features in sn `friends` variable to
// later use for filtering on `keyup`.
friends = uniqueFeatures;
}
});
map.on('mousemove', 'friend', function(e) {
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = 'pointer';
// Populate the popup and set its coordinates based on the feature.
var feature = e.features[0];
popup.setLngLat(feature.geometry.coordinates)
.setText(feature.properties.name + ' (' + feature.properties.location + ')')
.addTo(map);
});
map.on('mouseleave', 'friend', function() {
map.getCanvas().style.cursor = '';
popup.remove();
});
filterEl.addEventListener('keyup', function(e) {
var value = normalize(e.target.value);
// Filter visible features that don't match the input value.
var filtered = friends.filter(function(feature) {
var name = normalize(feature.properties.name);
var code = normalize(feature.properties.location);
return name.indexOf(value) > -1 || code.indexOf(value) > -1;
});
// Populate the sidebar with filtered results
renderListings(filtered);
// Set the filter to populate features into the layer.
map.setFilter('friend', ['match', ['get', 'location'], filtered.map(function(feature) {
return feature.properties.location;
}), true, false]);
});
// Call this function on initialization
// passing an empty array to render an empty state
renderListings([]);
});
</script>
<script>
var datapoints = {{datapoints}}
</script>
</body>
</html>