【问题标题】:W3C geolocation, Error:position unavailableW3C 地理位置,错误:位置不可用
【发布时间】:2012-04-03 10:04:10
【问题描述】:

我正在尝试实现 W3C 地理定位 API,即使我授予浏览器关于我的位置的权限,我在 Firefox 中使用它时收到错误 2(位置不可用),并且在尝试时收到错误 1(权限被拒绝)从铬。以下是我的代码:

调用 JS 函数的 HTML 按钮:

现在我们来谈谈 Geolocation API

<input type="button" name="canbutt" value="draw c" onClick="javascript:geo()"/>

还有我的 JS 代码:

         function geo(){

                    if (navigator.geolocation) {

                            navigator.geolocation.getCurrentPosition(
                                displayPosition, 
                                displayError);
                    }
                else {
                        alert("Geolocation is not supported by this browser");
                    }

        function displayPosition(position) {
                alert("Latitude: " + position.coords.latitude + ", Longitude: " + position.coords.longitude);
                        }
        function displayError(error) {
                var errors = { 
                    1: 'Permission denied',
                    2: 'Position unavailable',
                    3: 'Request timeout'
                    };
                alert("Error: " + errors[error.code]);
                    }


    }

     function canvas() {
                var canvas = document.getElementById("canv");
                var context = canvas.getContext("2d");
                context.fillStyle="red";
                context.strokeStyle="blue";
                context.strokeRect(50,25,50,60);
                context.fillRect(50,25,50,60);
                context.strokeStyle="green";
                context.moveTo(0,0);
                context.lineTo(300,150);
                context.stroke();
                context.fillStyle="blue";
                context.font="bold 12px Arial";
                context.textAlign="start";
                context.fillText("This is some Text",50,110);
                var gradient=context.createLinearGradient(0,0,100,100);
                gradient.addColorStop(0,"white");
                gradient.addColorStop(1,"black");
                context.fillStyle=gradient;
                context.fillRect(50,50,100,100);
                context.fillText("Im using Gradient!",60,10);
                context.drawImage(image, 20,20);


                    };

【问题讨论】:

    标签: javascript geolocation w3c-geolocation


    【解决方案1】:

    使用来自file:/// URI 的地理位置存在限制。尝试将您的页面上传到 Web 服务器(本地服务器也可以)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      • 2012-01-03
      相关资源
      最近更新 更多