【问题标题】:angular2 embed google mapsangular2嵌入谷歌地图
【发布时间】:2016-10-27 20:33:24
【问题描述】:

我想在不使用 angular2-google-maps 组件的情况下将谷歌地图集成到我的应用程序中。我尝试按照入门教程进行操作

Google maps - Hello world

当我将教程中的两个脚本放在 index.html 文件中并创建一个地图组件时,它基本上只链接到一个带有 <div id="map"></div> 标签的 html 文件,我收到以下错误:

TypeError: Cannot read property 'offsetWidth' of null.

这可能是由于在加载实际 div 之前执行的脚本。加载完所有元素后,如何使这些脚本执行?将脚本添加到我的地图组件的 html 部分似乎不起作用。

谢谢!

【问题讨论】:

标签: google-maps angular


【解决方案1】:

尝试从 google maps 脚本标签中删除 async 属性。 如果还是不行,可以试试这个Synchronous Loading

祝你好运。

【讨论】:

  • 我试过这个,但它不起作用。我认为问题在于地图 div 位于脚本之后加载的另一个组件中。此外,无法在组件 html 中运行脚本也阻碍了我。
  • 你能把你的代码放在js fiddle上吗,我帮你会容易得多。
【解决方案2】:

您可以尝试使用让 jQuery 与 Angular 2 一起运行的相同方法。

使用setTimeout:This let's the jQuery run in the next execution cycle after Angular has finished loading all the child components.

export class HomePage {

constructor() {
    setTimeout(() => {

   // your code here 

    }, 0);
}
}

【讨论】:

    【解决方案3】:

    你可以试试AGM或@angular/google-maps

    我个人更喜欢第二个选项,但两个链接都很好地解释了如何在 Angular 应用中实现 gmap。

    【讨论】:

      猜你喜欢
      • 2012-10-31
      • 2011-12-01
      • 1970-01-01
      • 2011-09-02
      • 2014-04-27
      • 2019-03-27
      • 2016-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多