Google Maps Library

| 2025-07-14

Google released a React library for Google Maps Platform on May 21, 2024.

By using this react-google-maps, it becomes easy to use Google Maps on your website. This time, we will check how to use this library with Next.js.

Get Google Maps API key

To use Google Maps, you need to obtain an API key. Basically, you need to subscribe to a paid plan to use the Google Maps API key, but you can use it for free up to $200 per month. This amount is sufficient for verification and testing.

As a prerequisite for obtaining an API key, you need to already have a Google Cloud developer account. After ensuring you can access the developer site, proceed with the following steps.

Access the following site with administrator privileges.

Image

Create a new project.

Image

Switch to the created project, and the API key will be displayed as shown below. We will use this key this time.

Image

Next, a dialog will be displayed to confirm the purpose of using this API key. Select Website this time, and for the Referrer, target your own domain.

Image

The screen will change, but click Skip now in the upper right corner to skip the remaining steps. The screen will change as follows.

Image

You can obtain the key again by clicking the Keys & Credentials menu on the left side of this screen.

Image

Click Edit API Key on the far right menu of the relevant key to display the API key settings. Since it needs to work locally this time, add http://localhost:3000/\* to Website restrictions.

Image

Now the preparation is complete.

App Router

We will enable the use of Google maps for the Next.js App Router project.

Create sample site

sh
Image
sh
text
src/components/Maps.tsx
src/app/page.tsx
Image

Sample code can be found below.

Pages Router

Let’s enable the use of Google Maps in a Next.js Pages Router project.

Running the Sample

sh
Image
sh
.env
src/pages/index.tsx
sh

As a result, the maps was displayed as follows.

Image

Error Handling

When running the above code, the following error is actually displayed.

Image

This error occurs because useLayoutEffect is being used in a server-side rendering (SSR) environment. useLayoutEffect is a hook that only works on the client side and has no effect on the server side.

Therefore, add the following code.

src/pages/index.tsx

Now, the error is no longer displayed.

You can find the sample code below.

Summary

This time, I created a sample to use the new React Google Maps library with Next.js. It is a form of pure Next.js operation check, but I think it is convenient in terms of preparing an environment that works.

References

Update