-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
146 lines (108 loc) · 4.58 KB
/
index.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
<html lang="pt-br">
<head>
<meta charset="utf-8">
<title>Worldwide Postal-Code-to-Location Converter</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link rel="icon" type="image/png" href="/logo.png" sizes="25x25">
<script>
function convertPostalCodeToLocation(postalCode, countryCode)
{
postalCode = postalCode.replaceAll(/\s/g, "") // removes white spaces
postalCode = postalCode.replaceAll(/-/g, "") // removes dashes
postalCode = postalCode.replaceAll(/\./g, "") // removes dots
const apikey = "16f2f800-f870-11ec-9299-4d575d01240d"
const URL = "https://app.zipcodebase.com/api/v1/search?apikey="+apikey+"&codes="+postalCode+"&country="+countryCode
let xmlHttp = new XMLHttpRequest()
xmlHttp.open('GET', URL);
xmlHttp.onreadystatechange = () => {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
let JSONLocationText = xmlHttp.responseText
let JSONLocationObject = JSON.parse(JSONLocationText)
const nestedArrays = ['results', postalCode, 0];
const nestedValues = nestedArrays.reduce((initialValue, currentValue) => initialValue[currentValue], JSONLocationObject);
if(nestedValues.province === null)
{
document.getElementById('city_county').value = nestedValues.city
}
else
{
document.getElementById('city_county').value = nestedValues.province
}
if((nestedValues.state_en === null || nestedValues.state_en === "") && (nestedValues.state === null || nestedValues.state === ""))
{
document.getElementById('state_province').value = 'Not Applicable'
}
else
{
if(nestedValues.state === null || nestedValues.state === "")
{
document.getElementById('state_province').value = nestedValues.state_en
}
else
{
document.getElementById('state_province').value = nestedValues.state
}
}
document.getElementById('latitude').value = nestedValues.latitude
document.getElementById('longitude').value = nestedValues.longitude
console.log(nestedValues)
}
}
xmlHttp.send()
}
</script>
</head>
<body>
<nav class="navbar navbar-light mb-4 projectNavBar">
<div class="container">
<div class="navbar-brand mb-0 h1">
<img src="logo.png">
<span id="projectTitle">Worldwide Postal-Code-to-Location Converter</span>
</div>
</div>
</nav>
<div class="container">
<div class="row form-group">
<div class="col-sm-4">
<span class="inputLabel">Enter Postal Code</span></span>
<input type="text" class="form-control inputField" placeholder="Example: 10005" name="postalCode" id="postalCode">
</div>
<div class="col-sm-4">
<span class="inputLabel">Enter Country Code</span><a class="label" href="https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements" target="_blank" tabindex="-1"> (Reference List)</a>
<input type="text" class="form-control inputField" placeholder='Example: US' name="countryCode" id="countryCode">
</div>
<div class="col-sm-2">
<div id="convertButton">
<button class="btn btn-success" onclick="convertPostalCodeToLocation(document.getElementById('postalCode').value,document.getElementById('countryCode').value)"><strong>Convert</strong></button>
</div>
</div>
</div>
<br>
<div class="row form-group">
<div class="col-sm-3">
<span class="resultLabelTitle">Results</span>
</div>
</div>
<div class="row form-group">
<div class="col-sm-2">
<span class="resultLabel">City/County</span>
<input readonly type="text" class="form-control" name="city_county" id="city_county">
</div>
<div class="col-sm-3">
<span class="resultLabel">State/Province</span>
<input readonly type="text" class="form-control" name="state_province" id="state_province">
</div>
<div class="col-sm-2">
<span class="resultLabel">Latitude</span>
<input readonly type="text" class="form-control" id="latitude" />
</div>
<div class="col-sm-2">
<span class="resultLabel">Longitude</span>
<input readonly type="text" class="form-control" id="longitude" />
</div>
</div>
</div>
</body>
</html>