在我的代码段here中,
我正在测试navigator.geolocation
功能。但是,Web浏览器甚至没有提示我在加载后提供位置权限。这是为什么?
这是我的代码:
var x = $("#location");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
答案 0 :(得分:0)
只需使用jQuery的html()方法而不是innerHTML。
var x = $("#location");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, error);
} else {
x.html("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
console.log(position)
x.html("Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude);
}
function error(err){
console.log(err)
}
getLocation();