-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUsingZipApi-1-getCityList.html
executable file
·217 lines (178 loc) · 7.97 KB
/
UsingZipApi-1-getCityList.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
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>hi</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
var sGeobytesLocationCode = "";
var sGeobytesIsLocationMatch = false;
var sGeobytesCountry = "";
var sGeobytesCity = "";
var uniqueCities=[];
console.log();
jQuery.getJSON(
"http://getnearbycities.geobytes.com/GetNearbyCities?callback=?&radius=100&limit=25&locationcode=" + sGeobytesLocationCode,
function (data) {
drawNbcTable(data);
//console.log("data from jquery.GetJSON is below: ");
//console.log(data);
}
);
// var cityFromZipApi="";
// var cityDistanceFromHere="";
// Some APIs will give us a cross-origin (CORS) error. This small function is a fix for that error. You can also check out the chrome extenstion (https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en).
jQuery.ajaxPrefilter(function (options) {
if (options.crossDomain && jQuery.support.cors) {
options.url = 'https://cors-anywhere.herokuapp.com/' + options.url;
}
});
jQuery.getJSON(
"https://www.zipcodeapi.com/rest/6QYBNDrLnXNMoygdHvxlKTLC0MJhdQopmngdzsCt2MCxIl9kjbL4q014piuYpqvW/radius.json/94122/65/mile",
function (zipData) {
console.log("data from ZIP API is below: ");
console.log(zipData);
drawZipApiTable(zipData);
///NOTE THIS MAY NEED TO BE REMOVED IF DOESNT WORK - EXPERIMENT TO FIND OUT IF WE CAN GET UNIQUES ARRAY
//var uniqueCities = [];
//uniqueCities = uniqueCitiesFromZipApi(zipData);
//getRandomCity(uniqueCities);
console.log("if this is printed drawZipApiTable should have ran");
console.log("BELOW: SJ");
console.log(zipData.zip_codes[0].city);
//key to object nav ^^
var zipDataArray = zipData.zip_codes;
console.log("below is zipDataArray newly made var ");
console.log(zipDataArray);
var cityList = convertCityArr(zipDataArray); //originally this line was : var cityList = convertCityArr(response);
uniqueCities = cityList.filter(unique);
console.log(uniqueCities);
var uniqueCities = cityList.filter(unique);
console.log("below print out of uniqueCitites function ");
console.log(uniqueCities);
getRandomCity(uniqueCities);
}
)
function drawNbcTable(data) {
var row = $("<tr /><th>Miles</th><th>Kms</th><th>City</th><th>Bearing</th><th>Direction</th>");
$("#geobytesnearbycities").html(row);
for (var i = 0; i < data.length; i++) {
drawNbcRow(data[i]);
}
}
function drawNbcRow(rowData) {
var row = $("<tr />");
$("#geobytesnearbycities").append(row);
row.append($("<td>" + rowData[11] + "</td>"));
row.append($("<td>" + rowData[7] + "</td>"));
row.append($("<td>" + rowData[1] + "</td>"));
row.append($("<td>" + rowData[0] + "</td>"));
row.append($("<td>" + rowData[4] + "</td>"));
}
function drawZipApiTable(zipData) {
var rowFromZipApi = $("<tr /><th>City</th><th>Distance (in miles)</th>");
console.log("now inside drawZipApiTable");
$("#zipApiCities").html(rowFromZipApi);
for (var i = 0; i < zipData.zip_codes.length; i++) {
drawZipApiRow(zipData.zip_codes[i]);
//console.log("current zipData element is");
//console.log(zipData.zip_codes[i].city);
}
}
function convertCityArr(array) {
var cities = [];
for (var i = 0; i < array.length; i++) {
var cityName = array[i].city;
cities.push(cityName)
}
return cities;
}
var unique = function (value, index, self) {
return self.indexOf(value) === index;
}
// var cityList = convertCityArr(zipData); //originally this line was : var cityList = convertCityArr(response);
// var uniqueCities = cityList.filter(unique);
// console.log("below print out of uniqueCitites function ");
// console.log(uniqueCities);
// function uniqueCitiesFromZipApi(zipData) {
// var uniqueCities = [[0, 0], [0, 0]];
// var element = [[0, 0], [0, 0]];
// if (i < 2) {
// element[i][0] = zipData.zip_codes[i].city;
// element[i][1] = zipData.zip_codes[i].distance;
// uniqueCities[i] = element;
// for (var i = 0; i < zipData.zip_codes.length; i++) {
// // if (i<2){
// // element[i][0]=zipData.zip_codes[i].city;
// // element[i][1]=zipData.zip_codes[i].distance;
// // uniqueCities[i]=element;
// if (uniqueCities[j][0] == zipData.zip_codes[i].city) { i++; }
// else {
// var elem = [[0, 0], [0, 0]];
// elem[0][0] = zipData.zip_codes[i].city;
// elem[0][1] = zipData.zip_codes[i].distance;
// uniqueCities[i] = element;
// }//closed the else
// //}//closed the IF
// }//closed the for
// }
// console.log("unique cities is below ");
// console.log(uniqueCities);
// return uniqueCities;
// }
//function to pick a random city
function getRandomCity(uniqueCities) {
//start debugging
console.log("debug stmt1: we are passing to getRandomCity func below arg: uniqueCitites");
console.log(uniqueCities);
//end debugging
var selectedCityIndex = uniqueCities[Math.floor(Math.random() * uniqueCities.length)];
//start debugging
console.log("we're creating seelctedCityIndex which is now equal to:");
console.log(selectedCityIndex);
//end debugging
console.log(uniqueCities);
//end debugging
//var selectedCity = uniqueCities[selectedCityIndex];//orig was: var selectedCity = uniqueCities[selectedCityIndex].city;
var selectedCity=selectedCityIndex;
console.log("the randomly chosen city is: ");
console.log(selectedCity);
$("#randomcity").text(selectedCity);
}
// example of zipcodes api response
// {
// "zip_codes": [
// {
// "zip_code": "95067",
// "distance": 59.953,
// "city": "Scotts Valley",
// "state": "CA"
// },
function drawZipApiRow(zipRowData) {
var zipRow = $("<tr />");
$("#zipApiCities").append(zipRow);
zipRow.append($("<td>" + zipRowData.city + "</td>"));
zipRow.append($("<td>" + zipRowData.distance + "</td>"));
console.log("drawZipApiRow just Ran ");
}
</script>
<p id="randomcity">Randomly chosen city is: </p>
<table id="geobytesnearbycities">
<tr>
<td><b>Mi</b></td>
<td><b>Km</b></td>
<td><b>City</b></td>
<td><b>Bearing</b></td>
<td><b>Direction</b></td>
</tr>
</table>
<table id="zipApiCities">
<tr>
<td><b>City</b></td>
<td><b>Distance (miles)</b></td>
</tr>
</table>
</body>
</html>