【问题标题】:Google map for meteor流星的谷歌地图
【发布时间】:2017-05-06 12:03:31
【问题描述】:

我在 Meteor 中安装了“meteor add dburles:google-maps”。我将此代码添加到 React 组件中,

...
import { GoogleMaps } from 'meteor/dburles:google-maps';
...
export default class Location extends TrackerReact(React.Component){
constructor(props) {
    super(props);
    this.state = {
        ...
    };
    GoogleMaps.load();
}
componentDidUpdate(){
    GoogleMaps.create({
      name: 'exampleMap',
      element: document.getElementById('basic_map'),
      options: {
        center: new google.maps.LatLng(-37.8136, 144.9631),
        zoom: 8
      }
    });
}
render() {
    ...
    return (
        ...
                    <div id="basic_map" style={{"width":"300px","height":"300px"}}></div>
        ...
    )
}

这里的问题是“google not defined”错误。我想是来自这行代码,

google.maps.LatLng(-37.8136, 144.9631),

我在安装过程中遗漏了什么吗?我怎样才能解决这个问题? 我正在使用 Meteor、ReactJs、Flow 路由器和 TrackerReact

【问题讨论】:

    标签: javascript google-maps reactjs meteor flow-router


    【解决方案1】:

    那是因为 google 尚未加载。请务必在点击创建之前检查GoogleMaps.loaded()

    作者 David Burles 写了一个很好的 example using the library with react,应该会有所帮助。它使用 createContainer 而不是 TrackerReact 但应该让你走上正轨。

    【讨论】:

    • 我将把 GoogleMaps.loaded() 放在哪里?
    • @JohnArellano 好吧,如果我查看 TrackerReact 文档,它会说:“使用任何反应性数据源(例如:collection.find() 或 Session.get('foo'))在您的渲染方法中或由您的渲染方法调用的方法会自动响应!”所以我会把它插入到 render 方法中的 if 语句中,只有当它为真时才执行create
    • 仍然出现“未定义谷歌”错误。我在渲染内部和返回语句之前使用了 if(GoogleMaps.loaded())。
    • 谢谢。我已经解决了这个问题,但是出现了新错误,Google Maps API 错误:MissingKeyMapError
    • 我认为您必须提供 GoogleMaps.load() 的密钥,如 herehere 所示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 2015-07-15
    • 2020-08-19
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多