【问题标题】:Problem with navigator.geolocation failing in cordova project科尔多瓦项目中 navigator.geolocation 失败的问题
【发布时间】: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


【解决方案1】:

如果您的android版本>= Android M,您需要先向用户询问运行时位置权限。

使用this cordova plugin 请求位置权限。

var permissions = cordova.plugins.permissions;

permissions.hasPermission(permissions.ACCESS_COARSE_LOCATION, function(status){

   if (!status.hasPermission) {

    //Does not have the required permission, request for the same
    permissions.requestPermission(permissions.ACCESS_COARSE_LOCATION, 
       function(status) {
         if(status.hasPermission) {
            //Has permission already, fetch the location
         }
       }, function () {

   });

  }
  else {
    //Has permission already, fetch the location
  }
});

【讨论】:

  • 我会试试这个,让你知道它是否有效。 M,我猜你是指棉花糖?
  • @ShaneD'Silva 是的棉花糖
【解决方案2】:

使用此代码对我来说效果很好:

navigator.geolocation.getCurrentPosition(position => {
  const { latitude, longitude } = position.coords;
  // Show a map centered at latitude / longitude.
  alert(latitude + '==' + longitude);
});

我正在使用对象解构 (ES6)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-27
    • 2018-01-30
    • 1970-01-01
    • 2014-12-29
    • 2014-04-23
    • 1970-01-01
    相关资源
    最近更新 更多