gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:35:05 25 4
gpt4 key购买 nike

我的应用程序在 MeteorJS 框架上运行并使用谷歌地图 (javascript api v3)。谷歌地图加载方案类似于此 post 中解释的方案,它非常类似于官方 tutorial .但有时在加载应用程序时会重复抛出异常:

Uncaught TypeError: Cannot read property 'lat' of null

下面的一段代码导致了它(不幸的是,缩小了):

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

我很确定这是加载同步问题:a) 应用程序运行良好,并且仅在加载的前几秒内抛出错误。 b) 这种情况在生产中经常发生,自然加载时间更长。

附言如果有帮助,我可以链接到我的应用程序。

最佳答案

这就是我在我的应用程序中处理 google-maps v3 加载的方法,基本上它涉及在 GoogleMaps 对象上声明一个响应式 ready 方法一旦我们确定脚本已加载,即设置为 true。

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

现在我们可以在 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));
};

关于javascript - 带有 MeteorJS 加载同步问题的 Google Maps v3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25536203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com