OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Navigator.geolocation is not giving correct current location always

  • Thread starter Thread starter Wreetbhas Pal
  • Start date Start date
W

Wreetbhas Pal

Guest
Code:
<!DOCTYPE html>
<html>
<body>
<h1>HTML Geolocation</h1>
<p id="loc"></p>

<script>
var ele=document.getElementById("loc");

function getLocation() {

  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition,showError);
  } else { 
    ele.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  ele.innerHTML = "Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;
}
function showError(error){
    ele.innerHTML = error.message;
}
getLocation();

</script>

</body>
</html>

I have opened the html file through chrome browser in a windows tablet which has gps enabled. After opening the html file it asks for location access and I am also giving the access. In this way, I have opened the html file for many times. For very few times, it is giving correct output “Latitude: 22.051516; Longitude: 88.104713”. But for most of the times, it is giving incorrect output “Latitude: 26.7181684; Longitude: 88.4984991”. Note that I am using WiFi in the tablet through my mobile hotspot.

I have also tried with the following code -

Code:
<!DOCTYPE html>
<html>
<body>
<h1>HTML Geolocation</h1>
<p id="loc"></p>

<script>
var ele=document.getElementById("loc");

function getLocation() {

  const options = {
    enableHighAccuracy: true,
    maximumAge: 0,
    timeout: 27000,
  };

  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition,showError, options);
  } else { 
    ele.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  ele.innerHTML = "Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;
}
function showError(error){
    ele.innerHTML = error.message;
}
getLocation();

</script>

</body>
</html>

But nothing is changed in output. Please guide me how to resolve the issue.

<pre><code><!DOCTYPE html>
<html>
<body>
<h1>HTML Geolocation</h1>
<p id="loc"></p>

<script>
var ele=document.getElementById("loc");

function getLocation() {

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,showError);
} else {
ele.innerHTML = "Geolocation is not supported by this browser.";
}
}

function showPosition(position) {
ele.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
function showError(error){
ele.innerHTML = error.message;
}
getLocation();

</script>

</body>
</html>
</code></pre>
<p>I have opened the html file through chrome browser in a windows tablet which has gps enabled. After opening the html file it asks for location access and I am also giving the access. In this way, I have opened the html file for many times. <strong>For very few times, it is giving correct output “Latitude: 22.051516; Longitude: 88.104713”. But for most of the times, it is giving incorrect output “Latitude: 26.7181684; Longitude: 88.4984991”</strong>. Note that I am using WiFi in the tablet through my mobile hotspot.</p>
<p>I have also tried with the following code -</p>
<pre><code><!DOCTYPE html>
<html>
<body>
<h1>HTML Geolocation</h1>
<p id="loc"></p>

<script>
var ele=document.getElementById("loc");

function getLocation() {

const options = {
enableHighAccuracy: true,
maximumAge: 0,
timeout: 27000,
};

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,showError, options);
} else {
ele.innerHTML = "Geolocation is not supported by this browser.";
}
}

function showPosition(position) {
ele.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
function showError(error){
ele.innerHTML = error.message;
}
getLocation();

</script>

</body>
</html>
</code></pre>
<p>But nothing is changed in output. Please guide me how to resolve the issue.</p>
 

Latest posts

Top