W3C Geolocation API无法在Chrome中运行

时间:2011-05-30 22:49:55

标签: javascript firefox google-chrome geolocation

以下代码适用于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>

发生了什么事?我认为Google Chrome应该支持W3C Geolocation API。

5 个答案:

答案 0 :(得分:21)

非常适合我 - 在Win 7上使用Chrome 11和Firefox 4.0.1

  • 确保您未在​​Chrome中停用位置跟踪:Options > Under the Hood > Content Settings > Location
  • 由于安全限制,不允许使用file:///方案加载的资源访问位置。见HTML 5 Geo Location Prompt in Chrome

答案 1 :(得分:18)

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

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

答案 2 :(得分:10)

2017年:

  

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

Geolocation API Removed from Unsecured Origins in Chrome 50

答案 3 :(得分:0)

通过Geolocation API,您可以在用户同意的情况下发现用户的位置。您可以将此功能用于引导用户到目的地以及对用户创建的内容进行地理标记等操作;例如,标记拍摄照片的位置。

Geolocation API还可以让您查看用户所在的位置,并在他们四处移动时始终关注他们,并且必须得到用户的同意(并且仅在页面打开时)。这会创建许多有趣的用例,例如,如果用户在附近,则与后端系统集成以准备收集订单。

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

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

答案 4 :(得分:0)

它适用于我 - 在Win 7上使用Chrome 11和Firefox 4.0.1

确保您未在​​Chrome中停用位置跟踪:选项&gt;在引擎盖下&gt;内容设置&gt;位置请允许  检查权限后,请运行

运行后,它将是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>