Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wedataintelligence/83bcece640c3334dd1cd9238e3456979 to your computer and use it in GitHub Desktop.
Save wedataintelligence/83bcece640c3334dd1cd9238e3456979 to your computer and use it in GitHub Desktop.
Implementing BigDataCloud's Free Client-side Reverse GeoLocation Client API

Implementing BigDataCloud's Free Client-side Reverse GeoLocation Client API

This example implements BigDataCloud's Reverse GeoCoding Client API along with HTML5's Geolocation API.

A Pen by BigDataCloud on CodePen.

License.

<!DOCTYPE html>
<html>
<body>
<p>Click the button to get Location.</p>
<p>Try with and without providing a permission to access your location. </p>
<button type="button" onclick="getLocation()">Share my Location</button>
<pre id="json-result"></pre>
</body>
</html>
var result = document.getElementById("json-result");
const Http = new XMLHttpRequest();
function getLocation() {
console.log("getLocation Called");
var bdcApi = "https://api.bigdatacloud.net/data/reverse-geocode-client"
navigator.geolocation.getCurrentPosition(
(position) => {
bdcApi = bdcApi
+ "?latitude=" + position.coords.latitude
+ "&longitude=" + position.coords.longitude
+ "&localityLanguage=en";
getApi(bdcApi);
},
(err) => { getApi(bdcApi); },
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
});
}
function getApi(bdcApi) {
Http.open("GET", bdcApi);
Http.send();
Http.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
result.innerHTML = this.responseText;
}
};
}
p {font-family: Arial, Helvetica, sans-serif;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment