Results 1 to 6 of 6

Thread: geolocation question

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    38

    geolocation question

    I am following the example of this code from w3schools. The code below works on their website. But when I put it on my own webserver, and open it on browser, the browser doesnt ask for permission and no result will show. But when I just put the html file on my desktop and open it, the browser asks for permission and the result will show. I dont know what is causing the problem in the web server.


    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click the button to get your coordinates.</p>
    
    <button onclick="getLocation()">Try It</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;
    }
    </script>
    
    </body>
    </html>

  2. #2
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,927

    Re: geolocation question

    Quote Originally Posted by UserBee View Post
    I am following the example of this code from w3schools. The code below works on their website. But when I put it on my own webserver, and open it on browser, the browser doesnt ask for permission and no result will show. But when I just put the html file on my desktop and open it, the browser asks for permission and the result will show. I dont know what is causing the problem in the web server.


    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click the button to get your coordinates.</p>
    
    <button onclick="getLocation()">Try It</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;
    }
    </script>
    
    </body>
    </html>
    Which browser are you using? Some browsers will not support geolocation over an http connection, they require https for security reasons.

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    38

    Re: geolocation question

    I am. using chrome and firefox now for testing

  4. #4
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,927

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    38

    Re: geolocation question

    how do you add geolocation permission in a manifest? havent done it before!

  6. #6
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,927

    Re: geolocation question

    Quote Originally Posted by UserBee View Post
    how do you add geolocation permission in a manifest? havent done it before!
    Never done that either, realistically the only way to ensure this works properly is to host it on a valid https connection. What are you using for the web server?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width