对于我的Chrome扩展程序,我正在尝试显示用户位置。我的js代码如下
document.addEventListener("DOMContentLoaded",function(loc){
var x = document.getElementById(demo);
getLocation();
});
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;
}
并在mainfest.json中添加了权限
"permissions": [
"tabs", "<all_urls>",
"geolocation"
但它没有显示任何东西。请告诉我。
答案 0 :(得分:0)
查看https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation
您只需要允许您的Chrome访问您的位置。