【问题标题】:Meteor Geolocation method from Event来自 Event 的 Meteor Geolocation 方法
【发布时间】:2015-07-20 16:06:26
【问题描述】:

我想得到 latLng,但只能在一个事件之后。如何做到这一点?我已经尝试过跟踪器等,但没有任何效果。唯一有效的是调用 Geolocation.latLng();从助手内部,在事件之前。

这就是我希望它工作的方式。我用 Session.set() 和 Session.get()) 尝试过同样的事情。我也尝试过使用 Tracker 依赖项,但由于该位置无法立即触发 changed() 并没有帮助。

我应该说明我正在使用由位于 https://github.com/meteor/mobile-packages/ 的 Meteor 开发组创建的包。

var location = {};

Template.Home.helpers({
  'location': function() {
    return location;
  }
);
Template.Home.events({
  'focus .location': function() {
    location =  Geolocation.latLng();
  },
});

【问题讨论】:

  • Geolocation 是什么?它在哪里定义?你在使用模块吗?

标签: meteor


【解决方案1】:

我喜欢@ZuzEL 的回答,但如果你真的想用 Sessions 来做的话:

Template.Home.helpers({
  'location': function() {
    return Session.get("location");
  }
);
Template.Home.events({
  'focus .location': function() {
    Session.set("location", Geolocation.latLng());
  },
});

不需要 ReactiveVar 包,因为 Session 本身就像全局响应式 :)

【讨论】:

  • 我正在尝试这个,但不相信它有效 - 我会重新考虑。
【解决方案2】:

这是因为您的 location 本身不是反应变量。

var location = new ReactiveVar();

Template.Home.helpers({
  'location': function() {
    return location.get();
  }
);
Template.Home.events({
  'focus .location': function() {
    location.set(Geolocation.latLng());
  },
});

不要忘记包含响应式 var 包

流星添加反应变量

但是,由于您使用的是 mdg:geolocation

并且here API Doc 说每个方法都是响应式的,无论位置发生变化,您都可以在onRendered 回调中使用跟踪器

    Template.Home.onRendered(function(){
      this.autorun(function(){
         location.set(Geolocation.latLng());
      })
    });

【讨论】:

    猜你喜欢
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 2023-01-02
    • 2021-10-13
    相关资源
    最近更新 更多