javascript-js地理位置定位但没有提示-可能吗?

是否可以在没有浏览器提示的情况下获得用户的地理位置?

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之类的服务.

上一篇:蓝桥杯比赛javaB组练习《四平方和》


下一篇:【js 是否手机】JavaScript判读当前是否是手机端