Search

How to Get Current Location in JavaScript Example

post-title

In this article, I will share with you how to get the current location in javascript with the example. get current let-long in javascript is very easy and you get done this help of the getCurrentPosition() in javascript.

JavaScript has a huge By default functions and it can be very helpful in our web application. so, you can get the current location in the javascript help of a few line codes. 

here is an 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 type="text/javascript">
		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>

i hope you like this article.