是否可以在没有浏览器提示的情况下获得用户的地理位置?
Here’s the code sample from W3
<script>
var x = document.getElementById("demo")
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;
}
</script>
是否有任何类似于preventPrompt()的函数?
解决方法:
不,您无法阻止该提示,它的安全功能导致并非每个用户都希望共享其位置.
从W3C docs开始:
A conforming implementation of this specification must provide a
mechanism that protects the user’s privacy and this mechanism should
ensure that no location information is made available through this API
without the user’s express permission.
但是您可以尝试在错误回调中使用诸如geoip之类的服务.