-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap.js
More file actions
89 lines (77 loc) · 3.03 KB
/
map.js
File metadata and controls
89 lines (77 loc) · 3.03 KB
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
// window.addEventListener('DOMContentLoaded', myFunction, false)
// function myFunction() {
// Declare zipArray in a higher scope
import fs from 'fs'
var zipArray = []
// Make an API request to fetch your statistical data from the server
fetch('http://localhost:3000/getData/winResults/zipcodes')
.then((response) => {
if (!response.ok) {
throw new Error('Network response was not ok')
}
return response.json()
})
.then((data) => {
// Check if the response data is an array
if (!Array.isArray(data)) {
console.error('API response data is not an array.')
return
}
// Push the zipcodes into zipArray
zipArray = zipArray.concat(data)
// Call a function or perform operations that depend on zipArray here
// For example, you can call processJsonArray or do something else
// Continue with your other code here
const apiKey = 'AIzaSyCg8cry2Qy-Hgn9c9eEMRjoZeSqsjk4ymc'
// Initialize an array to store coordinates
var coordinates = []
// Loop through zipArray and make requests for each zipcode
zipArray.forEach((zipcode) => {
fetch(
`https://maps.googleapis.com/maps/api/geocode/json?address=${zipcode}&key=${apiKey}`
)
.then((response) => response.json())
.then((data) => {
if (data.status === 'OK' && data.results.length > 0) {
const result = data.results[0] // Assuming you want the first result
if (result.geometry && result.geometry.bounds) {
const northeast = result.geometry.bounds.northeast
if (northeast) {
const { lat, lng } = northeast
coordinates.push({ lat, lng })
}
}
console.log(coordinates)
}
})
.catch((error) => {
console.error(`Error fetching data for zipcode ${zipcode}:`, error)
})
.finally(() => {
// Check if all fetch requests are completed
if (coordinates.length === zipArray.length) {
console.log(coordinates)
// Continue with your code that depends on the coordinates array
}
const jsonData = JSON.stringify(coordinates)
fs.writeFileSync('coordinates.json', jsonData, 'utf-8')
})
})
// Initialize the map
// var map = L.map('map').setView([28.241, -83.183], 7)
// // Add a base map layer (e.g., OpenStreetMap)
// L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
// attribution:
// '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
// }).addTo(map)
// // Add markers for each coordinate
// var markers = []
// coordinates.forEach((coord) => {
// var marker = L.marker([coord.lat, coord.lng]).addTo(map)
// markers.push(marker)
// })
// // Create a marker cluster group for better performance
// var markerCluster = L.markerClusterGroup()
// markerCluster.addLayers(markers)
// map.addLayer(markerCluster)
})