-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaptest.html
290 lines (235 loc) · 8.18 KB
/
maptest.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<!DOCTYPE html>
<html>
<head>
<title>Merit Badge Counselor Coverage</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin=""/>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA==" crossorigin=""></script>
<script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-3.5.1.js"></script>
<style>
.leaflet-mboverlay-pane {opacity: 0.2}
</style>
</head>
<body>
<div id="mapid" style="width: 98vw; height: 98vh;"></div>
<script>
var mapGeoJSON = // {{include: map.json}}
var roads = // {{include: roads.json}}
var units = [
// {{include: units.json}}
];
var data =[
// {{include: live-mbc.json filter:(^\s*\{\s*$|^\s*\},\s*$|zip:|meritbadges:|workwith:|availability:|yptexpiry:)}}
];
var zipData = {
// {{include: demo-zip.json}}
};
var requiredMBs = [
"Camping",
"Citizenship in the Community",
"Citizenship in the Nation",
"Citizenship in the World",
"Communication",
"Cooking",
"Cycling",
"Emergency Preparedness",
"Environmental Science",
"Family Life",
"First Aid",
"Hiking",
"Lifesaving",
"Personal Fitness",
"Personal Management",
"Sustainability",
"Swimming"
];
var layerColor = {
"Camping": 'Green',
"Cooking": 'YellowGreen',
"Cycling": 'Lime',
"Hiking": 'DarkGreen',
"Citizenship in the Community": 'Blue',
"Citizenship in the Nation": 'RoyalBlue',
"Citizenship in the World": 'SkyBlue',
"Communication": 'Cyan',
"Environmental Science": 'SaddleBrown',
"Sustainability": 'Tan',
"Emergency Preparedness": 'Salmon',
"First Aid": 'Red',
"Lifesaving": 'IndianRed',
"Swimming": 'DarkRed',
"Family Life": 'BlueViolet',
"Personal Fitness": 'Violet',
"Personal Management": 'MediumOrchid',
}
// the following is lifted from the DataTables source (MIT license)
// Escape regular expression special characters
var _re_escape_regex = new RegExp( '(\\' + [ '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\', '$', '^', '-' ].join('|\\') + ')', 'g' );
/**
* Escape a string such that it can be used in a regular expression
*
* @param {string} val string to escape
* @returns {string} escaped string
*/
function escapeRegex( val ) {
return val.replace( _re_escape_regex, '\\$1' );
}
/**
* Build a regular expression object suitable for searching a table
* @param {string} sSearch string to search for
* @param {bool} bRegex treat as a regular expression or not
* @param {bool} bSmart perform smart filtering or not
* @param {bool} bCaseInsensitive Do case insensitive matching or not
* @returns {RegExp} constructed object
* @memberof DataTable#oApi
*/
function CreateSearchRegExp( search, regex, smart, caseInsensitive )
{
search = regex ?
search :
escapeRegex( search );
if ( smart ) {
/* For smart filtering we want to allow the search to work regardless of
* word order. We also want double quoted text to be preserved, so word
* order is important - a la google. So this is what we want to
* generate:
*
* ^(?=.*?\bone\b)(?=.*?\btwo three\b)(?=.*?\bfour\b).*$
*/
var a = $.map( search.match( /"[^"]+"|[^ ]+/g ) || [''], function ( word ) {
if ( word.charAt(0) === '"' ) {
var m = word.match( /^"(.*)"$/ );
word = m ? m[1] : word;
}
return word.replace('"', '');
} );
search = '^(?=.*?'+a.join( ')(?=.*?' )+').*$';
}
return new RegExp( search, caseInsensitive ? 'i' : '' );
}
var baseMap = L.geoJSON(mapGeoJSON, {color: 'grey', fillOpacity: 0});
var roadMap = L.geoJSON(roads, {color: 'black', opacity: 1, fillOpacity: 1});
var mymap = L.map('mapid',
{
//center: [39.8, -111.24],
//zoom: 7,
layers: [baseMap]
}).fitBounds([[43.463,-114.038],[37.003,-107.501]]);
var overlays = {
"Roads": roadMap
};
// add all the units to the map as points
mymap.createPane('unitoverlay', mymap.getPane('overlay-pane'));
// step through all the units
var circles = [];
units.forEach(function (unit) {
// create a tiny circle for each unit
var circle = L.circle([unit.lat, unit.lon], {
color: 'white',
fillColor: 'red',
fillOpacity: 1,
opacity: 0,
radius: 1000,
pane: 'unitoverlay',
});
// circle.bindPopup("I am a circle.");
circles.push(circle);
});
// add a layer group for the units
var unitOverlay = L.layerGroup(circles);
overlays[`<span style='color: red'>Units</span>`] = unitOverlay;
// turn on the units layer by default
mymap.addLayer(unitOverlay);
var radius = 1609 * 15; // 15 miles
var opacity = 0.1;
mymap.createPane('mboverlay', mymap.getPane('overlay-pane'));
// step through all the required MBs
requiredMBs.forEach(function (mb) {
// make a list (set) of all zip codes
var ziplist = new Set();
data.forEach(function (person) {
if (person.meritbadges.includes(mb) &&
person.availability == 'Available' &&
(Date.parse(person.yptexpiry) - Date.now()) > 0
)
ziplist.add(person.zip.substr(0,5));
});
// create a circle for each zip code
var circles = [];
ziplist.forEach(function (zip) {
// valid zip
if (zipData.hasOwnProperty(zip))
{
var circle = L.circle([zipData[zip].lat, zipData[zip].lon], {
color: 'white',
fillColor: layerColor[mb],
fillOpacity: 1,
opacity: 0,
radius: radius,
pane: 'mboverlay',
});
// circle.bindPopup("I am a circle.");
circles.push(circle);
}
}); // ziplist.forEach
// add a layer group for this MB to the overlays
overlays[`<span style='color: ${layerColor[mb]}'>${mb}</span>`] = L.layerGroup(circles);
}); // requiredMBs.forEach
var otherLayerColors = {mbother0: 'Black', mbother1: 'DeepPink', mbother2: 'DarkMagenta'};
var othersLayerGroup = [];
for (var i = 0; i < 3; i++)
{
var lg = L.layerGroup();
othersLayerGroup['mbother'+i] = lg;
overlays[`<span style="color: ${otherLayerColors['mbother'+i]}">Other <input class="mbother" type="text" id="mbother${i}" placeholder="smart search"/></span>`] = lg;
}
L.control.layers(null, overlays).addTo(mymap);
$('input.mbother').on('change keyup paste', function(event) {
var mb = $(this).val();
var target = event.target.id;
// rebuild layer
othersLayerGroup[target].clearLayers();
if (mb.length < 3 || mb.search(/\S/) == null)
return;
// make a list (set) of all zip codes
var ziplist = new Set();
var mbregex = CreateSearchRegExp( mb, false, true, true );
data.forEach(function (person) {
if (person.availability == 'Available' &&
(Date.parse(person.yptexpiry) - Date.now()) > 0
)
person.meritbadges.forEach( function (pmb) {
if (mbregex.test(pmb))
ziplist.add(person.zip.substr(0,5));
});
});
// create a circle for each zip code
ziplist.forEach(function (zip) {
// valid zip
if (zipData.hasOwnProperty(zip))
{
var circle = L.circle([zipData[zip].lat, zipData[zip].lon], {
color: 'white',
fillColor: otherLayerColors[target],
fillOpacity: 1,
opacity: 0,
radius: radius,
pane: 'mboverlay',
});
othersLayerGroup[target].addLayer(circle);
}
}); // ziplist.forEach
});
var popup = L.popup();
// https://www.google.com/maps/@40.2548192,-111.7137328,14z
function onMapClick(e) {
popup
.setLatLng(e.latlng)
.setContent(`Location <a href="https://www.google.com/maps/@${e.latlng.lat.toFixed(5)},${e.latlng.lng.toFixed(5)},${mymap.getZoom()}z" target="_blank">${e.latlng.lat.toFixed(3)} ${e.latlng.lng.toFixed(3)}</a>`)
.openOn(mymap);
}
mymap.on('click', onMapClick);
</script>
</body>
</html>