Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

W3C Geolocation API not working in Chrome

The below code works in Firefox but not in Google Chrome:

<!DOCTYPE html>
<html>
    <head>
        <title>title</title>
        <script type="text/javascript">
            var successCallback = function(data) {
                console.log('latitude: ' + data.coords.latitude + ' longitude: ' + data.coords.longitude);
            };

            var failureCallback = function() {
                console.log('location failure :(');
            };

            var logLocation = function() {

                //determine if the handset has client side geo location capabilities
                if(navigator.geolocation){
                   navigator.geolocation.getCurrentPosition(successCallback, failureCallback);
                }
                else{
                   alert("Functionality not available");
                }
            };

            logLocation();
            setTimeout(logLocation, 5000);
        </script>
    </head>
    <body>
        <p>Testing</p>
    <body>
</html>

What's going on? I thought Google Chrome was supposed to support the W3C Geolocation API.

like image 426
LandonSchropp Avatar asked May 30 '11 22:05

LandonSchropp


People also ask

Why is geolocation API not working?

Sites that don't use SSL can no longer access Geolocation APIs to determine a user's physical location. You can fix this by asking your host to install an SSL certificate, or by disabling the “Attempt to auto-locate the user” option on the settings page.

What does the w3c geolocation API do?

If an end user grants permission, the Geolocation API : Provides location data as latitude, longitude, altitude, speed, and heading, as well as the accuracy of the acquired location data, and the approximate time for when the position was acquired via the GeolocationPosition interface.

Do all browsers support Geolocation?

Do all browsers support the HTML5 geolocation API? HTML5 geolocation is supported by multiple browsers including Chrome, Edge, Firefox, Internet Explorer, Opera and Safari. Although you will have to check the respective versions of each browser.

How do I access Geolocation in my browser?

The Geolocation API is accessed via a call to navigator. geolocation ; this will cause the user's browser to ask them for permission to access their location data. If they accept, then the browser will use the best available functionality on the device to access this information (for example, GPS).


2 Answers

Works perfectly for me - with both Chrome 11 and Firefox 4.0.1 on Win 7

  • Make sure you've not disabled location tracking in Chrome: Options > Under the Hood > Content Settings > Location
  • Because of security restrictions, resources loaded with the file:/// scheme are not allowed access to location. See HTML 5 Geo Location Prompt in Chrome.
like image 77
no.good.at.coding Avatar answered Sep 21 '22 11:09

no.good.at.coding


If your domain is insecure (e.g. HTTP rather than HTTPS) then you are not allowed access to location in Chrome. This is since Chrome version 50 (12PM PST April 20 2016).

See https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only for details.

like image 33
Mark Avatar answered Sep 19 '22 11:09

Mark