0

i try to set so geolocation informations in an helper. I use these packages:

mdg:geolocation, aldeed:geocoder

This is my helper:

Template.registerHelper("geo", function () {
  var geo = Geolocation.latLng() || { lat: 0, lng: 0 };

  if(geo.lat !== 0){
    var infos = Meteor.call('locationName', geo.lat, geo.lng, function(err, res){
        return res[0];
    });
  }
});

This is the object returned

enter image description here

And this is my server call

Meteor.methods({
  locationName: function(lat, lng){
  console.log(lat, lng);
  var geo = new GeoCoder({
    geocoderProvider: configs.geocoder.geocoderProvider,
    httpAdapter: configs.geocoder.httpAdapter,
    apiKey: configs.geocoder.apiKey
  });

  var reverseGeocoding = geo.reverse(lat, lng);

  return reverseGeocoding;
}
});

I tried a lot of thing but my {{geo.city}} is always empty. How can i structure it correctly?

Thank you

Community
  • 1
  • 1
Mike Boutin
  • 5,297
  • 12
  • 38
  • 65

2 Answers2

0

You are in a typical issue of method call within a helper. Look at your template helper's code. It basically does this:

Template.registerHelper("geo", function () {
  // inside helper
  // doing some stuff...
  Meteor.call('someMethod', function(err, res){
    // Other scope!
    console.log("Toto, I've a feeling we're not in the `geo` helper any more.");
    return res; // returning this to nowhere
  });
  // back inside helper
  return undefined; // returns before the above callback even triggers
});

The issue is that you cannot return something from a method call's callback and expect the helper from which you called that method to return this value: they're different scopes, because method calls are asynchronous. Here is what you can do:

Template.registerHelper("geo", function () {
  return Session.get('locationName');
});

updateLocationName = function (geo) {
  return Meteor.call('locationName', geo.lat, geo.lng, function(err, res){
        Session.set('locationName', res[0]);
  });
}

if (Meteor.isClient) {
  Meteor.startup(function () {
    var geo = Geolocation.latLng() || { lat: 0, lng: 0 };
    Session.setDefault('locationName', null);
    if(geo.lat !== 0){
      var infos = updateLocationName(geo);
    }
  });
}

If you want to update your session variable (inside an onRendered or autorun for example), you will just have to call updateLocationName() with an updated Geolocation.latLng() object.

Community
  • 1
  • 1
SylvainB
  • 4,765
  • 2
  • 26
  • 39
  • It doesn't work. Thank you for your answer about the scope, i tried a lot of things... In Meteor.startup, the geo variable is set to { lat: 0, lng: 0 }. I need to set it at first load. – Mike Boutin Aug 25 '15 at 17:58
0

I made it worked, but maybe it have a better way to do it. Thank you for your answer! There's my code

Template.registerHelper("geo", function () {

if(Session.get('geolocation') === null){
    var geo = Geolocation.latLng() || { lat: 0, lng: 0 };

    if(geo.lat !== 0){
        Meteor.call('locationInfos', geo.lat, geo.lng, function(err, res){
            Session.set('geolocation', res[0]);
        });
    }
}

return Session.get('geolocation');

});

I set the session variable at NULL in Meteor.startup.

Mike Boutin
  • 5,297
  • 12
  • 38
  • 65