1

我已经使用 JQM (1.3.1)、jQuery(1.9)、CSS3 和 javascript 编写了一个 webapp,现在我正处于第二阶段,我正在使用 phone gap (2.7) 将此应用程序移植到 Apple 商店(目标 iOS(6.1)/ iPhone5 - 问题可以在 iPhone 4S/iOS 6.0 上重现

如下所示的我的地理位置代码在浏览器中执行时运行良好。

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    
}

但是,一旦通过phonegap,我的应用程序就会两次请求用户访问位置服务的权限,即它显示了两次弹出窗口。因此,为了解决我在stackoverflow上的许多解决方案中建议的使用下面的代码。

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

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

这解决了弹出问题,但又开始了一个新问题。

问题:如果位置服务被禁用,我的代码会添加 deviceready 侦听器并等待 deviceready 事件触发,但该事件永远不会触发,它只是坐在那里。如果定位服务已打开,它将按预期工作。

有没有人遇到过这个问题,你能告诉我你做了什么来解决它,请我已经投入了一天值得调试和研究的时间。

亲切的问候,谢谢。

4

1 回答 1

2

问题解决了!:)

事实证明,我没有完全按照 phonegap build 文章中的描述包含 phone gap 库。

检查这篇文章https://build.phonegap.com/docs/preparing-your-app

您不需要在每个页面上都包含此库,而只需在索引页面上包含此库,我不确定它有何不同,但是在我删除了任何其他页面上对 phonegap.js 的任何引用后,“deviceready”事件开始触发,但索引.html。

我注意到的第二件事是,由于某种原因,我的错误处理如下所示在 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;
}

我不得不把它改成下面

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;
}

希望这可以帮助某人。谢谢。

于 2013-06-04T11:51:52.613 回答