js geolocation but without prompting - possible?(js 地理定位但没有提示 - 可能吗?)
问题描述
是否可以在没有浏览器提示的情况下获取用户的地理位置?
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 地理定位但没有提示 - 可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:js 地理定位但没有提示 - 可能吗?


基础教程推荐
- Bokeh Div文本对齐 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01