How to Get Current Location in JavaScript Example

  5737 views   1 year ago Javascript

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.

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]

Related Articles