繁体   English   中英

从返回值到Google Maps函数的价值

[英]Value from return, into a Google maps function

我希望能够基于存储在Mongo集合中的坐标来设置Google Map的中心。

我使用Meteor作为框架。 在我的Template.Home.helpers内部,我具有以下函数来获取坐标:

mapCenter: function () {
  return (this.map); //returns -37.8136, 144.9631
}

我如何将这个值传递到这个mapOptions函数中,也可以在同一模板帮助器中进行传递:

mapOptions: function() {
    // Make sure the maps API has loaded
    if (GoogleMaps.loaded()) {
      // Map initialization options
      return {
        center: new google.maps.LatLng(XX.XXXX,XX.XXXX),
        zoom: 8
      };
    }
  }

我可以将mapCenter存储为变量,然后将其放在mapOptions函数的中心吗?

四个基本选项:

  1. 将坐标保存到Session变量中,然后再取用
  2. 将坐标保存到闭包中
  3. 将坐标保存到反应变量
  4. 将坐标保存在集合中的文档中

会话变量示例:

mapCenter: function () {
  Session.set('coordinates',this.map);
  return (this.map); //returns -37.8136, 144.9631
}

mapOptions: function() {
    // Make sure the maps API has loaded
    if (GoogleMaps.loaded()) {
      // Map initialization options
      return {
        var coord = Session.get('coordinates');
        center: new google.maps.LatLng(coord.latitude,coord.longitude),
        zoom: 8
      };
    }
  } 

但是,由于您的Session变量的作用域是全局的,因此这种方法有一些局限性。 如果用户打开了带有多个地图的多个选项卡,则最终所有地图都相同。 那么关闭可能会更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM