-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
385 lines (338 loc) · 11.1 KB
/
map.js
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// map.js
let map, markerClusterGroup, heatmapLayer, votingLocationsLayer, votingLocationsControl, districtLayer, countyLayer;
// Initialize map configurations
const mapConfig = {
center: config.MAP_CENTER,
zoom: config.MAP_ZOOM,
maxZoom: 18,
minZoom: 5
};
// District data configuration
const districtData = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "McAllen District 1",
"style": { "color": "#FF0000", "weight": 2, "opacity": 0.7, "fillOpacity": 0.2 }
},
"geometry": {
"type": "Polygon",
"coordinates": [[[-98.2235, 26.2815], [-98.2392, 26.2814], [-98.2392, 26.2627], [-98.2235, 26.2627], [-98.2235, 26.2815]]]
}
},
{
"type": "Feature",
"properties": {
"name": "McAllen District 2",
"style": { "color": "#00FF00", "weight": 2, "opacity": 0.7, "fillOpacity": 0.2 }
},
"geometry": {
"type": "Polygon",
"coordinates": [[[-98.2392, 26.2814], [-98.2571, 26.2814], [-98.2571, 26.2734], [-98.2392, 26.2734], [-98.2392, 26.2814]]]
}
},
{
"type": "Feature",
"properties": {
"name": "McAllen District 3",
"style": { "color": "#808080", "weight": 2, "opacity": 0.7, "fillOpacity": 0.2 }
},
"geometry": {
"type": "Polygon",
"coordinates": [[[-98.2235, 26.2627], [-98.2392, 26.2627], [-98.2392, 26.1896], [-98.2235, 26.1896], [-98.2235, 26.2627]]]
}
},
{
"type": "Feature",
"properties": {
"name": "McAllen District 4",
"style": { "color": "#0000FF", "weight": 2, "opacity": 0.7, "fillOpacity": 0.2 }
},
"geometry": {
"type": "Polygon",
"coordinates": [[[-98.2392, 26.2165], [-98.2701, 26.2165], [-98.2701, 26.1896], [-98.2392, 26.1896], [-98.2392, 26.2165]]]
}
},
{
"type": "Feature",
"properties": {
"name": "McAllen District 5",
"style": { "color": "#800080", "weight": 2, "opacity": 0.7, "fillOpacity": 0.2 }
},
"geometry": {
"type": "Polygon",
"coordinates": [[[-98.2483, 26.2734], [-98.2571, 26.2734], [-98.2571, 26.2165], [-98.2483, 26.2165], [-98.2483, 26.2734]]]
}
},
{
"type": "Feature",
"properties": {
"name": "McAllen District 6",
"style": { "color": "#FFFF00", "weight": 2, "opacity": 0.7, "fillOpacity": 0.2 }
},
"geometry": {
"type": "Polygon",
"coordinates": [[[-98.2392, 26.2627], [-98.2483, 26.2627], [-98.2483, 26.2165], [-98.2392, 26.2165], [-98.2392, 26.2627]]]
}
}
]
};
function initMap() {
// Initialize map
map = L.map('map', mapConfig);
// Create custom icon using Font Awesome
var customIcon = L.divIcon({
html: '<i class="fa-solid fa-flag-usa"></i>',
iconSize: [32, 32],
className: 'custom-div-icon'
});
// Set as default icon for all markers
L.Marker.prototype.options.icon = customIcon;
// Add base tile layer
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: '© OpenStreetMap contributors | DL-R23'
}).addTo(map);
addGeolocationControl()
// Initialize layers
initializeLayers();
// Add controls
addControls();
// Add event listeners
addEventListeners();
// Load data
loadData();
}
function initializeLayers() {
// Create marker cluster group
markerClusterGroup = L.markerClusterGroup({
disableClusteringAtZoom: 17,
spiderfyOnMaxZoom: true,
maxClusterRadius: 20,
chunkedLoading: true,
zoomToBoundsOnClick: true,
showCoverageOnHover: false,
removeOutsideVisibleBounds: true,
animate: false,
spiderfyDistanceMultiplier: 2.0,
singleMarkerMode: true,
iconCreateFunction: createClusterIcon
});
// Initialize heatmap layer
heatmapLayer = L.heatLayer([], {
radius: config.HEATMAP_RADIUS,
blur: config.HEATMAP_BLUR,
maxZoom: config.HEATMAP_MAX_ZOOM,
max: 1.0,
minOpacity: 0.1,
maxOpacity: 0.6
});
// Initialize voting locations layer
votingLocationsLayer = L.layerGroup();
// Initialize district layer
districtLayer = L.layerGroup();
// Initialize county layer
countyLayer = L.layerGroup();
}
function addControls() {
// Add layer control with all layers
votingLocationsControl = L.control.layers(
// Base layers (null if none)
null,
// Overlay layers
{
"Voting Locations": votingLocationsLayer,
"District Boundaries": districtLayer,
"County Boundaries": countyLayer,
"Heat Map": heatmapLayer,
// "Clusters": markerClusterGroup
},
// Options
{
collapsed: false,
position: 'topright'
}
);
votingLocationsControl.addTo(map);
}
function addEventListeners() {
// Map zoom event
map.on('zoomend', updateMapView);
}
function loadData() {
// Add districts to map
addDistrictsToMap();
// Add county boundaries
loadCountyBoundaries();
// Load voting locations
loadVotingLocations();
}
function addDistrictsToMap() {
L.geoJSON(districtData, {
style: feature => feature.properties.style,
onEachFeature: (feature, layer) => {
layer.bindPopup(feature.properties.name);
districtLayer.addLayer(layer);
}
});
// Add district layer but keep it hidden initially
districtLayer.addTo(map);
map.removeLayer(districtLayer);
}
function updateMapView() {
const currentZoom = map.getZoom();
if (currentZoom > config.HEATMAP_MAX_ZOOM) {
map.removeLayer(heatmapLayer);
map.addLayer(markerClusterGroup);
} else {
map.removeLayer(markerClusterGroup);
map.addLayer(heatmapLayer);
}
}
function loadVotingLocations() {
let heatmapData = [];
fetch('data/voting_locations.json')
.then(response => response.json())
.then(locations => {
if (!Array.isArray(locations)) {
console.error('Expected array of locations');
return;
}
locations.forEach(location => {
// Add to heatmap data
heatmapData.push([
location.latitude,
location.longitude,
location.total_votes / 1000 // Normalize the intensity
]);
const marker = L.marker([location.latitude, location.longitude])
.bindPopup(`
<strong>${location.location}</strong><br>
Address: ${location.address}<br>
City: ${location.city}<br>
Voting Area: ${location.voting_area}<br>
Total Votes: ${location.total_votes}
`);
votingLocationsLayer.addLayer(marker);
markerClusterGroup.addLayer(marker.clone());
});
votingLocationsLayer.addTo(map);
heatmapLayer.setLatLngs(heatmapData);
// Initial view based on zoom level
updateMapView();
})
.catch(error => console.error('Error loading voting locations:', error));
}
function createClusterIcon(cluster) {
const childCount = cluster.getChildCount();
let c = ' marker-cluster-';
c += childCount < 5 ? 'small' : childCount < 25 ? 'medium' : 'large';
return new L.DivIcon({
html: '<div><span>' + childCount + '</span></div>',
className: 'marker-cluster' + c,
iconSize: new L.Point(40, 40)
});
}
function addCustomMarker(latlng) {
const customPinIcon = L.divIcon({
className: 'custom-pin-icon',
html: `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="#007bff">
<path d="M12 0C7.31 0 3.5 3.81 3.5 8.5C3.5 14.88 12 24 12 24S20.5 14.88 20.5 8.5C20.5 3.81 16.69 0 12 0ZM12 13C9.79 13 8 11.21 8 9C8 6.79 9.79 5 12 5C14.21 5 16 6.79 16 9C16 11.21 14.21 13 12 13Z"/>
</svg>
`,
iconSize: [40, 40],
iconAnchor: [20, 40],
popupAnchor: [0, -40]
});
return L.marker(latlng, { icon: customPinIcon }).addTo(map);
}
function addGeolocationControl() {
const geolocationButton = document.getElementById('geolocation-button');
if (geolocationButton) {
geolocationButton.addEventListener('click', centerMapOnUserLocation);
}
}
function centerMapOnUserLocation() {
if ("geolocation" in navigator) {
const geolocationButton = document.getElementById('geolocation-button');
geolocationButton.classList.add('loading');
geolocationButton.disabled = true;
navigator.geolocation.getCurrentPosition(function(position) {
const lat = position.coords.latitude;
const lng = position.coords.longitude;
map.setView([lat, lng], 16);
const userLocationMarker = L.marker([lat, lng], {
icon: L.divIcon({
className: 'user-location-marker',
html: '<div style="background-color: #4285F4; width: 16px; height: 16px; border-radius: 50%; border: 3px solid white;"></div>',
iconSize: [22, 22],
iconAnchor: [11, 11]
})
}).addTo(map);
userLocationMarker.bindPopup("You are here").openPopup();
showNearbyLocations([lat, lng]);
reverseGeocode(lat, lng);
geolocationButton.classList.remove('loading');
geolocationButton.disabled = false;
}, function(error) {
console.error("Error getting user location:", error);
alert("Unable to get your location. Please make sure you've granted permission to access your location.");
geolocationButton.classList.remove('loading');
geolocationButton.disabled = false;
}, {
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0
});
} else {
alert("Geolocation is not supported by your browser.");
}
}
function reverseGeocode(lat, lng) {
const geocoder = new google.maps.Geocoder();
const latlng = { lat: lat, lng: lng };
geocoder.geocode({ location: latlng }, (results, status) => {
if (status === "OK") {
if (results[0]) {
const address = results[0].formatted_address;
document.getElementById('search-input').value = address;
} else {
console.log("No results found");
}
} else {
console.log("Geocoder failed due to: " + status);
}
});
}
function loadCountyBoundaries() {
fetch('data/tx-county-outlines.json')
.then(response => response.json())
.then(data => {
L.geoJSON(data, {
style: {
color: '#666',
weight: 2,
opacity: 0.7,
fillOpacity: 0,
dashArray: '5, 5' // Creates a dashed line
},
onEachFeature: (feature, layer) => {
if (feature.properties && feature.properties.CNTY_NM) {
layer.bindPopup(feature.properties.CNTY_NM);
}
countyLayer.addLayer(layer);
}
});
// Add county layer but keep it hidden initially
countyLayer.addTo(map);
map.removeLayer(countyLayer);
})
.catch(error => console.error('Error loading county boundaries:', error));
}
// Initialize map when DOM is loaded
document.addEventListener('DOMContentLoaded', initMap);
// Export for global access
window.initMap = initMap;