1

我想知道在哪里放置 Tracker.autorun 以保证之前加载了其他所有内容。我以为

Meteor.startup(function(){...});

用于这种情况,但是当我从 mdg:geolocation 引用 Geolocation 对象时,它告诉我它尚未定义:

未捕获的类型错误:无法读取 null 的属性“lng”

我使用以下解决方法,但我希望有一个更优雅的解决方案:

 Meteor.startup(function () {
    trackerGeolocationInit = setInterval(enableLocationTracking, 100);
});

enableLocationTracking = function(){

    var location = Geolocation.latLng();
    if(location === null)
        return;
    else
        clearInterval(trackerGeolocationInit);

    Tracker.autorun(function () {
        var location = Geolocation.latLng();
        Meteor.users.update(Meteor.userId(), {
            $set: {
                "profile.location": {
                    type: 'Point',
                    coordinates: [location.lng, location.lat]
                }
            }
        });
    });
}
4

1 回答 1

2

我正在编写一个有趣的应用程序,这很有效:

   Tracker.autorun(function () {
      if(Meteor.userId())
      {
        var latLng = Geolocation.latLng();
        var userId = Meteor.userId();
        if(latLng &&  userId)
        {
          //do something
        }
     }
    });

无需使用间隔。我只是放入了一个名为 geolocation.js 的文件。

于 2015-12-10T15:45:31.297 回答