0

I'm working on my first cordova project and I'm testing on an emulator.

I need to use navigator.geolocation to get the coordinates.

I have installed the cordova geolocation plugin and it's also mentioned in the config.xml file. I've given location access to the app in the emulator settings and I also click the "send" button in the the emulator's extended settings.

Despite this, the geolocation keeps failing and I don't know why.

Here is the js code:

//Initialise entry page for the first time and handle entry page input validation

$(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();

});

P.S. I'm using Nginx and Nodejs. My Nginx is configured for https.

Shane D'Silva
  • 405
  • 1
  • 4
  • 11

2 Answers2

0

If your android version is >= Android M, you need to ask user for the run-time location permission first.

Use this cordova plugin to request the location permission.

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

Worked fine for me with this code:

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

I'm using Object destructuring (ES6).

ihsandulu
  • 61
  • 1
  • 2