skip to Main Content

I’m trying to implement a web page which displays your location on a map. Given that the person accessing the web page may not have internet access, I want the web page to be a local HTML file.

These days, for security reasons, it doesn’t seem to be possible to access geolocation form a local HTML file.

Any ideas as to how I can get access to geolocation from a local HTML file?

Thanks

I have tried accessing my local HTML file via a local web server but these only allow http pages whereas geolocation seems to be dependent upon an https file.

2

Answers


  1. To use Geolocation API your application should run on HTTPS protocol or on the localhost web server. Otherwise, Geolocation API will not work.
    Most of all you will get the coordinates of your internet provider stations 🙂

    navigator.geolocation.getCurrentPosition(showPosition);
    
          function showPosition(position) {
            console.log("Latitude: " + position.coords.latitude +
            " Longitude: " + position.coords.longitude);
          }
    
    Login or Signup to reply.
  2. <button onclick="getLocation()">Get Location</button>
    
    <script>
    function getLocation() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
      } else {
        alert("Geolocation is not supported by this browser.");
      }
    }
    
    function showPosition(position) {
      alert("Latitude: " + position.coords.latitude +
      "nLongitude: " + position.coords.longitude);
    }
    </script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search