Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find namespace 'google'. #1209

Open
DarkTrick opened this issue Jun 15, 2022 · 4 comments
Open

Cannot find namespace 'google'. #1209

DarkTrick opened this issue Jun 15, 2022 · 4 comments
Assignees

Comments

@DarkTrick
Copy link

DarkTrick commented Jun 15, 2022

Operating system

macOS 12.04

Browser Version

Firefox 100.0.2

How severe is the bug?

moderate

Bug description

This bug is about the code excerpt from https://developers.google.com/maps/documentation/javascript/react-map#add-map

Using the given code block (see below), will lead to
Cannot find namespace 'google'. and Property 'google' does not exist on type 'Window & typeof globalThis'. The imports linked in this repository does also not solve the problem.

const ref = React.useRef<HTMLDivElement>(null);
const [map, setMap] = React.useState<google.maps.Map>();

React.useEffect(() => {
  if (ref.current && !map) {
    setMap(new window.google.maps.Map(ref.current, {}));
  }
}, [ref, map]);

Steps to reproduce

Follow the steps on https://developers.google.com/maps/documentation/javascript/react-map#typescript

Console log output

Uncaught TypeError: window.google is undefined

Source code as suggested by the guide

import React from 'react';
import { Wrapper, Status } from '@googlemaps/react-wrapper';

export const Map: React.FC<{}> = () => {
	const ref = React.useRef<HTMLDivElement>(null);
	const [map, setMap] = React.useState<google.maps.Map>();

	React.useEffect(() => {
		if (ref.current && !map) {
			setMap(new window.google.maps.Map(ref.current, {}));
		}
	}, [ref, map]);

	return <div ref={ref} />;
};

export const MyMap: React.FC = () => {
	return (
		<>
			<Map />
		</>
	);
};
@DarkTrick

This comment was marked as outdated.

@DarkTrick DarkTrick changed the title Cannot find namespace 'google'. Cannot find namespace 'google'. → Docs change suggestion Jun 15, 2022
@DarkTrick DarkTrick changed the title Cannot find namespace 'google'. → Docs change suggestion Cannot find namespace 'google'. Jun 15, 2022
@DarkTrick
Copy link
Author

I found a solution. The docs are rather confusing in various points.
The code below is a working example, in case anyone is struggling similarly.

Would be nice, if the docs could feature a more simpler and working example like the one below :)

/**
 * Packages to install:
 * 	 yarn add @googlemaps/react-wrapper
 **/

import React from 'react';
import { Wrapper, Status } from '@googlemaps/react-wrapper';

/**
 * Internal component you use as a pure wrapper for google maps
 **/
const GoogleMap: React.FC = () => {
	const myRef = React.useRef<HTMLDivElement>(null);
	const [map, setMap] = React.useState<any>();

	// Why `as any`? `window.google` will be set dynamically within `<Wrapper>`.
	// However, typescript does not know about that. So we make it shut up.
	const google = (window as any).google;

	const centerPoint = { lat: 52.5167, lng: 13.3833 };

	React.useEffect(() => {
		if (myRef.current && !map) {
			setMap(
				new google.maps.Map(myRef.current, {
					center: centerPoint,
					zoom: 13, // IMPORTANT: Without `zoom` your map stays blank
				})
			);
		}
	}, [myRef, map]);

	// IMPORTANT: You cannot move the Wrapper inside this component (for an unknown reason). 
	// Doing so will lead to a blank screen.
	return <div ref={myRef} style={{ width: '100%', height: '500px' }} />;
};

/**
 * Component you will use in the rest of your code
 **/
export const MyMap: React.FC = () => {
	// IMPORTANT: Wrapper must be here, cannot be above
	return (
		<Wrapper apiKey={'[Your API key]'}>
			<GoogleMap />
		</Wrapper>
	);
};

@sijn82
Copy link

sijn82 commented Jul 12, 2022

Thanks for this, now I have a working example I can start playing around.

@wangela wangela assigned amuramoto and unassigned wangela Jul 12, 2022
@wangela
Copy link
Member

wangela commented Jul 12, 2022

@DarkTrick Thank you for sharing your solution! We'll look at improving the code sample.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants