Skip to content

geolocation #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions JavaScript/Advance/Web API/web-geolocation API/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Geolocation API</title>
<link rel="shortcut icon" href="images/js-logo.png" type="image/x-icon">
</head>

<body>
<h1>Geolocation API</h1>
<!-- Example One -->
<h2>Example One</h2>
<button onclick="getLocationOne()">display location</button>
<p id="textOne"></p>
<!-- Example Two -->
<h2>Example Two</h2>
<button onclick="getLocationTwo()">display location</button>
<p id="textTwo"></p>
<!-- Example Three -->
<h2>Example Three</h2>
<button onclick="getLocationThree()">display location</button>
<p id="textThree"></p>
<!-- Example Four -->
<h2>Example Four</h2>
<button onclick="showPositionFour()">display location</button>
<div id="mapholder"></div>
<script src="script.js"></script>
</body>

</html>
81 changes: 81 additions & 0 deletions JavaScript/Advance/Web API/web-geolocation API/script.js
Original file line number Diff line number Diff line change
@@ -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 +
"<br>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 +
"<br>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 +

"<br>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 = "<img src='" + img_url + "'>";
}