简体   繁体   English

带有MeteorJS的Google Maps v3加载同步问题

[英]Google Maps v3 with MeteorJS loading sync issue

My app is running on MeteorJS framework and using google maps (javascript api v3). 我的应用程序在MeteorJS框架上运行,并使用Google Maps(javascript api v3)。 Google maps loading scheme is similar to one explained in this post , and it's pretty analogous to the official tutorial . 谷歌地图加载方案类似于在此说明一个职位 ,而这是相当类似的官方教程 But sometimes on it's loading the app throwing a repeated exception: 但是有时在加载应用程序时会引发重复的异常:

Uncaught TypeError: Cannot read property 'lat' of null 未捕获的TypeError:无法读取null的属性“ lat”

the following piece of code cause it (unfortunately, minified): 下面的代码导致了它(不幸的是,缩小了):

function $H(a, b, c, d) {
    var e = c[B], f = new jB(d);
    f[p]("title", e);
    b[p]("draggableCursor", e, "cursor");
    var g = e.Nb;
    Q("click dblclick rightclick mouseover mouseout mousemove mousedown mouseup".split(" "), function(d) {
        S[z](b, d, function(e, q, s) {
            var v = a[Wp](e, !0);
            e = new U(v.lat(), v.lng()); //here, v is probably null 
        })
    })
}

I'm pretty sure that's loading synchronization issue: a) the app is working fine and the errors are thrown in only first seconds of loading. 我非常确定这是加载同步问题:a)应用运行正常,并且仅在加载的最初几秒钟就引发了错误。 b) this happens much frequently in production, naturally the loading times there are longer. b)这种情况在生产中经常发生,因此加载时间自然会更长。

PS I can link to my app if it'll help. 附言:如果有帮助,我可以链接到我的应用程序。

This is how I address google-maps v3 loading in my application, basically it involves declaring a reactive ready method on a GoogleMaps object that is set to true once we are sure the script is loaded. 这就是我处理应用程序中google-maps v3加载的方式,基本上它涉及到在确定脚本已加载后,在设置为true的GoogleMaps对象上声明反应式ready方法。

client/lib/google-maps.js :

GoogleMaps={
  // public methods
  config:function(options){
    _.extend(this,options);
  },
  ready:function(){
    this._loadingDependency.depend();
    return this._ready;
  },
  // private methods
  _loaded:function(){
    this._ready=true;
    this._loadingDependency.changed();
  },
  // public members
  apiKey:"",
  // private members
  _ready:false,
  _loadingDependency:new Deps.Dependency()
};

_googleMapsLoaded=function(){
  GoogleMaps._loaded();
};

Meteor.startup(function(){
  if(!GoogleMaps.apiKey){
    throw new Meteor.Error(-1,"API key not set, use GoogleMaps.config({apiKey:YOUR_API_KEY});");
  }
  $.getScript("https://maps.googleapis.com/maps/api/js?key="+GoogleMaps.apiKey+"&callback=_googleMapsLoaded");
});

Now we can use the ready method inside a GoogleMapsView template rendered callback : 现在,我们可以在GoogleMapsView模板rendered回调中使用ready方法:

client/views/google-maps-view/google-maps-view.js :

<template name="GoogleMapsView">
  <div class="google-maps-view"></div>
</template>

Template.GoogleMapsView.rendered=function(){
  this.autorun(_.bind(function(){
    if(GoogleMaps.ready()){
      this.mapOptions={
        center:new google.maps.LatLng(48.8582,2.2945),
        zoom:15
      };
      this.map=new google.maps.Map(this.find(".google-maps-view"),this.mapOptions);
    }
  },this));
};

Well the reason to the loading errors laid in initialize function. 好吧,加载错误的原因在于初始化函数。 The function created a map without map options, and when connection with Meteor server side was established, map options were added to the map. 该函数创建了没有地图选项的地图,并且当与Meteor服务器端建立连接时,地图选项已添加到地图中。 In this gap of time between map creation and options setting the errors appeared. 在地图创建和选项设置之间的这段时间间隔内,出现了错误。 The lesson is: add options to created map in the same code piece. 课程是:在同一代码段中向创建的地图添加选项。

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

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