-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (106 loc) · 3.49 KB
/
index.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
const CIVIC_SEARCH_URL = 'https://www.googleapis.com/civicinfo/v2/representatives';
const GEOCODE_URL = 'https://maps.googleapis.com/maps/api/geocode/json';
let divisons;
let office;
let officials;
let dataobject;
let lat;
let longi;
function watchSubmit() {
$('.js-search-form').submit(event => {
event.preventDefault();
const queryTarget = $(event.currentTarget).find('.js-query');
const query = queryTarget.val();
currentquery = queryTarget.val();
// clear out the input
queryTarget.val("");
getDataFromApi(query, parseobject);
$('.results').html(`${query}`);
$('.results').prop('hidden', false);
$('.js-search-results').html('');
});
}
function locateButtonClick() {
$('.locate').on('click', function(event) {
event.preventDefault();
getCoords();
});
}
function getDataFromApi(searchTerm, callback) {
const query = {
address: `${searchTerm}`,
key: "AIzaSyCD8I3_yEbiRrdX_G1sTrMP3MrxwPk-nTA",
}
$.ajax({
url: CIVIC_SEARCH_URL,
data: query,
method: 'GET',
})
.then(function(response) {
console.log(response);
parseobject(response);
})
.catch(function(error) {
console.log(error);
$('.js-search-results').html('<h1>Your search did not work, please try again.</h1>');
});
}
function parseobject(data) {
console.log(data);
office = data.offices;
officials = data.officials;
const results = data.offices.slice(2).map((item, index) => renderResult(item));
}
function renderResult(result) {
const officialsobject = result.officialIndices.map((item, index) => officials[item]);
console.log(officialsobject.length);
for (var i = 0; i < officialsobject.length; i++) {
console.log(officialsobject.length);
$('.js-search-results').append(`
<div class ="col-md-4">
<div class ="result" aria-label="result">
<h3 aria-label="Office">${result.name}</h3>
<h4 aria-labelledby="Name">${officialsobject[i].name}</h4>
${officialsobject[i].party ? `<p aria-labelledby="Party">${officialsobject[i].party}</p>` : '' }
${officialsobject[i].phones ? `<p aria-labelledby="Phone Number">${officialsobject[i].phones}</p>` : '' }
${officialsobject[i].emails ? `<p aria-labelledby="Email">${officialsobject[i].emails}</p>` : '' }
${officialsobject[i].urls ? `<a aria-labelledby="Website" href="${officialsobject[i].urls}">${officialsobject[i].urls}</a>` : ``}
</div>
</div>
`);
}
}
function getCoords() {
$.ajax({
url: 'https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyCD8I3_yEbiRrdX_G1sTrMP3MrxwPk-nTA',
method: 'post',
})
.then(function(response) {
console.log(response);
lat = response.location.lat;
longi = response.location.lng;
console.log(lat, longi);
getAddress(lat, longi, getDataFromApi);
})
.catch(function(error) {
console.log(error);
$('.js-search-results').html('<h1>Your search did not work, please try again.</h1>');
});
}
function getAddress(Latitude, Longitude, callback) {
const query = {
latlng: `${Latitude},${Longitude}`,
key: "AIzaSyCD8I3_yEbiRrdX_G1sTrMP3MrxwPk-nTA",
}
$.getJSON(GEOCODE_URL, query, addressSuccess);
}
function addressSuccess(data) {
let thisobj = data.results[0].formatted_address;
console.log(thisobj);
$('.results').html(data.results[1].formatted_address);
$('.results').prop('hidden', false);
$('.js-search-results').html('');
getDataFromApi(thisobj, parseobject);
}
$(watchSubmit);
$(locateButtonClick);