問題描述
是否可以在沒有瀏覽器提示的情況下獲取用戶的地理位置?
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模板網!