11

I am trying to use this library "react-google-maps" to display google maps in my application. After having followed the tutorial from their github page, I get this error on first run (this is a create-react-app + typescript application) :

enter image description here

I am kind of lost here. As any one used this library before? Here is my code:

import * as React from 'react';
import { GoogleMap, Marker, withGoogleMap, withScriptjs } from 'react-google-maps';
import { compose, withProps } from 'recompose';

const TerrMap: React.StatelessComponent<{
    isMarkerShown: boolean
}> = compose(
    withProps({
        googleMapURL: "https://maps.googleapis.com/maps/api/js?key=__my_api_key__&v=3.exp&libraries=geometry,drawing,places",
        loadingElement: <div style={{ height: `100%` }} />,
        containerElement: <div style={{ height: `400px` }} />,
        mapElement: <div style={{ height: `100%` }} />,
      }),
      withScriptjs,
      withGoogleMap
)( (props: any) => {

    return (
        <GoogleMap
            defaultZoom={8}
            defaultCenter={{ lat: -34.397, lng: 150.644 }}
        >
            {props.isMarkerShown && <Marker position={{ lat: -34.397, lng: 150.644 }} />}
        </GoogleMap>
    );
} ) as any;
2
  • Add a code snippet instead of a screenshot Commented Feb 13, 2018 at 17:04
  • Was this ever answered? Commented May 26, 2018 at 18:23

4 Answers 4

17

If this hasn't been answered yet. Here is how to fix this.

  1. Add these two typing "@types/googlemaps" and "@types/markerclustererplus"
  2. Make sure that you include these types in for project. You can do that different ways, but the easiest would be to add these two types in your tsconfig.json file like this.

Sample:

    {
        "compilerOptions":{
            ...,
            "types":[
                ...
                "googlemaps",
                "markerclustererplus"
            ] 
        }
    }

This should solve your issue.

2024 Update

Type namespace has changed for google.maps "@types/googlemaps" to "@types/google.maps"

So, the types to be included in tsconfig.json should look like this (I didn't even have to include "markerclustererplus"):

    {
        "compilerOptions":{
            ...,
            "types":[
                ...
                "google.maps",
                "markerclustererplus"
            ] 
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

This should be marked as answer. Worked for me, thank you!
2021: i had to add "google.maps" not "googlemaps"
+1 to @xaphod. The @types/googlemaps has been renamed to @types/google.maps (with the dot). Therefore in the types array you also need to type it with the dot.
Yes, this workes with @vis.gl/react-google-maps also
3

You have to add this notation at the top of the file

/*global google*/

I hope it works for you

Comments

3

If you are using typescript you have to install:

npm install --save-dev @types/googlemaps

A better description can be found here: react-google-maps-cannot-find-namespace-google

Comments

0

add to eslint config:

"globals": {
        "google": "readonly",
    }

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.