Search code examples
javascriptphplaravel-5google-geocoder

Google not revert currect lattitude and longitude of current position


I want to build locate me functionality in laravel. I am using this code but it is sending slightly different latitude and longitude.

function getLocation() 
{
  var x = document.getElementById("lat_long");
   if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(showPosition);
  } else {
  x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) 
{
   var x = document.getElementById("lat_long");
   x.innerHTML = "Latitude: " + position.coords.latitude + 
   "<br>Longitude: " + position.coords.longitude; 
}

Is there any alternate. so that i can improve my result.


Solution

  • In order, for more accuracy you need to set Options object with enableHighAccuracy true. For more detail about the function you can check on this link https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition

    var options = {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0
    };
    
    function success(pos) {
      var crd = pos.coords;
    
      console.log('Your current position is:');
      console.log(`Latitude : ${crd.latitude}`);
      console.log(`Longitude: ${crd.longitude}`);
      console.log(`More or less ${crd.accuracy} meters.`);
    }
    
    function error(err) {
     console.warn(`ERROR(${err.code}): ${err.message}`);
    }
    
    navigator.geolocation.getCurrentPosition(success, error, options);