3

i am using cordova 6 with onsen-ui,jquery and javascript. So i am trying to make a simple login site, but i need to get if gps is active. I want to know WHERE i must do that validation. Now I do the following on ons.ready() event.

var options = {maximumAge: 0, timeout: 3000, enableHighAccuracy:true};
navigator.geolocation.getCurrentPosition(onGPSCheckSuccess, onGPSCheckError, options);

function onGPSCheckSuccess()
{
    console.log("Encontro el GPS Activado");
}

function onGPSCheckError()
{
    console.log("Error al chequear el GPS");
    gpsDetect.switchToLocationSettings(onSwitchToLocationSettingsSuccess,     onSwitchToLocationSettingsError);
}

function onSwitchToLocationSettingsSuccess() {
}

function onSwitchToLocationSettingsError(e) {
  console.log("Error al activar el GPS");
  alert("Error onSwitchToLocationSettingsError: "+e);
}

So if there a way to do this BEFORE my main page es loaded?

Regards

sonseiya
  • 488
  • 3
  • 14
  • 31
  • 1
    check device ready here -- http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html – Tasos Feb 13 '16 at 20:53

2 Answers2

1

Actually, you can do a simple page redirect. You can have that code in a .htm file, which you call before your page. When this succeeds, you then store the result using localStorage, then redirect to the page you want, and retrieve the results from localStorage.

Supreme Dolphin
  • 2,248
  • 1
  • 14
  • 23
0

I think the best way to do this, it with the following code:

document.addEventListener("init", function(event){
    if(event.target.id=='yourHomePageID') {
      var options = {maximumAge: 0, timeout: 3000, enableHighAccuracy:true};
      navigator.geolocation.getCurrentPosition(onGPSCheckSuccess, onGPSCheckError, options);
    }
},false);

By using the init function for the homepage, this actually runs a bit before ons.ready() as discussed here by @fran-dios:

Onsen 2.0 - Adding event listener to Ons-Switch with Javascript

Hope this helps. It is what I am doing based on the aforementioned question and it solved my issue.

Community
  • 1
  • 1
Munsterlander
  • 1,356
  • 1
  • 16
  • 29