【问题标题】:Problem with Navigator.geoLocation.getCurrentPosition not workingNavigator.geoLocation.getCurrentPosition 无法正常工作的问题
【发布时间】:2019-12-23 06:22:12
【问题描述】:

我正在制作一个名为“Chicken Logger”的小型移动网络应用程序,用户可以在其中选择特定类型的鸡肉并在下一页中输入相关数据。

我的问题是我需要使用Navigator.geolocation 来获取纬度和经度。

代码在我的桌面 chrome 浏览器上运行良好,但在我的手机上由于某种原因它不断抛出错误 Location permission denied

我已尝试清除和重置手机上的网站设置,但仍然无法正常工作。我确保也授予 Chrome 位置访问权限。

附:我的 Web 应用程序托管在我的 PC 的本地主机上,并且我已将手机连接到我的 PC 的热点以访问本地主机。另外,我使用的是华为 Mate 20 Pro。

基本上用户一打开“入口页面”,就会被要求提供位置权限。但我没有得到询问。直奔错误。

下面是我的 JS 代码:

$(document).delegate("#entry_page","pageinit",function()
{

  if (navigator.geolocation) {
       navigator.geolocation.getCurrentPosition(onSuccess, onError);
    }
});


function onSuccess(position)
{
  latitude = position.coords.latitude;
  longitude = position.coords.longitude;
  alert(latitude);
  today = new Date();
  date = today.getDate()+'/'+(today.getMonth()+1)+'/'+today.getFullYear();
  time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
  dateTime = date+' '+time;
  alert(dateTime);



}

function onError(error) {
  var txt;
  switch(error.code)
  {
    case error.PERMISSION_DENIED:
    txt = 'Location permission denied';
    break;
    case error.POSITION_UNAVAILABLE:
    txt = 'Location position unavailable';
    break;
    case error.TIMEOUT:
    txt = 'Location position lookup timed out';
    break;
    default:
    txt = 'Unknown position.'
  }
  alert(txt)
}

Nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

【问题讨论】:

    标签: javascript jquery html nginx localhost


    【解决方案1】:

    您的问题是 Geolocation API 仅适用于 HTTPS安全上下文),不适用于 HTTP,因此它拒绝了权限。
    来自docs,

    此功能仅在安全上下文 (HTTPS) 中可用,在某些或 所有支持的浏览器。

    另请参阅您的Browser Compatibilty

    编辑:

    要启用HTTPSNginx,您必须生成SSL 证书并修改配置文件。详细步骤请关注this manual

    【讨论】:

    • 哇哦。那么你是说我的本地主机使用的是 https 吗?但是当我的服务器为我的手机提供服务时,它不是 https 吗?我该如何解决?我需要它在我的手机上工作,因为这是一项任务
    • 你的服务器配置是什么?可以分享一下吗?
    • 我使用 Nginx 作为我的网络服务器。我在哪里可以找到服务器配置?我对此很陌生,对不起
    • 我已经编辑了答案,这是针对 Ubuntu,你的操作系统是什么?
    • 是的,您必须取消注释并添加您自己生成的 SSL 证书,我会更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 2018-02-14
    相关资源
    最近更新 更多