使用 localhost 本机访问没问题。一旦换成 ip 地址访问就不行。报用户拒绝访问的错。丢到服务器上也是这样。、
$(function(){
getLocation();
});
function getLocation(){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition,showError);
}else{
alert("浏览器不支持地理定位。");
}
}
function showPosition(position){
var lat = position.coords.latitude; //纬度
var lag = position.coords.longitude; //经度
alert('纬度:'+lat+',经度:'+lag);
}
function showError(error){
switch(error.code) {
case error.PERMISSION_DENIED:
alert("定位失败,用户拒绝请求地理定位");
break;
case error.POSITION_UNAVAILABLE:
alert("定位失败,位置信息是不可用");
break;
case error.TIMEOUT:
alert("定位失败,请求获取用户位置超时");
break;
case error.UNKNOWN_ERROR:
alert("定位失败,定位系统失效");
break;
}
}
上代码 每次输出,用户拒绝请求地理定位
1
cnqncom 2016 年 8 月 16 日
关注哈子,我好像没有遇到这样的情况
|
2
DualWield 2016 年 8 月 16 日
代码这样基本不会有人看了
|
3
wanderingFaker OP @DualWield 为什么呢 小白一枚
|
4
ziki 2016 年 8 月 16 日
刚好遇到这个问题,貌似是新版的 Chrome 只允许 localhost 域或者是走 https 的链接在允许获取地理位置信息
|
5
morethansean 2016 年 8 月 16 日
|
6
fahai 2016 年 8 月 16 日
getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. See https://goo.gl/rStTGz for more details.
|
7
DualWield 2016 年 8 月 16 日
@wanderingFaker 代码没有对齐
|
8
zander1024 2016 年 8 月 16 日
$(function() {
getLocation(); }); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition, showError); } else { alert("浏览器不支持地理定位。"); } } function showPosition(position) { var lat = position.coords.latitude; //纬度 var lag = position.coords.longitude; //经度 alert('纬度:' + lat + ',经度:' + lag); } function showError(error) { switch (error.code) { case error.PERMISSION_DENIED: alert("定位失败,用户拒绝请求地理定位"); break; case error.POSITION_UNAVAILABLE: alert("定位失败,位置信息是不可用"); break; case error.TIMEOUT: alert("定位失败,请求获取用户位置超时"); break; case error.UNKNOWN_ERROR: alert("定位失败,定位系统失效"); break; } } ------------------------------------------------ 我帮你格式化下。。 |
9
zander1024 2016 年 8 月 16 日
诶 卧槽 明明格式化好的。。
|
10
bdbai 2016 年 8 月 16 日 via Android
@zander1024 用 Gist 或者各种 Paste
|