如何强制用户启用位置以使用 C# 在 ASP.NET Core 6 MVC 中访问页面?

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

我正在处理一项要求,用户必须启用他的设备位置,以便我可以获得他的位置(纬度、经度)。

  1. 用户必须启用his/her设备的位置
  2. 获取用户的确切位置

我该怎么做?

请参考http://www.w3schools.com/html/html5_geolocation.asp

您可以使用 javascript 和 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;
  }
}