Javascript W3C 地理定位 API 在 Chrome 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6181379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:36:34  来源:igfitidea点击:

W3C Geolocation API not working in Chrome

javascriptfirefoxgoogle-chromegeolocation

提问by LandonSchropp

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.

这是怎么回事?我认为 Google Chrome 应该支持 W3C Geolocation API。

回答by no.good.at.coding

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
  • Because of security restrictions, resources loaded with the file:///scheme are not allowed access to location. See HTML 5 Geo Location Prompt in Chrome.
  • 确保您没有在 Chrome 中禁用位置跟踪: Options > Under the Hood > Content Settings > Location
  • 由于安全限制,加载了该file:///方案的资源不允许访问位置。请参阅Chrome 中的 HTML 5 地理位置提示

回答by Mark

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).

如果您的域不安全(例如 HTTP 而不是 HTTPS),则不允许您访问 Chrome 中的位置。这是自 Chrome 版本 50(太平洋标准时间 2016 年 4 月 20 日下午 12 点)以来。

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

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

回答by kurumkan

in 2017 :

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.

注意:从 Chrome 50 开始,地理定位 API 将仅适用于安全上下文,例如 HTTPS。如果您的站点托管在非安全源(例如 HTTP)上,则获取用户位置的请求将不再起作用。

Geolocation API Removed from Unsecured Origins in Chrome 50

从 Chrome 50 中的不安全来源中删除地理定位 API

回答by Android Developer Vinod

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 sucesscallbackor else it comes to errorcallback

运行后要么是,要么sucesscallbackerrorcallback

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>

回答by Bahram Etemad

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.

Geolocation API 允许您在征得用户同意的情况下发现用户的位置。您可以使用此功能将用户引导至目的地和对用户创建的内容进行地理标记;例如,标记照片的拍摄地点。

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.

Geolocation API 还允许您查看用户的位置并在他们四处走动时密切关注他们,始终在用户同意的情况下(并且仅在页面打开时)。这创造了许多有趣的用例,例如与后端系统集成以在用户附近时准备收集订单。

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

使用 Geolocation API 时需要注意很多事情。本指南将引导您了解常见用例和解决方案。

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

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