How to get a user’s Geolocation in Html
In this tutorial, we will discuss How to get a user’s Geolocation in Html. the HTML Geolocation API is utilized to find a client’s position. Geolocation refers to the identification of the geographic location of a user device via a variety of data collection mechanisms. most geolocation services use network routing addresses and internal GPS to determine this location. The Geolocation API is accessed via a call to the navigator. Geolocation user’s browsers to ask them for permission to access their location data. and if the user accepts, then the browser will use the best available functionality on the device to access this information.
HTML Geolocation:
getCurrentPosition() is utilized to restore position. the model beneath restores the scope and longitude of the client’s position :
Example
<!DOCTYPE html>
<html>
<head>
<title>How to get a user's Geolocation in Html</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<h3 style="text-align: center;">How to get a user's Geolocation in Html</h3>
<div style="padding: 50px; text-align: center;">
<input type="text" name="latitude" id="latitude" placeholder="latitude"><br><br>
<input type="text" name="longitude" id="longitude" placeholder="longitude ">
</div>
<script type="text/javascript">
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position) {
$('#latitude').val(position.coords.latitude);
$('#longitude').val(position.coords.longitude);
}
getLocation();
========= OR ========
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
$('#latitude').val(position.coords.latitude);
$('#longitude').val(position.coords.longitude);
});
}
</script>
</body>
</html>