简体   繁体   中英

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.

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 .

If your domain is insecure (eg 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.

in 2017 :

Note: As of Chrome 50, the Geolocation API will only work on secure contexts such as HTTPS. If your site is hosted on an non-secure origin (such as HTTP) the requests to get the users location will no longer function.

Geolocation API Removed from Unsecured Origins in Chrome 50

It works fine 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 please allow the permission and after checking the permission please run it

after running either it will be sucesscallback or else it comes to errorcallback

 function sucesscallback (position) { var userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); var myOptions = { zoom: 15, center: userLatLng, mapTypeId: google.maps.MapTypeId.ROADMAP }; var mapObject = new google.maps.Map(document.getElementById("googleMap"), myOptions); var marker = new google.maps.Marker({ map: mapObject, position: userLatLng }); } function failureCallback(error) { switch (error.code) { case 1: alert("User denied the request for Geolocation."); break; case 2: alert("Location information is unavailable. Please ensure Location is On"); break; case 3: alert("timeout"); break; case 4: alert("An unknown error occurred."); break; } } 
 <div id='googleMap' style='width:300px;height:300px;'> </div> 

The Geolocation API lets you discover, with the user's consent, the user's location. You can use this functionality for things like guiding a user to their destination and geo-tagging user-created content; for example, marking where a photo was taken.

The Geolocation API also lets you see where the user is and keep tabs on them as they move around, always with the user's consent (and only while the page is open). This creates a lot of interesting use cases, such as integrating with backend systems to prepare an order for collection if the user is close by.

You need to be aware of many things when using the Geolocation API. This guide walks you through the common use cases and solutions.

https://developers.google.com/web/fundamentals/native-hardware/user-location/?hl=en

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM