Search This Blog

Saturday, September 28, 2019

html to get co-ordinates (latitude/longitude) and validation around +/- 50 meters.

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Check In</button>


<p id="demo"></p>

<script>
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) {

// x.innerHTML = "Latitude: " + position.coords.latitude +
  //"<br>Longitude: " + position.coords.longitude;
 
  var meters = 50;
  var off_latitude = 85.1288229;
  var off_longitude = 89.3264959;

 
  var curr_latitude = position.coords.latitude;
  var curr_longitude = position.coords.longitude;


var coef = meters * 0.0000089;

var plus50_lat = off_latitude + coef;
// pi / 180 d= 0.018
var plus50_long = off_longitude + coef / Math.cos(off_latitude * 0.018);

var minus50_lat = off_latitude - coef;
var minus50_long = off_longitude - coef / Math.cos(off_latitude * 0.018);


if (
((curr_latitude <= off_latitude && curr_latitude >= minus50_lat)
||(curr_latitude <= plus50_lat && curr_latitude >= off_latitude)
)
   
      && (curr_longitude >= off_longitude && curr_longitude <= plus50_long) )
      {
         x.innerHTML = "You are in the office, good Morning" + "<br>"+ "CurrLatitude: "+ curr_latitude + "<br>CurrLongitude: " +curr_longitude
                        + "<br>"+ "off_latitude : "+ off_latitude + "<br>off_longitude : " + off_longitude
                        + "<br>"+ "plus50_lat : "+ plus50_lat + "<br>plus50_long : " + plus50_long + "<br>"+ "minus50_lat : "+ minus50_lat + "<br>minus50_long : " + minus50_long ; 
      }
     
    else
      {
      x.innerHTML = "You are not in the office, please recheck"+ "<br>"+ "CurrLatitude: "+ curr_latitude + "<br>CurrLongitude: " +curr_longitude
                        + "<br>"+ "off_latitude : "+ off_latitude + "<br>off_longitude : " + off_longitude
                        + "<br>"+ "plus50_lat : "+ plus50_lat + "<br>plus50_long : " + plus50_long + "<br>"+ "minus50_lat : "+ minus50_lat + "<br>minus50_long : " + minus50_long ;   
      }

 
}
</script>

</body>
</html> 

No comments:

Post a Comment