简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM