6

I am working with the react-google-maps package https://github.com/tomchentw/react-google-maps and https://www.npmjs.com/package/react-google-maps. I have an error that says:

./src/App.js Line 31:  'google' is not defined  no-undef  Line 32:  'google' is not defined  no-undef  Line 37:  'google' is not defined  no-undef Line 42:  'google' is not defined  no-undef Line 44:  'google' is not defined  no-undef

and heres my line in the error:

 state = {
    origin: new google.maps.LatLng(41.8507300, -87.6512600),
    destination: new google.maps.LatLng(41.8525800, -87.6514100),
    directions: null,
}

componentDidMount() {
    const DirectionsService = new google.maps.DirectionsService();

    DirectionsService.route({
        origin: this.state.origin,
        destination: this.state.destination,
        travelMode: google.maps.TravelMode.DRIVING,
    }, (result, status) => {
        if (status === google.maps.DirectionsStatus.OK) {
            this.setState({
                directions: result,
            });
        } else {
            console.error(`error fetching directions ${result}`);
        }
    });
}

all the 'google' thing is error.

3 Answers 3

7

the answer is I didnt include this line /* global google */ to the top of my file.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you give some detailed answer please?
This error caused by Eslint, you can try and add /* global google */ to the top of your file to disable ESlint.
3

If google is not defined, than you didn't load Google Maps API correctly. As explained in react-google-maps's README, put this in your HTML before your React component code loads:

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

1 Comment

If you add script tag in HTML then in render function when you pass props to a component it expects a prop googleMapURL and passing URL with API key there will generate this warning: You have included the Google Maps API multiple times on this page. This may cause unexpected errors.
1

you can also just include window reference to resolve the error, at least with default create-react-app config it does

new window.google.maps.LatLng(41.8507300, -87.6512600)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.