React-google-maps: Cannot instantiate google object ( google is undefined )

Created on 30 Apr 2017  路  7Comments  路  Source: tomchentw/react-google-maps

I am using _MeteorJS ( 1.4.4 ) + ReactJS_, currently i can get the map, markers, working properly. I am using ' _withScriptjs.js ' async_ method to render the map, because i tried without the async and it does not work. Here is my problem, i want to use the _DirectionsRenderer_ to draw direction, which is must needed for my traffic jam project, and following the example, I cannot get it works because of _google is undefined_ when the code runs until
const DirectionsService = new google.maps.DirectionsService(); throws the error inside componentDidMount().
Because I am still on testing the package, so there is no complex code, and my code was just exactly the same as the example code ( directions ). Thanks you very much for helping.
PS: Awesome package with almost google api completed.

Most helpful comment

Solved it by adding:

<script src="https://maps.googleapis.com/maps/api/js"></script>

to the head of my index.html, as suggested in the demo project's source code.

@tomchentw This should be documented, willing to send in a pull request.

All 7 comments

@xshadowlegendx I have faced this same issue before. So please refer to my question on same topic (ex) Stackoverflow react-google-map

Connect the api tag before the bundle.js

Solved it by adding:

<script src="https://maps.googleapis.com/maps/api/js"></script>

to the head of my index.html, as suggested in the demo project's source code.

@tomchentw This should be documented, willing to send in a pull request.

This helped resolve my 'google is undefined' issue.
https://stackoverflow.com/questions/43714895/google-is-not-defined-in-react-app-using-create-react-app

My build is React.js with Next.js.

@sebasgarcep I'll put a PR together and add it to the README, because this comes up way too often. Since you haven't sent a PR yet, I'll just open it up now.

A possible solution is using withScriptjs
https://tomchentw.github.io/react-google-maps/#directionsrenderer

You can try this after imports :

const google = window.google

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LukasZvikas picture LukasZvikas  路  3Comments

tahir-masood1 picture tahir-masood1  路  4Comments

SyedSaifAli picture SyedSaifAli  路  3Comments

johnantoni picture johnantoni  路  3Comments

ShintaroNippon picture ShintaroNippon  路  3Comments