setTimeout回调中的异常:ReferenceError:未定义google

时间:2017-05-26 06:31:27

标签: jquery meteor google-maps-api-3 meteor-blaze geocomplete

我想我一遍又一遍地对这个问题撒谎。我希望我今天能找到帮助来解决这个问题。这已经好几天试图设置Geocomplete以使用我的Meteor js应用程序。这个相同的代码在非Meteor应用程序上运行得非常好,但它只是证明了Meteor js应用程序的顽固性。在阅读了论坛上的许多建议后进行了修改,我想出了这段代码。运行它给出了以下错误。请我完全需要在Meteor js上成功使用Geocomplete的人的专业支持。 Onrendered功能代码:

Template.geolayout.onRendered(function(){
    Meteor.setTimeout(function () {
        $(function () {
        $('body').append('<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyA55ihs_QPBpO5r465dgdfg1TJ0FX4ofL8zk&libraries=places">');
        $('body').append('<script src="/js/jquery.geocomplete.js">');

            $("#geocomplete").geocomplete({
              map: ".map_canvas",
              details: "form",
              types: ["geocode", "establishment"],
            });

            $("#find").click(function(){
              $("#geocomplete").trigger("geocode");
            });
        });
    }, 200);
});

Blaze代码

<template name="geolayout">
      <div class="map_canvas"></div>

    <form>
      <input id="geocomplete" type="text" placeholder="Type in an address" value="Empire State Bldg" />
      <input id="find" type="button" value="find" />

      <fieldset>
        <h3>Address-Details</h3>

        <label>Name</label>
        <input name="name" type="text" value="">

        <label>URL</label>
        <input name="url" type="text" value="">

        <label>Website</label>
        <input name="website" type="text" value="">
      </fieldset>
    </form>

</template>

控制台出错:

Exception in setTimeout callback: ReferenceError: google is not defined
    at GeoComplete.$.extend.initMap (eval at <anonymous> (http://localhost:3000/packages/jquery.js?hash=c57b3cfa0ca9c66400d4456b6f6f1e486ee10aad:397:22), <anonymous>:123:22)
    at GeoComplete.$.extend.init (eval at <anonymous> (http://localhost:3000/packages/jquery.js?hash=c57b3cfa0ca9c66400d4456b6f6f1e486ee10aad:397:22), <anonymous>:105:12)
    at new GeoComplete (eval at <anonymous> (http://localhost:3000/packages/jquery.js?hash=c57b3cfa0ca9c66400d4456b6f6f1e486ee10aad:397:22), <anonymous>:99:10)
    at HTMLInputElement.eval (eval at <anonymous> (http://localhost:3000/packages/jquery.js?hash=c57b3cfa0ca9c66400d4456b6f6f1e486ee10aad:397:22), <anonymous>:586:22)
    at Function.jQuery.extend.each (http://localhost:3000/packages/jquery.js?hash=c57b3cfa0ca9c66400d4456b6f6f1e486ee10aad:442:23)
    at jQuery.fn.jQuery.each (http://localhost:3000/packages/jquery.js?hash=c57b3cfa0ca9c66400d4456b6f6f1e486ee10aad:194:17)
    at $.fn.geocomplete (eval at <anonymous> (http://localhost:3000/packages/jquery.js?hash=c57b3cfa0ca9c66400d4456b6f6f1e486ee10aad:397:22), <anonymous>:582:19)
    at HTMLDocument.<anonymous> (http://localhost:3000/app/app.js?hash=a15884274aa7ef8a6a17bc31acbc671588e8bcb5:568:22)
    at fire (http://localhost:3000/packages/jquery.js?hash=c57b3cfa0ca9c66400d4456b6f6f1e486ee10aad:3201:30)
    at Object.self.add [as done] (http://localhost:3000/packages/jquery.js?hash=c57b3cfa0ca9c66400d4456b6f6f1e486ee10aad:3247:7)

1 个答案:

答案 0 :(得分:1)

只需移动main.html末尾的2个脚本标记即可。这应该有效,让你的生活更轻松。

<body>
  {{> geolayout}}

  <script src="http://maps.googleapis.com/maps/api/js?key=YOUR_KEY&libraries=places"></script>
  <script src="/js/jquery.geocomplete.js"></script>
</body>

用您的API密钥替换YOUR_KEY。