簡體   English   中英

如何在Web應用程序中請求HTML5地理位置api的用戶權限?

[英]How to request user permission for html5 geolocation api in web apps?

我有這段代碼可以使用html5地理位置

    this.getCurrentLocation = function(callback) {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (pos) {
                callback({
                    'lat' : pos.coords.latitude,
                    'lng' : pos.coords.longitude        
                });
            }, function (error) {
                switch(error.code) {
                    case error.PERMISSION_DENIED:
                        alert("Could not get your location. User denied the request for Geolocation.");
                        break;
                    case error.POSITION_UNAVAILABLE:
                        alert("Could not get your location. Location information is unavailable.");
                        break;
                    case error.TIMEOUT:
                        alert("Could not get your location. The request to get user location timed out.");
                        break;
                    case error.UNKNOWN_ERROR:
                        alert("Could not get your location. An unknown error occurred.");
                        break;
                    default:
                        alert("Could not get your location. An unknown error occurred.");
                }
                callback(null);
            }, {
                maximumAge: 500000, 
                enableHighAccuracy:true, 
                timeout: 6000
            });
        }
    };

我正在Android手機上的Chrome瀏覽器中對其進行測試。 但是,馬上說用戶拒絕了許可。 它甚至不問我是否要允許它。 如何獲得請求權限。 我在網站設置中,也應要求該位置獲得許可。

如果我在台式機上嘗試,它會起作用。

有人知道怎么了嗎?

謝謝

這不是通過HTML或JavaScript完成的(這是一件好事)。 必須通過用戶的應用程序設置來完成。 否則,開發人員將能夠繞過用戶的意願,將接受位置服務的代碼編碼到他們的應用程序中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM