是否可以在没有浏览器提示的情况下获取用户的地理位置?
Is it possible to get the geolocation of a user without the browser prompt?
这是来自 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()
的功能?
Is there any preventPrompt()
-like function ?
不,你不能阻止提示,它是一个安全功能,因为不是每个用户都想分享它的位置.
No you cant prevent the prompt, its a security feature cause not every user wanna share its location.
来自 W3C 文档:
符合本规范的实现必须提供保护用户隐私的机制,这种机制应该确保不会通过此 API 提供任何位置信息未经用户明确许可.
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之类的服务.
But you can try to use a service like geoip in the error callback.
这篇关于js 地理定位但没有提示 - 可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!