简体   繁体   English

W3C Geolocation API无法在Chrome中运行

[英]W3C Geolocation API not working in Chrome

The below code works in Firefox but not in Google Chrome: 以下代码适用于Firefox,但不适用于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. 我认为谷歌Chrome应该支持W3C Geolocation API。

Works perfectly for me - with both Chrome 11 and Firefox 4.0.1 on Win 7 对我来说非常适合 - 在Win 7上使用Chrome 11和Firefox 4.0.1

  • Make sure you've not disabled location tracking in Chrome: Options > Under the Hood > Content Settings > Location 确保您未在​​Chrome中停用位置跟踪: Options > Under the Hood > Content Settings > Location
  • Because of security restrictions, resources loaded with the file:/// scheme are not allowed access to location. 由于安全限制,不允许使用file:/// scheme加载的资源访问位置。 See HTML 5 Geo Location Prompt in Chrome . 请参阅Chrome中的HTML 5地理位置提示

If your domain is insecure (eg HTTP rather than HTTPS) then you are not allowed access to location in Chrome. 如果您的域不安全(例如HTTP而不是HTTPS),则不允许您访问Chrome中的位置。 This is since Chrome version 50 (12PM PST April 20 2016). 这是自Chrome版本50(太平洋标准时间2016年4月20日下午12点)。

See https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only for details. 有关详细信息,请参阅https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only

in 2017 : 在2017年:

Note: As of Chrome 50, the Geolocation API will only work on secure contexts such as HTTPS. 注意:从Chrome 50开始,Geolocation API仅适用于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. 如果您的站点托管在非安全源(例如HTTP)上,则获取用户位置的请求将不再起作用。

Geolocation API Removed from Unsecured Origins in Chrome 50 地理位置API从Chrome 50中的不安全起源中删除

It works fine for me - with both Chrome 11 and Firefox 4.0.1 on Win 7 它适用于我 - 在Win 7上同时使用Chrome 11和Firefox 4.0.1

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 确保您未在​​Chrome中停用位置跟踪:选项>在引擎盖>内容设置>位置,请允许许可,并在检查权限后运行它

after running either it will be sucesscallback or else it comes to errorcallback 在运行之后它将是sucesscallback或者它来到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. 通过Geolocation API,您可以在用户同意的情况下发现用户的位置。 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). Geolocation API还允许您查看用户所在的位置,并在用户移动时始终关注它们,始终在用户同意的情况下(并且仅在页面打开时)。 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. 使用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 https://developers.google.com/web/fundamentals/native-hardware/user-location/?hl=en

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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