diff --git a/JavaScript/Advance/Web API/web-geolocation API/Images/js-logo.png b/JavaScript/Advance/Web API/web-geolocation API/Images/js-logo.png new file mode 100644 index 0000000..4637ac9 Binary files /dev/null and b/JavaScript/Advance/Web API/web-geolocation API/Images/js-logo.png differ diff --git a/JavaScript/Advance/Web API/web-geolocation API/index.html b/JavaScript/Advance/Web API/web-geolocation API/index.html new file mode 100644 index 0000000..89c1845 --- /dev/null +++ b/JavaScript/Advance/Web API/web-geolocation API/index.html @@ -0,0 +1,33 @@ + + + + + + + + Geolocation API + + + + +

Geolocation API

+ +

Example One

+ +

+ +

Example Two

+ +

+ +

Example Three

+ +

+ +

Example Four

+ +
+ + + + \ No newline at end of file diff --git a/JavaScript/Advance/Web API/web-geolocation API/script.js b/JavaScript/Advance/Web API/web-geolocation API/script.js new file mode 100644 index 0000000..d44b697 --- /dev/null +++ b/JavaScript/Advance/Web API/web-geolocation API/script.js @@ -0,0 +1,81 @@ +// Geolocation API + +// Example One + +const textOne = document.getElementById("textOne"); + +function getLocationOne() { + if (navigator.geolocation) { + navigator.geolocation.getCurrentPosition(showPositionOne); + } else { + textOne.innerHTML = "Geolocation is not supported by this browser."; + } +} + +function showPositionOne(position) { + textOne.innerHTML = "Latitude: " + position.coords.latitude + + "
Longitude: " + position.coords.longitude; +} + + +// Example Two + +const textTwo = document.getElementById("textTwo"); + +function getLocationTwo() { + if (navigator.geolocation) { + navigator.geolocation.getCurrentPosition(showPositionTwo, showError); + } else { + textTwo.innerHTML = "Geolocation is not supported by this browser."; + } +} + +function showPositionTwo(position) { + textTwo.innerHTML = "Latitude: " + position.coords.latitude + + "
Longitude: " + position.coords.longitude; +} + +function showError(error) { + switch (error.code) { + case error.PERMISSION_DENIED: + textTwo.innerHTML = "User denied the request for Geolocation." + break; + case error.POSITION_UNAVAILABLE: + textTwo.innerHTML = "Location information is unavailable." + break; + case error.TIMEOUT: + textTwo.innerHTML = "The request to get user location timed out." + break; + case error.UNKNOWN_ERROR: + textTwo.innerHTML = "An unknown error occurred." + break; + } +} + +// Example Three +const textThree = document.getElementById("textThree"); + +function getLocationThree() { + if (navigator.geolocation) { + navigator.geolocation.watchPosition(showPositionThree); + } else { + textThree.innerHTML = "Geolocation is not supported by this browser."; + } +} + +function showPositionThree(position) { + textThree.innerHTML = "Latitude: " + position.coords.latitude + + + "
Longitude: " + position.coords.longitude; +} + +// Show Position Example Four +let mapholder = document.getElementById("mapholder") + +function showPositionFour(position) { + let latlon = position.coords.latitude + "," + position.coords.longitude; + + let img_url = "https://maps.googleapis.com/maps/api/staticmap?center=" + latlon + "&zoom=14&size=400x300&sensor=false&key=YOUR_KEY"; + + mapholder.innerHTML = ""; +} \ No newline at end of file