JSFiddle - React, Tailwind, and code Playground
by Roman Fedytskyi
HTML
<div id="post-code"></div>
JavaScript
var API_KEY = "AIzaSyC2iHBX7N_JxK_J35h6kSE35l2DVgUVbNY";
function sendRequest(cb) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
cb(this.responseText)
}
};
xhttp.open("GET", "https://maps.googleapis.com/maps/api/geocode/json?address=Ooms Tanktransporten+B.V.+Bij+de+Kerk+37,+Nieuwpoort,+t&key=" + API_KEY, true);
xhttp.send();
}
function getPostCode(res) {
var results = JSON.parse(res).results[0];
var postCode = results.address_components[results.address_components.length - 1].long_name;
console.log(postCode);
document.getElementById('post-code').innerHTML = postCode;
}
sendRequest(getPostCode);