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

北慕城南 提交于 2020-05-15 07:37:22

问题


I have this code to use html5 geolocation

    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
            });
        }
    };

I'm testing it on my chrome browser on android phone. But it right away goes to saying user denied permission. It doesn't even ask if I want to allow it. How can I get it to request permission. I have in the site settings, that the location should be asked for permission too.

It works if I try on a desktop.

Does anyone know what's wrong?

Thanks


回答1:


This is not done via HTML or JavaScript (and that's a good thing). It must be done via the user's app settings. Otherwise, developers would just be able to code acceptance of the use of location services into their apps, bypassing the user's wishes.



来源:https://stackoverflow.com/questions/42043363/how-to-request-user-permission-for-html5-geolocation-api-in-web-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!