简体   繁体   English

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

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

I want to be able to set the center of a Google Map based on coordinates stored in a Mongo collection. 我希望能够基于存储在Mongo集合中的坐标来设置Google Map的中心。

I use Meteor as framework. 我使用Meteor作为框架。 Inside my Template.Home.helpers, I have the following function to get the coordinates: 在我的Template.Home.helpers内部,我具有以下函数来获取坐标:

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

How can I pass this value into this mapOptions function, also inside the same template helper: 我如何将这个值传递到这个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
      };
    }
  }

Can I store mapCenter as a variable and put it into the center in the mapOptions function? 我可以将mapCenter存储为变量,然后将其放在mapOptions函数的中心吗?

Four basic options: 四个基本选项:

  1. Save the coordinates into a Session variable then pick them up later 将坐标保存到Session变量中,然后再取用
  2. Save the coordinates into a closure 将坐标保存到闭包中
  3. Save the coordinates into a reactive variable 将坐标保存到反应变量
  4. Save the coordinates in a document in a collection 将坐标保存在集合中的文档中

Session variable example: 会话变量示例:

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
      };
    }
  } 

This approach has some limitations however since your Session variable is global in scope. 但是,由于您的Session变量的作用域是全局的,因此这种方法有一些局限性。 If the user has multiple tabs open with multiple maps then you might end up with all the maps being the same. 如果用户打开了带有多个地图的多个选项卡,则最终所有地图都相同。 Then a closure might be better. 那么关闭可能会更好。

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

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