簡體   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