html5 geolocation not working on safari browser

17,289

Solution 1

Are you connected with wi-fi or over cable network?

It seems safari has trouble when you aren't connected with wi-fi.

Source

Geolocation not workin on Safari 5.x on Windows 7/XP

Geolocation in Safari 5

Solution 2

Based on the example in the question, the following is a complete working snippet:

<p id=demo>
<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;
}
getLocation()
</script>

I tested that in Safari in my environment and it works as expected. The actual example in the question is incomplete, so it would never do anything as-is by itself. So it’s hard to know what might have been working in Mozilla, Chrome, and IE without seeing the complete code.

If the above snippet still doesn’t work for you in your Safari, please open Safari’s Web Inspector (for example, from the Develop menu or by right-clicking on the page and selecting Inspect Element), and go to the Console tab there and check to see what errors have been logged.

For now, as far as the code snippet, just be completely clear, some basic points to note:

  • the script must actually call the getLocation() function (not just define it)
  • the document must actually have an id=demo element
  • the script should be placed after the id=demo element (if you put the call to getLocation() in, for example, the head of the document, it’s not going to work, because it will run before the id=demo element exists in the DOM)

Also, a general note: Never use w3schools.com for anything. It’s sloppy and highly unreliable and very poorly maintained. Instead use MDN. For example, MDN has a very good Using geolocation page, with complete code for a good live example.

Solution 3

Well unfortunately Apple does not serve geolocation data over HTTP connections. You'll need HTTPS in order to access the client geolocation on Safari.

Share:
17,289
Kamini
Author by

Kamini

I m Java Developer having experience in jsp, servlet, struts, hibernate, spring mvc, mybatis, jersy , RestFul webservices, jmeter, etc. Currently providing APIs to Front End developer(Android/Ios) and also working on Web applications. Worked on svn tool and currently using git. Having experience of DB and deployment of project using Ant and also using mvn package.

Updated on June 04, 2022

Comments

  • Kamini
    Kamini about 2 years

    working on html5 geolocation.

    tried on mozilla, chrome, IE. working fine on them but not working for safari. tested on safari(8.0.5) on mac, safari(5.1) on windows

    simply hitting the url

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

    OR

    http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation
    
      <!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>
    

    it gives popup Allow/Disallow but does nothing after allowing it. And i have tested for safari on Mac connected with wi-fi But not for laptop on wi-fi and LAN too.