-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
109 lines (83 loc) · 3.01 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<title>MYGEOCODE APP(Latest)</title>
</head>
<body>
<div class="container">
<h2 id="text-center">Enter Location:</h2>
<form id="location-form" >
<input type="text" id="llocation-input" class="form-control form-control-lg">
<br>
<button type="submit" name="button" class="btn btn-primary btn-block">Submit</button>
</form>
<div class="card-block" id="formatted-address">
</div>
<div class="card-block" id="address-components">
</div>
<div class="card-block" id="geometry">
</div>
</div>
<script type="text/javascript">
//call geocode
//geocode();
//get location from form
var locationForm = document.getElementById('location-form');
//Listen for Submit
locationForm.addEventListener('submit', geocode);
function geocode(e) {
//prevent actual submit
e.preventDefault();
var location = document.getElementById('llocation-input').value;
axios.get('https://maps.googleapis.com/maps/api/geocode/json',{
params:{
address: location,
key: 'AIzaSyDSevgbhXHfojBJMeyV6CnUGQQChEZ1TtI'
}
})
.then(function(response){
//log full response
console.log(response);
//formated address
var formattedAddress = response.data.results[0].formatted_address;
var formattedAddressOutput = `
<ul class="list-group">
<li class="list-group-item">${formattedAddress}</li>
</ul>
`;
//adddress components
var addressComponents = response.data.results[0].address_components;
var addressComponentsOutput = '<ul class="list-group"';
for(var i = 0; i < addressComponents.length; i++){
addressComponentsOutput += `
<li class="list-group-item"><strong>${addressComponents[i].types[0]}
</strong>: ${addressComponents[i].long_name}</li>
`;
}
addressComponentsOutput += '</ul>';
//formated geometry
var lat = response.data.results[0].geometry.location.lat;
var lng = response.data.results[0].geometry.location.lng;
var geometryOutput = `
<ul class="list-group">
<li class="list-group-item"><strong>Latitude</strong>:${lat}</li>
<li class="list-group-item"><strong>Longitude</strong>:${lng}</li>
</ul>
`;
//output to APP
document.getElementById('formatted-address').innerHTML = formattedAddressOutput;
document.getElementById('address-components').innerHTML = addressComponentsOutput;
document.getElementById('geometry').innerHTML = geometryOutput;
})
.catch(function(error){
console.log(error);
})
}
</script>
</body>
</html>