0

I am working in a meteor project(1.5.1) and using mdg:geolocation@1.3.0, and i am trying to get the value of Geolocation.currentLocation() but it is giving me null value and the behaviour is not the same for all the time, sometime it is giving me null value and sometime giving me proper value.

I have research for the long time, but not got the solution yet, if possible please provide solution with meteor@1.5.1 and with use of mdg:geolocation package.

Thanks in advance...

Rajit Garg
  • 13
  • 3

1 Answers1

0

MDG has implemented mdg:geolocation for continuous geolocation and functions in this Meteor package return a reactive var. You can use the package this way (for example):

// Continous geolocation with mdg:geolocation
// This code will run every time user location changes.

Tracker.autorun(() => {
  const position = Geolocation.currentLocation();

  Tracker.nonreactive(() => {
    if (position) {
      Meteor.call('user.update.position', position);
    }
  });
});

If you don't need continous geolocation, You can use javascript method instead of meteor package. See this answer for other options with windows.navigator.geolocation. In this case you may need to add cordova-plugin-geolocation in case you are building Android or iOS apps

// Navigation geolocation to get geolocation only once
let errorCallback;
let successCallback;

successCallback = position => Meteor.call('user.update.position', position);
errorCallback = err => console.log(err);

navigator.geolocation.getCurrentPosition(successCallback, errorCallback, {
  maximumAge: 60000,
  timeout: 20000
});
Violeta
  • 140
  • 1
  • 8