简体   繁体   English

如果关闭位置服务,则不会触发Phonegap构建“ deviceready”事件

[英]Phonegap build “deviceready” event never fires if the location service is turned off

I have written a webapp using JQM (1.3.1), jQuery(1.9), CSS3 and javascript now I am in the second phase where I am porting this app to Apple store using phone gap (2.7) (target iOS(6.1)/iPhone5 - Issue can be reproduced on iPhone 4S/ iOS 6.0 我已经使用JQM(1.3.1),jQuery(1.9),CSS3和javascript编写了一个Webapp,现在我处于第二阶段,我正在使用手机差距(2.7)(目标iOS(6.1)/ iPhone5-问题可以在iPhone 4S / iOS 6.0上重现

My geolocation code as shown below worked fine when it was executed in a browser. 当在浏览器中执行时,如下所示的地理位置代码可以正常工作。

var geoOptions = { 'enableHighAccuracy': true, 'timeout': 10000, 'maximumAge': 0 };
navigator.geolocation.getCurrentPosition(geoSuccess, geoError, geoOptions);

function geoSuccess(postion)
{
  //on success code here    
}
function geoError(error)
{
  //on error code here    
}

However once passed through phonegap then my app requested user permission to access the location service twice ie it showed a pop up twice. 但是,一旦通过phonegap,我的应用就会请求用户两次访问位置服务的权限,即显示两次弹出窗口。 Hence to resolve that I used the code below as suggested in many solutions on stackoverflow. 因此,为了解决这一问题,我使用了许多关于stackoverflow的解决方案中所建议的以下代码。

function onDeviceReady() {
 navigator.geolocation.getCurrentPosition(geoSuccess, geoError,geoOptions);
}

$(function() {
 document.addEventListener("deviceready", onDeviceReady, false);
});

This resolved the pop up issue however started a new one. 这解决了弹出问题,但是开始了新的问题。

Issue: if the location service is disabled my code adds the deviceready listener and waits for the deviceready event to fire however the event never fires and it just sits there. 问题:如果禁用了位置服务,我的代码将添加deviceready侦听器并等待deviceready事件触发,但是该事件从不触发,它就坐在那儿。 If the location service is turned on it works as expected. 如果打开了定位服务,它将按预期工作。

Has anyone faced this issue before can you please suggest me what you did to resolve it, please I have already invested a day worth debug and research time in it. 有没有人遇到过这个问题,在您能提出建议之前,我是否可以解决这个问题,请问我已经花了一天的时间进行调试和研究。

Kind regards and thank you. 亲切的问候,谢谢。

Issue resolved! 问题解决了! :) :)

Turns out I was not including the phone gap library exactly as described in the phonegap build article. 事实证明,我并没有完全按照phonegap构建文章中的描述包括电话空白库。

Check this article https://build.phonegap.com/docs/preparing-your-app 检查本文https://build.phonegap.com/docs/preparing-your-app

You do not need to include this library on each page but just the index page, I am not sure how it makes any difference but then the "deviceready" event started firing after I removed any references to phonegap.js on any other page but the index.html. 您无需在每个页面上都包括此库,而只需在索引页面上就可以了,我不确定它有什么区别,但是在删除了其他任何页面上对phonegap.js的引用之后,“ deviceready”事件就开始触发,但是index.html的。

Second thing that I noticed was that for some reason my error handling as shown below stopped working after phonegap build. 我注意到的第二件事是由于某种原因,在构建phonegap之后,如下所示的错误处理停止了工作。

switch(error.code) 
{
  case error.PERMISSION_DENIED:
   search.openPanelForSearch("Either the app was denied permission or the location srvice is currently turned off.", showInitialMap);
   break;
  case error.POSITION_UNAVAILABLE:
   search.openPanelForSearch("Geolocation information was unavailable. Would you like to try out a manual serach instead?", showInitialMap);
   break;
  case error.TIMEOUT:
   search.openPanelForSearch("Service was timed out since it took too long to retrieve the gelolcations. Would you like to try out a manual search instead?", showInitialMap);
   break;
  case error.UNKNOWN_ERROR:
   search.openPanelForSearch("Sorry an unknown error occurred. Would you like to try out a manual serach instead?", showInitialMap);
   break;
}

I had to change it to below 我必须将其更改为以下

switch(error.code) 
{
  case 1:
   search.openPanelForSearch("Either the app was denied permission or the location service is currently turned off.", showInitialMap);
   break;
  case 2:
   search.openPanelForSearch("Geolocation information was unavailable. Would you like to try out a manual search instead?", showInitialMap);
   break;
  case 3:
   search.openPanelForSearch("Service was timed out since it took too long to retrieve the gelolcations. Would you like to try out a manual serach instead?", showInitialMap);
   break;
  default:
   search.openPanelForSearch("Sorry an unknown error occurred. Would you like to try out a manual search instead?", showInitialMap);
   break;
}

Hope this helps someone. 希望这对某人有帮助。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM