Navigator.geoLocation.getCurrentPosition无法正常工作的问题

时间:2019-08-17 02:39:11

标签: javascript jquery html nginx localhost

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

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

该代码在我的桌面chrome浏览器上可以正常工作,但是由于某些原因,在手机上它总是抛出错误Location permission denied

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

P.S。我的Web应用程序托管在PC的localhost上,并且我已将手机连接到PC的热点以访问localhost。另外,我正在使用Huawei 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;
    #    }
    #}

}

1 个答案:

答案 0 :(得分:1)

您的问题是Geolocation API仅在SUBSTRING(value, start[, length])安全上下文)上可用,而在HTTPS上不起作用,因此它拒绝了权限。
来自docs

  

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

另请参阅您的Browser Compatibilty

编辑:

要为HTTP启用HTTPS,必须生成Nginx证书并修改配置文件。按照this manual进行详细操作。