-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.html
145 lines (133 loc) · 5.42 KB
/
geo.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Batch Geocoding Tool | Get LngLat Coordinates for Places</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
<link href='https://api.mapbox.com/mapbox-assembly/v1.3.0/assembly.min.css' rel='stylesheet'>
<script src='https://api.mapbox.com/mapbox-assembly/v1.3.0/assembly.js'></script>
<style>
#results td {
white-space: nowrap;
}
</style>
</head>
<body>
<table id="results" class="table">
<thead>
<tr>
<th>name</th>
<th>longitude</th>
<th>latitude</th>
<th>street</th>
<th>city</th>
<th>state</th>
<th>zip</th>
<th>country</th>
<th>category</th>
<th>county</th>
</tr>
</thead>
</table>
<script>
// Initialize constants for the API request:
const urlBase = 'https://api.mapbox.com/geocoding/v5/mapbox.places/'
const token = 'pk.eyJ1IjoiZG9tbGV0IiwiYSI6ImNsaTN5MXhseTA0bWMzZHBoNTlnaG1hYmUifQ.9IpTpyptAxhC6ZoUqKCd9w' // Restricted token
/*
See this example `queries` array with 2 example strings:
let queries = [
'Allen+University+Columbia+South+Carolina+USA',
'Edward+Waters+University+Jacksonville+Florida+USA',
]
*/
// Use the same format as examples ^ above to add
// your strings inside the `queries` array below.
// (If characters like apostrophes cause problems, delete them.)
let queries = [
'Calbright+College+-,+California,+US',
'Compton+College+Compton,+California,+US',
'Deanza+College+Cupertino,+California,+US',
'Folsom+Lake+College+Folsom,+California,+US',
'Los+Angeles+City+College+Los+Angeles,+California,+US',
'Las+Positas+College+Livermore,+California,+US',
'Lassen+College+Susanville,+California,+US',
'Los+Angeles+Trade-Tech+College+Los+Angeles,+California,+US',
'Madera+College+Madera,+California,+US',
'Orange+Coast+College+Costa+Mesa,+California,+US',
'Palo+Verde+College+Blythe,+California,+US',
'Riverside+City+College+Riverside,+California,+US',
'Saddleback+College+Mission+Viejo,+California,+US',
'Solano+Community+College+Fairfield,+California,+US',
'Victor+Valley+College+Victorville,+California,+US',
'West+Hills+College+Coalinga+Coalinga,+California,+US',
'West+Hills+College+Lemoore+Lemoore,+California,+US',
'Butte+College+Oroville,+California,+US',
'Cañada+College+Redwood+City,+California,+US',
'College+Of+The+Canyons+Santa+Clarita,+California,+US',
]
// Define the function, including the loop:
async function makeGeoRequest(i) {
const query = await fetch(
// `https://api.mapbox.com/geocoding/v5/mapbox.places/${queries[i]}.json?language=es&limit=1&fuzzyMatch=true&access_token=pk.eyJ1IjoiZG9tbGV0IiwiYSI6ImNsanp3a24xeDAxMjMzZGxpeDY3MWZpMXkifQ.4TIVDbum8xmIdzokCod3Ww`,
`https://api.mapbox.com/geocoding/v5/mapbox.places/${queries[i]}.json?limit=1&fuzzyMatch=true&access_token=pk.eyJ1IjoiZG9tbGV0IiwiYSI6ImNsanp3a24xeDAxMjMzZGxpeDY3MWZpMXkifQ.4TIVDbum8xmIdzokCod3Ww`,
{ method: 'GET' }
);
console.log('i made an api request')
const data = await query.json();
let name = data.features[0].text // name
let lng = data.features[0].center[0] // lng
let lat = data.features[0].center[1] // lat
let street = data.features[0].properties.address // street
let city = data.features[0].context[1].text // city
let state = data.features[0].context[3].text // state
let zip = data.features[0].context[0].text // zip
let country = data.features[0].context[4].text // country
let category = data.features[0].properties.category // category
let county = data.features[0].context[2].text // county
console.log(name + lng + lat + street + city + state + zip + country + category + county)
// Create HTML elements:
let html = `<td>${name}</td>
<td>${lng}</td>
<td>${lat}</td>
<td>${street}</td>
<!-- <td>${city}</td>
<td>${state}</td>
<td>${zip}</td>
<td>${country}</td>
<td>${category}</td>
<td>${county}</td>-->`
let row = document.createElement('tr'); // create a row
resultsTable.appendChild(row); // add it to the table
let rowId = 'row' + i // create a unique id
row.setAttribute('id', rowId) // attach the unique id
document.getElementById(rowId).innerHTML = html
}
// Create the loop, and call the function:
const resultsTable = document.getElementById("results");
for (i in queries) {
makeGeoRequest(i)
}
// async function geoRequest() {
// for (i in queries) {
// item = queries[i]
// const query = await fetch(
// `${api}${item}${token}`,
// { method: 'GET' }
// );
// // // do something with the api response:
// const data = await query.json();
// for (i in data.features[0]) {
// console.log(i)
// console.log(data.features[0].i)
// }
// let text = data.features[0].text
// let address = data.features[0].properties.address
// let center = data.features[0].center
// let place_name = data.features[0].place_name
// let category = data.features[0].properties.category
// }
// }
</script>
</body>
</html>