【发布时间】:2019-09-17 10:58:32
【问题描述】:
我正在开发我的第一个 cordova 项目,并且正在模拟器上进行测试。
我需要使用 navigator.geolocation 来获取坐标。
我已经安装了 cordova 地理定位插件,它也在 config.xml 文件中被提及。 我已经在模拟器设置中授予对应用程序的位置访问权限,并且我还单击了模拟器扩展设置中的“发送”按钮。
尽管如此,地理位置仍然失败,我不知道为什么。
这里是js代码:
//第一次初始化入口页面并处理入口页面输入验证
$(document).delegate("#entry_page","pageinit",function()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(onSuccess, onError, {timeout: 30000});
}
//rest of code
if(latitude == null || longitude == null)
{
alert("Location not given. Please allow location access and refresh the application");
error_free = 0;
}
if(dateTime == null)
{
alert("Date & Time not acquired");
error_free = 0;
}
// remaining code
});
});
//Get location and time values on successful location access
function onSuccess(position)
{
latitude = position.coords.latitude;
longitude = position.coords.longitude;
today = new Date();
date = today.getDate()+'/'+(today.getMonth()+1)+'/'+today.getFullYear();
time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
dateTime = date+' '+time;
}
//Throw error if location access is not possible
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)
}
//Reinitialise entry_page when it is revisted again
$(document).on("pageshow", "#entry_page", function()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(onSuccess, onError, {timeout: 30000});
}
changeHeaderName("#chickenNameHeader");
clearFields();
});
附:我正在使用 Nginx 和 Nodejs。我的 Nginx 配置为 https。
【问题讨论】:
-
你的
error说什么? -
它请求许可,然后在我点击允许之前立即失败并说“位置不可用”
-
你的意思是错误对象说'位置不可用'?
-
是的。该错误被放入警报消息中
-
您在设备上试用吗?
标签: android cordova geolocation