4

How do I ask for Location Permissions in Javascript

 1 year ago
source link: https://www.laravelcode.com/post/how-do-i-ask-for-location-permissions-in-javascript
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How do I ask for Location Permissions in Javascript

  13333 views

  1 year ago

Javascript

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.

Author : Harsukh Makwana
Harsukh Makwana

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]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK