如何在Web应用程序中请求HTML5地理位置api的用户权限? [英] How to request user permission for html5 geolocation api in web apps?

查看:169
本文介绍了如何在Web应用程序中请求HTML5地理位置api的用户权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码可以使用html5地理位置

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

我正在android手机上的chrome浏览器中对其进行测试.但是马上就说用户拒绝了许可.它甚至都没有问我是否要允许它.如何获得请求权限.我在网站设置中也要求该位置也获得许可.

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.

有人知道怎么了吗?

谢谢

推荐答案

这不是通过HTML或JavaScript完成的(这是一件好事).必须通过用户的应用程序设置来完成.否则,开发人员将能够绕过用户的意愿,将接受位置服务的代码编码到他们的应用程序中.

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.

这篇关于如何在Web应用程序中请求HTML5地理位置api的用户权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆