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.

Why need Location 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.

Example :

<!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>

Web console Output :

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.


Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

OR

No Comments