-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
146 lines (138 loc) · 3.97 KB
/
main.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
import { ApiKeyManager } from "@esri/arcgis-rest-request";
import { geocode, suggest } from "@esri/arcgis-rest-geocoding";
import API_KEY from "./js/apikey";
const authentication = new ApiKeyManager({ key: API_KEY });
let sugg = [];
let inputField = document.getElementById("input");
let ulField = document.getElementById("suggestions");
let apt = document.getElementById("apt");
let city = document.getElementById("city");
let state = document.getElementById("state");
let postal = document.getElementById("postal");
let country = document.getElementById("country");
let form = document.getElementById("form");
let address = "";
let theCity = "";
let theState = "";
let thePostal = "";
let theCountry = "";
let value = {};
//getting suggestions
inputField.addEventListener("input", ({ target }) => {
suggest(target.value, {
params: { maxSuggestions: 5 },
authentication,
}).then((res) => {
res.suggestions.forEach((item) => {
sugg.push(item);
});
if (target.value) {
inputField.setAttribute("autocomplete", "new-password");
}
changeAutoComplete(target.value);
});
});
//showing suggestions
function changeAutoComplete(data) {
ulField.innerHTML = ``;
if (data.length > 0) {
ulField.style.border = "1px solid gray";
for (value of sugg) {
const li = document.createElement("li");
li.innerHTML = value.text;
li.innerText = value.text;
li.classList.add(value.magicKey);
li.classList.add("list-group-item-action");
li.classList.add("active");
li.addEventListener("click", (event) => {
inputField.removeAttribute("autocomplete");
li.classList.remove("active");
geo(event.target.className);
ulField.style.border = "";
ulField.innerHTML = "";
});
ulField.appendChild(li);
}
sugg = [];
}
}
function geo(magicKey) {
geocode({
magicKey,
maxLocations: 1,
outFields: "*",
authentication,
}).then((resu) => {
let result = resu.candidates[0];
console.log(result);
result.attributes.StAddr
? (inputField.value = result.attributes.StAddr)
: (inputField.value = result.attributes.ShortLabel);
address = result.attributes.StAddr;
theCity = result.attributes.City;
city.value = result.attributes.City;
theState = result.attributes.Region;
state.value = result.attributes.Region;
theCountry = result.attributes.CntryName;
country.value = result.attributes.CntryName;
thePostal = result.attributes.Postal;
if (result.attributes.PostalExt) {
postal.value = `${result.attributes.Postal}-${result.attributes.PostalExt}`;
} else {
postal.value = result.attributes.Postal;
}
postal.value = result.attributes.Postal;
setAllFields();
});
}
function setAllFields() {
apt.addEventListener("change", (event) => {
event.preventDefault();
apt = event.target.value;
});
city.addEventListener("change", (event) => {
event.preventDefault();
theCity = event.target.value;
});
state.addEventListener("change", (event) => {
event.preventDefault();
theState = event.target.value;
});
postal.addEventListener("change", (event) => {
event.preventDefault();
thePostal = event.target.value;
});
country.addEventListener("change", (event) => {
event.preventDefault();
theCountry = event.target.value;
});
geocode({
address: address,
city: theCity,
region: theState,
postal: thePostal,
countryCode: theCountry,
authentication,
}).then((res) => {
console.log(res.candidates[0].score);
});
}
form.addEventListener("submit", (event) => {
event.preventDefault();
geocode({
address: address,
city: theCity,
region: theState,
postal: thePostal,
countryCode: theCountry,
authentication,
}).then((res) => {
console.log(res.candidates[0]);
if (res.candidates[0].score > 99) {
alert("Form accepted!");
window.location.reload(false);
} else {
alert("Please check address!");
}
});
});