In this article, we will share with you how to get user location permission in javascript. many times you see when you browse the many websites in your computer then you notice some websites have shown one browser popup and they asked us for location permission. so, how it has been working and how to store some location data after any user allows permission.
Yes, location permission data was very helpful in the website's performance statistics. in the future, we will use that kind of data to improve our website content. because we know in which region our website visited most so we can improve our website for our engaged audience and we will be delivered our best to our audience.
Use the following javascript simple code in your web application and you can get the user's location permission.
<!DOCTYPE html>
<html>
<head>
<title>Get User Location</title>
</head>
<body>
<p>Click the button to get your live location.</p>
<button onclick="getLocation()">Get Location</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
console.log(position);
x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>
GeolocationPosition {coords: GeolocationCoordinates, timestamp: 1576907157111}
coords: GeolocationCoordinates
latitude: 23.074603
longitude: 72.5709305a
ltitude: null
accuracy: 37
altitudeAccuracy: null
heading: null
speed: null
__proto__: GeolocationCoordinates
timestamp: 1576907157111
__proto__: GeolocationPosition
We hope it can help you a lot.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to install and connect MS SQL server with PHP in Ubuntu
SQL or MS SQL is relational database ser...How to check if a variable exists or defined in JavaScript
Use the typeof operator If...How to get the position of an element relative to the document using jQuery
Use the jQuery offset() method...Laravel 5.5 - How To Make cURL HTTP Request Example
Today, we are share with you how to make...How to integrate Rozorpay Payment Gateway in Laravel 8
Today, I will share with you how to inte...