-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocode.js
129 lines (101 loc) · 3.15 KB
/
geocode.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
const APIKEY = `AIzaSyBr-bRYdgqrfnYLNzQR8_m_sW1cLMPGRpY`;
const APIURL = "https://maps.googleapis.com/maps/api/geocode/json";
var addressInput = document.getElementById("addressInput");
var mapElem = document.getElementById("map");
const statusDiv = document.getElementById("statusMsg");
let theAddress = '';
let ADDRSS_LAT, ADDRSS_LNG, INFO_ADDRSS;
document.getElementById("loadMap").addEventListener("click", testAll);
document.getElementById("clearAll").addEventListener("click", function(e){
e.preventDefault();
mapElem.innerHTML = "";
statusDiv.innerHTML = "";
});
addressInput.addEventListener("keyup", function(e){
var keyboardCode = (e.keyCode ? e.keyCode : e.which);
statusDiv.innerHTML = addressInput.value;
if(keyboardCode == 13) {
theAddress = addressInput.value;
initGeoCodeAxios();
}
});
function testAll(e){
e.preventDefault();
statusDiv.innerHTML = addressInput.value;
addressInput.value? ( theAddress = addressInput.value, initGeoCodeAxios() ) : ( statusDiv.innerHTML = "<h5> Please Type sth!</h5>", mapElem.innerHTML = "");
}
function initGeoCodeAxios() {
axios.get(APIURL, {
params: {
address: theAddress,
key: APIKEY
}
}).then((res) => {
var theData = res.data.results[0];
var formatted_address = theData.formatted_address;
var types = "";
for (var i in theData.types) {
types += `${theData.types[i]} ,`;
};
ADDRSS_LAT = theData.geometry.location.lat;
ADDRSS_LNG = theData.geometry.location.lng;
INFO_ADDRSS = `${theData.address_components[0].long_name}, ${theData.address_components[1].long_name} - ${types}`;
console.log(ADDRSS_LAT, ADDRSS_LNG);
statusDiv.innerHTML = `
<div class='col s12'>
<h5> Address: ${formatted_address}</h5>
<p> Lat: ${ADDRSS_LAT}</p>
<P> Lng: ${ADDRSS_LNG}</P>
</div>
`;
var mapOptionsObj = {
zoom: 14,
center: {
lat: ADDRSS_LAT,
lng: ADDRSS_LNG,
}
}
var theMap = new google.maps.Map(mapElem, mapOptionsObj);
var markerOptionsObj = {
position: {
lat: ADDRSS_LAT,
lng: ADDRSS_LNG
},
map: theMap
}
var theMarker = new google.maps.Marker(markerOptionsObj);
var theInfoWindow = new google.maps.InfoWindow({
content: `${INFO_ADDRSS}`
});
theMarker.addListener("click", function () {
theInfoWindow.open(theMap, theMarker);
})
}).catch((err) => {
statusMsg.innerHTML = err;
})
}
// function initMap(){
// var mapElem = document.getElementById("map");
// var mapOptionsObj = {
// zoom:12,
// center:{
// lat: ADDRSS_LAT,
// lng: ADDRSS_LNG,
// // lat: 23.8639354,
// // lng: 90.3953525
// }
// }
// var markerOptionsObj = {
// position: {
// lat: ADDRSS_LAT,
// lng: ADDRSS_LNG
// },
// map: theMap
// }
// var theMap = new google.maps.Map(mapElem, mapOptionsObj);
// var theMarker = new google.maps.Marker(markerOptionsObj);
// var theInfoWindow = new google.maps.InfoWindow({ content: `${INFO_ADDRSS}`});
// theMarker.addListener("click", function(){
// theInfoWindow.open(theMap, theMarker);
// })
// }