我有这段代码可以使用 html5 地理定位
this.getCurrentLocation = function(callback) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (pos) {
callback({
'lat' : pos.coords.latitude,
'lng' : pos.coords.longitude
});
}, function (error) {
switch(error.code) {
case error.PERMISSION_DENIED:
alert("Could not get your location. User denied the request for Geolocation.");
break;
case error.POSITION_UNAVAILABLE:
alert("Could not get your location. Location information is unavailable.");
break;
case error.TIMEOUT:
alert("Could not get your location. The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
alert("Could not get your location. An unknown error occurred.");
break;
default:
alert("Could not get your location. An unknown error occurred.");
}
callback(null);
}, {
maximumAge: 500000,
enableHighAccuracy:true,
timeout: 6000
});
}
};
我正在 Android 手机上的 chrome 浏览器上对其进行测试。但它马上就说用户拒绝了许可。它甚至不问我是否要允许它。我怎样才能让它请求许可。我在网站设置中,该位置也应该请求许可。
如果我在桌面上尝试,它会起作用。
谁知道这是怎么回事?
谢谢
最佳答案
这不是通过 HTML 或 JavaScript 完成的(这是一件好事)。它必须通过用户的应用程序设置来完成。否则,开发人员将能够绕过用户的意愿,将接受使用位置服务的代码编码到他们的应用程序中。
关于javascript - 如何在网络应用程序中请求用户对 html5 geolocation api 的许可?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42043363/