我正在尝试实施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);
};
答案 0 :(得分:1)
从file:/// URI
使用地理位置有一些限制。尝试在Web服务器上上传您的页面(本地服务器也可以)。