Home > other >  How to force user to enable location for accessing a page in ASP.NET Core 6 MVC using C#?
How to force user to enable location for accessing a page in ASP.NET Core 6 MVC using C#?

Time:04-05

I am working on a requirement where a user has to enable his device location so that I can get his location (latitude, longitude).

  1. User has to enable his/her device's location
  2. Get user's exact location

How can I do this?

CodePudding user response:

Pleaser refer http://www.w3schools.com/html/html5_geolocation.asp

You can achieve using javascript with navigator

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

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

function showError(error) {
  switch(error.code) {
    case error.PERMISSION_DENIED:
      x.innerHTML = "User denied the request for Geolocation."
      break;
    case error.POSITION_UNAVAILABLE:
      x.innerHTML = "Location information is unavailable."
      break;
    case error.TIMEOUT:
      x.innerHTML = "The request to get user location timed out."
      break;
    case error.UNKNOWN_ERROR:
      x.innerHTML = "An unknown error occurred."
      break;
  }
}
  • Related