Search code examples
javascriptgoogle-mapsreactjsmeteorflow-router

Google map for meteor


I install "meteor add dburles:google-maps" in Meteor. I added this code to the React Component,

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

The problem here is the error "google not defined". I think it is from this line of code,

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

Did I miss something in installation? How can I fix this? I am using Meteor, ReactJs, Flow router and TrackerReact


Solution

  • That's because google hasn't been loaded yet. Make sure to check GoogleMaps.loaded() before you hit create.

    The author, David Burles, wrote a nice example using the library with react, which should be helpful. It uses createContainer instead of TrackerReact but should get you on the right track.