简体   繁体   中英

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

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. Hence to resolve that I used the code below as suggested in many solutions on 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. 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.

Check this article 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.

Second thing that I noticed was that for some reason my error handling as shown below stopped working after phonegap build.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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