-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
js-address-lookup.js
349 lines (297 loc) · 9.83 KB
/
js-address-lookup.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* # js-address-lookup.js
* Auto complete addresses as the user types. Save the output to individual fields.
*/
document.addEventListener('DOMContentLoaded', function(){
(function(){
// Settings - Remove the // and edit these as needed
var options = {
googleCloudAPIKey: 'AIzaSyBVrqGDBqE85jvuaOO6-bXBTywbfCzoET0',
lookupFieldWrapperClassName: 'jls-address-lookup',
lookupFieldWrapperHiddenClassName: 'jls-address-lookup--hidden',
lookupFieldEditLinkClassName: 'jls-address-lookup__manual-link',
lookupFieldClassName: 'jls-address-lookup__field',
manualFieldsClassName: 'jls-manual-address',
manualFieldsHiddenClassName: 'jls-manual-address--hidden',
addressPreviewClassName: 'jls-address-preview',
addressPreviewContentClassName: 'jls-address-preview__content',
addressPreviewHiddenClassName: 'jls-address-preview--hidden',
manualFieldNames: {
address_line_1: 'address_line_1',
address_line_2: 'address_line_2',
city: 'city',
county: 'county',
postcode: 'postcode',
country: 'country'
}
};
// #############################################################################
// Do not edit below this line
var googleAutoComplete, googlePlaceSearch;
var lookupFieldEditLink = document.getElementsByClassName(options.lookupFieldEditLinkClassName);
var lookupFieldWrapper = document.getElementsByClassName(options.lookupFieldWrapperClassName)[0];
var lookupField = document.getElementsByClassName(options.lookupFieldClassName)[0];
var addressPreview = document.getElementsByClassName(options.addressPreviewClassName)[0];
var addressPreviewContent = addressPreview.getElementsByClassName(options.addressPreviewContentClassName)[0];
var manualAddressFields = document.getElementsByClassName(options.manualFieldsHiddenClassName)[0];
var initGoogle = function(){
(function(d, script) {
script = d.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.onload = function(){
// remote script has loaded
};
script.src = 'https://maps.googleapis.com/maps/api/js?key='+ options.googleCloudAPIKey +'&libraries=places&callback=';
d.getElementsByTagName('body')[0].appendChild(script);
}(document));
}
var init = function(){
lookupField.addEventListener("focus", initAutocomplete);
addEventListenerOnEach(lookupFieldEditLink, "click", showManual);
initGoogle();
}
var addEventListenerOnEach = function(els, e, f){
for (var i = 0; i < els.length; i++) {
els[i].addEventListener(e, f);
}
}
var initAutocomplete = function(){
googleAutoComplete = new google.maps.places.Autocomplete(
lookupField,
{types: ['geocode', 'establishment']}
);
googleAutoComplete.setFields(['address_component', 'formatted_address', 'name', 'type']);
googleAutoComplete.addListener('place_changed', addressSelected);
}
var showManualAddressFields = function(){
manualAddressFields.classList.remove(options.manualFieldsHiddenClassName);
}
var hideManualAddressFields = function(){
manualAddressFields.classList.add(options.manualFieldsHiddenClassName);
}
var showAddressPreview = function(){
addressPreview.classList.remove(options.addressPreviewHiddenClassName);
}
var hideAddressPreview = function(){
addressPreview.classList.add(options.addressPreviewHiddenClassName);
}
var showLookupFieldWrapper = function(){
lookupFieldWrapper.classList.remove(options.lookupFieldWrapperHiddenClassName);
}
var hideLookupFieldWrapper = function(){
lookupFieldWrapper.classList.add(options.lookupFieldWrapperHiddenClassName);
}
var showManual = function(){
hideAddressPreview();
hideLookupFieldWrapper();
showManualAddressFields();
}
var hideManual = function(){
showAddressPreview();
showLookupFieldWrapper();
hideManualAddressFields();
}
var populateAddressPreview = function(){
var address = populatedAddressString();
addressPreviewContent.textContent = address;
}
var populatedAddressString = function(){
var address = '';
var address_line_1 = document.getElementsByName(options.manualFieldNames.address_line_1)[0].value;
var address_line_2 = document.getElementsByName(options.manualFieldNames.address_line_2)[0].value;
var city = document.getElementsByName(options.manualFieldNames.city)[0].value;
var county = document.getElementsByName(options.manualFieldNames.county)[0].value;
var postcode = document.getElementsByName(options.manualFieldNames.postcode)[0].value;
var country = document.getElementsByName(options.manualFieldNames.country)[0].value;
address += (address_line_1 != '')? address_line_1 + ', ' : '';
address += (address_line_2 != '')? address_line_2 + ', ' : '';
address += (city != '')? city + ', ' : '';
address += (county != '')? county + ', ' : '';
address += (postcode != '')? postcode + ', ' : '';
address += (country != '')? country : '';
return address;
}
// Add the selected address to hidden fields
var addressSelected = function(){
var selectedPlace = googleAutoComplete.getPlace();
populateFields(parsePlaceToAddress(selectedPlace));
populateAddressPreview();
showAddressPreview();
hideLookupFieldWrapper();
}
var parsePlaceToAddress = function(place){
var address_components = place.address_components;
var result = {
raw: {
"country": {
"code": "",
"name": ""
}
},
formatted: {
"formatted": "",
"address_line_1": "",
"address_line_2": "",
"city": "",
"county": "",
"postcode": "",
"country": {
"code": "",
"name": ""
},
}
};
// Create a raw version
for (var i = 0; i < address_components.length; i++) {
var item = address_components[i];
if(item.types[0] == 'country'){
result.raw[item.types[0]].name = item.long_name;
result.raw[item.types[0]].code = item.short_name;
}
else result.raw[item.types[0]] = item.long_name;
}
/*
Weight
City
- postal_town
- locality
Address Line 2
- sublocality_level_1
- locality (if not used)
*/
// Used values
var hasAddressLine2 = false;
var hasCity = false;
// Assign raw to formatted
for (var key in result.raw) {
if (result.raw.hasOwnProperty(key)) {
var value = result.raw[key];
// Address Line 1
if(key == 'street_number'){
result.formatted.address_line_1 += value + ' ';
}
if(key == 'route'){
result.formatted.address_line_1 += value;
}
// Sublocality
if(key == 'sublocality_level_1' && !hasAddressLine2){
result.formatted.address_line_2 = value;
hasAddressLine2 = true;
}
// Locality
if(key == 'locality'){
if(!hasAddressLine2) result.formatted.address_line_2 = value;
if(!hasCity) result.formatted.city = value;
hasAddressLine2 = true;
}
// Town/City
if(key == 'postal_town'){
result.formatted.city = value;
hasCity = true;
}
// Country
if(key == 'country'){
result.formatted.country = value;
}
// County
if(result.raw.country.code == 'US'){
if(key == 'administrative_area_level_1'){
result.formatted.county = value;
}
}
else {
if(key == 'administrative_area_level_2'){
result.formatted.county = value;
}
}
// Postcode
if(key == 'postal_code'){
result.formatted.postcode = value;
}
}
}
// Business Name Parsing
var isBusinessAddress = false;
var placeTypes = place.types;
var placeName = place.name;
if(placeTypes.includes("establishment")){
result.formatted.address_line_1 = placeName + ', ' + result.formatted.address_line_1;
}
// Formatted from Google
result.formatted.formatted = place.formatted_address;
return result;
}
// Parse place to address object
var parsePlaceToAddressV1 = function(place){
var result = {
"formatted": "",
"address_line_1": " ",
"address_line_2": "",
"city": "",
"county": "",
"postcode": "",
"country": {
"code": "",
"name": ""
},
};
// Place type
var placeTypes = place.types;
var isBusinessAddress = false;
var placeName = place.name;
if(placeTypes.includes("establishment")) isBusinessAddress = true;
if(isBusinessAddress) result['address_line_1'] = result['address_line_1'] + placeName + ', ';
var components = place.address_components;
for (var i = 0; i < place.address_components.length; i++) {
var type = place.address_components[i].types[0];
var value = place.address_components[i];
switch (type) {
case 'street_number':
result['address_line_1'] = result['address_line_1'] + value.long_name + result['address_line_1'];
break;
case 'route':
result['address_line_1'] = result['address_line_1'] + value.long_name;
break;
case 'locality':
result['address_line_2'] = value.long_name;
break;
case 'postal_town':
result['city'] = value.long_name;
break;
case 'administrative_area_level_2':
result['county'] = value.long_name;
break;
case 'country':
result['country']['name'] = value.long_name;
result['country']['code'] = value.short_name;
break;
case 'postal_code':
result['postcode'] = value.long_name;
break;
default:
}
}
result['formatted'] = place.formatted_address;
return result;
}
// Fill in fields
var populateFields = function(place){
for (var key in options.manualFieldNames) {
if (options.manualFieldNames.hasOwnProperty(key)) {
var field = document.getElementsByName(options.manualFieldNames[key])[0];
var value = place.formatted[key];
if(typeof field !== 'undefined'){
if(key == 'country'){
field.value = value.code;
}
else {
field.value = value;
}
}
}
}
}
init(); // Run
})();
});