gpt4 book ai didi

javascript - Navigator.geoLocation.getCurrentPosition 不工作的问题

转载 作者:行者123 更新时间:2023-11-30 19:20:30 25 4
gpt4 key购买 nike

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

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

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

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

附言我的网络应用程序托管在我电脑的本地主机上,我已将手机连接到电脑的热点以访问本地主机。另外,我使用的是华为 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;
# }
#}

}

最佳答案

你的问题是 Geolocation API仅适用于 HTTPS(安全上下文),不适用于 HTTP,因此拒绝许可。
来自docs ,

This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

另请参阅您的 Browser Compatibilty .

编辑:

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

关于javascript - Navigator.geoLocation.getCurrentPosition 不工作的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57532887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com