GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — JavaScript - Geolocation API
Selected Reading
JavaScript - Geolocation APIGeolocation APIThe Geolocation API is a web API that provides a JavaScript interface to access the user's geographical location data. A Geolocation API contains the various methods and properties that you can use to access the user's location on your website. It detects the location of the user's using the device's GPS. The accuracy of the location depends on the accuracy of the GPS device. As location compromises the users' privacy, it asks for permission before accessing the location. If users grant permission, websites can access the latitude, longitude, etc. Sometimes, developers need to get the user's location on the website. For example, if you are creating an Ola, Uber, etc. type applications, you need to know the user's location to pick them up for the ride. The Geolocation API allows users to share the location with a particular website. Real-time use cases of the Geolocation APIHere are the real-time use cases of the Geolocation API.
Using the Geolocation APITo use the Geolocation API, you can use the 'navigator' property of the window object. The Navigator object contains the 'geolocation' property, containing the various properties and methods to manipulate the user's location. SyntaxFollow the syntax below to use the Geolocation API. var geolocation = window.navigator.geolocation; OR var geolocation = navigator.geolocation; Here, the geolocation object allows you to access the location coordinates. Example: Checking the browser supportUsing the navigator, you can check whether the user's browser supports the Geolocation.geolocation property. The code below prints the message accordingly whether the Geolocation is supported. First, we convert the data into the JSON format. After that, we convert the data into the string and print it on the web page. <html>
<body>
<div id = "output"> </div>
<script>
const output = document.getElementById("output");
if (navigator.geolocation) {
output.innerHTML += "Geolocation is supported by your browser."
} else {
output.innerHTML += "Geolocation is not supported by your browser."
}
</script>
</body>
</html>
OutputGeolocation is supported by your browser. Geolocation MethodsHere are the methods of the Geolocation API.
The Location PropertiesThe getCurrentPosition() method executes the callback function you passed as an argument. The callback function takes the object as a parameter. The parametric object contains various properties with information about the user's location. Here, we have listed all properties of the location object in the table below.
Getting User's LocationYou can use the getCurrentPosition() method to get the user's current location. SyntaxUsers can follow the syntax below to get the user's current position using the getCurrentPosition() method. navigator.geolocation.getCurrentPosition(successCallback, errorCallback, options); ParametersThe getCurrentPosition() object takes 3 parameters.
Permitting the website to access your locationWhenever any website tries to access your location, the browser pops up the permission alert box. If you click the 'allow, it can fetch your location details. Otherwise, it throws an error. You can see the permission pop-up in the image below. ExampleIn the below code, we use the getCurrentPosition() method to get the user's location. The method calls the getCords() function to get the current location. In the getCords() function, we print the value of the various properties of the cords object. First, we convert the data into the JSON format. After that, we convert the data into the string and print it on the web page. <html>
<body>
<h3> Location Information </h3>
<button onclick = "findLocation()"> Find Location </button>
<p id = "output"> </p>
<script>
const output = document.getElementById("output");
function findLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getCords); //
}
else {
output.innerHTML += "Geo Location is not supported by this browser!";
}
}
// Callback function
function getCords(coords) {
output.innerHTML += "The current position is: <br>";
output.innerHTML += "Latitude: " + coords.coords.latitude + "<br>";
output.innerHTML += "Longitude: " + coords.coords.longitude + "<br>";
output.innerHTML += "Accuracy: " + coords.coords.accuracy + "<br>";
output.innerHTML += "Altitude: " + coords.coords.altitude + "<br>";
}
</script>
</body>
</html>
Geolocation ErrorsThe getCurrentPosition() method takes the callback function as a second parameter to handle the error. The callback function can have an error object as a parameter. In the below cases, an error can occur.
Here is the list of properties of the error object.
Here is the list of different error codes.
Example: Error HandlingWe use the getCurrentPosition() method in the findLocation() function in the below code. We have passed the errorCallback() function as a second parameter of the getCurrentPosition() method to handle the errors. In the errorCallback() function, we use the switch case statement to print the different error messages based on the error code. When you click the Find Location button, it will show you a pop-up asking permission to access the location. If you click on the 'block, it will show you an error. First, we convert the data into the JSON format. After that, we convert the data into the string and print it on the web page. <html>
<body>
<div id = "output"> </div>
<button onclick = "findLocation()"> Find Location </button>
<script>
const output = document.getElementById("output");
function findLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getCords, errorCallback); //
}
else {
output.innerHTML += "Geo Location is not supported by this browser!";
}
}
// Callback function
function getCords(coords) {
output.innerHTML += "The current position is: <br>";
output.innerHTML += "Latitude: " + coords.coords.latitude + "<br>";
output.innerHTML += "Longitude: " + coords.coords.longitude + "<br>";
}
// Function to handle error
function errorCallback(err) {
switch (err.code) {
case err.PERMISSION_DENIED:
output.innerHTML += "You have denied to access your device's location";
break;
case err.POSITION_UNAVAILABLE:
output.innerHTML += "Your position is not available.";
break;
case err.TIMEOUT:
output.innerHTML += "Request timed out while fetching your location.";
break;
case err.UNKNOWN_ERROR:
output.innerHTML += "Unknown error occurred while fetching your location.";
break;
}
}
</script>
</body>
</html>
Geolocation OptionsThe getCurrentPosition() method takes the object containing the options as a third parameter. Here is the list of options you can pass as a key to the option object.
ExampleThe below code finds the most accurate location. Also, we have set milliseconds to the maximumAge, and timeout properties of the options object. First, we convert the data into the JSON format. After that, we convert the data into the string and print it on the web page. <html>
<body>
<div id = "output"> </div>
<button onclick = "findLocation()"> Find Location </button>
<script>
const output = document.getElementById("output");
function findLocation() {
if (navigator.geolocation) {
// Options for geolocation
const options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
navigator.geolocation.getCurrentPosition(getCords, errorfunc, options);
}
else {
output.innerHTML += "Geo Location is not supported by this browser!";
}
}
// Callback function
function getCords(coords) {
output.innerHTML += "The current position is: <br>";
output.innerHTML += "Latitude: " + coords.coords.latitude + "<br>";
output.innerHTML += "Longitude: " + coords.coords.longitude + "<br>";
}
function errorfunc(err) {
output.innerHTML += "The error message is - " + err.message + "<br>";
}
</script>
</body>
</html>
Watching the Current Location of the UserThe watchPosition() method allows you to track the live location of users. It returns the ID, which we can use with the clearWatch() method when you want to stop tracking the user. SyntaxFollow the syntax below to use the watchPosition() method to track the live location of users. var id = navigator.geolocation.watchPosition(successCallback, errorCallback, options) The errorCallback and options are optional arguments. If you want to stop tracking the user, you can follow the syntax below. navigator.geolocation.clearWatch(id); The clearWatch() method takes the id returned by the watchPosition() method as an argument. ExampleIn the below code, we used the geolocation object's watchPosition () method to get the user's continuous position. We used the getCords() function as a callback of the watchPosition() method, where we print the latitude and longitude of the user's position. In the findLocation() method, we used the setTimeOut() method to stop tracking after 30 seconds. In the output, you can observe that the code prints the user's position multiple times. First, we convert the data into the JSON format. After that, we convert the data into the string and print it on the web page. <html>
<body>
<button onclick = "findLocation()"> Find Location </button>
<div id = "output"> </div>
<script>
let output = document.getElementById("output");
function findLocation() {
if (navigator.geolocation) {
let id = navigator.geolocation.watchPosition(getCoords);
setTimeout(function () {
navigator.geolocation.clearWatch(id);
output.innerHTML += "<br>Tracking stopped!";
}, 30000); // Stop tracking after 30 seconds.
} else {
output.innerHTML += "<br>Geo Location is not supported by this browser!";
}
}
// Callback function
function getCoords(location) {
let latitude = location.coords.latitude;
let longitude = location.coords.longitude;
output.innerHTML += `<br> Latitude: ${latitude}, Longitude: ${longitude}`;
}
</script>
</body>
</html>
Advertisements
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |