question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HeatMap - 'google' is not defined and Module not found errors

See original GitHub issue

Please provide an explanation

HeatMap implementation results in two errors described below. I have tried using the Gatsby example but have the same issue. I am just trying to display on a simple one page app using CRA.

Your Environment

os: mac/linux/windows/

node --version v16.13.0

react version 18.0.0

@react-google-maps/api version 2.8.1

How is it behave?

I cannot get the heatmap later to display - getting two errors:

  1. 'google' is not defined
  2. Module not found: Error: You attempted to import ../../docs/ScriptLoaded which falls outside of the project src/ directory.

How should it behave correctly?

Heatmap should render

basic implementation of incorrect behavior in codesandbox.com

https://codesandbox.io/s/kind-wescoff-ud7jb8?file=/src/App.js

Screenshot 2022-04-11 at 10 58 07
import { GoogleMap, HeatmapLayer } from '@react-google-maps/api';
const ScriptLoaded = require('../../docs/ScriptLoaded').default;

const mapContainerStyle = {
  height: '400px',
  width: '800px',
};

const center = {
  lat: 37.774546,
  lng: -122.433523,
};

function App() {
  const onLoad = (heatmapLayer) => {
    console.log('HeatmapLayer onLoad heatmapLayer: ', heatmapLayer);
  };

  const onUnmount = (heatmapLayer) => {
    console.log('HeatmapLayer onUnmount heatmapLayer: ', heatmapLayer);
  };

  return (
    <div className="App">
      <div className="map-container">
        <ScriptLoaded>
          {() => (
            <GoogleMap
              // optional
              id="heatmap-layer-example"
              // required to set height and width either through mapContainerClassName, either through mapContainerStyle prop
              mapContainerStyle={mapContainerStyle}
              // required
              zoom={13}
              // required
              center={center}>
              <HeatmapLayer
                // optional
                onLoad={onLoad}
                // optional
                onUnmount={onUnmount}
                // required
                data={[
                  new google.maps.LatLng(37.782, -122.447),
                  new google.maps.LatLng(37.782, -122.445),
                  new google.maps.LatLng(37.782, -122.443),
                  new google.maps.LatLng(37.782, -122.441),
                  new google.maps.LatLng(37.782, -122.439),
                  new google.maps.LatLng(37.782, -122.437),
                  new google.maps.LatLng(37.782, -122.435),
                  new google.maps.LatLng(37.785, -122.447),
                  new google.maps.LatLng(37.785, -122.445),
                  new google.maps.LatLng(37.785, -122.443),
                  new google.maps.LatLng(37.785, -122.441),
                  new google.maps.LatLng(37.785, -122.439),
                  new google.maps.LatLng(37.785, -122.437),
                  new google.maps.LatLng(37.785, -122.435),
                ]}
              />
            </GoogleMap>
          )}
        </ScriptLoaded>
      </div>
    </div>
  );
}

export default App;

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
EthanEFungcommented, Apr 20, 2022
import * as React from "react";
import {
  GoogleMap,
  HeatmapLayer,
  useJsApiLoader
} from "@react-google-maps/api";

const mapContainerStyle = {
  height: "400px",
  width: "800px"
};

const center = {
  lat: 37.774546,
  lng: -122.433523
};

const libraries = ["visualization"];

export default function App() {
  const { isLoaded } = useJsApiLoader({
    googleMapsApiKey: "YOUR_API_KEY",
    libraries
    // ...otherOptions
  });
  const data = React.useMemo(() => {
    if (isLoaded === false) {
      return [];
    }
    return [
      new window.google.maps.LatLng(37.782, -122.447),
      new window.google.maps.LatLng(37.782, -122.445),
      new window.google.maps.LatLng(37.782, -122.443),
      new window.google.maps.LatLng(37.782, -122.441),
      new window.google.maps.LatLng(37.782, -122.439),
      new window.google.maps.LatLng(37.782, -122.437),
      new window.google.maps.LatLng(37.782, -122.435),
      new window.google.maps.LatLng(37.785, -122.447),
      new window.google.maps.LatLng(37.785, -122.445),
      new window.google.maps.LatLng(37.785, -122.443),
      new window.google.maps.LatLng(37.785, -122.441),
      new window.google.maps.LatLng(37.785, -122.439),
      new window.google.maps.LatLng(37.785, -122.437),
      new window.google.maps.LatLng(37.785, -122.435)
    ];
  }, [isLoaded]);

  if (isLoaded === false) {
    return null;
  }

  return (
    <div className="map">
      <div className="map-container">
        <GoogleMap
          id="heatmap-example"
          mapContainerStyle={mapContainerStyle}
          zoom={13}
          center={center}
        >
          <HeatmapLayer data={data} />
        </GoogleMap>
      </div>
    </div>
  );
}

1reaction
SKempincommented, May 19, 2022

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Google Maps API throws "Uncaught ReferenceError
Uncaught ReferenceError : google is not defined error will be gone when removed the async defer from the map API script tag.
Read more >
Google / HeatmapOverlay is not defined · Issue #218 · pa7 ...
Hi Guys,. I'm getting the above two errors, the first being within the 'gmaps-heatmap.js' file, and the second being when i create a...
Read more >
Upgrading Your Maps JavaScript API Application from V2 to V3
The Maps JavaScript API v2 is no longer available as of May 26, 2021. As a result, your site's v2 maps will stop...
Read more >
How to Fix: No module named plotly - Statology
This error occurs when Python does not detect the Plotly library in your current environment. This tutorial shares the exact steps you can...
Read more >
NameError: name 'plt' is not defined in Python | bobbyhadz
Make sure you haven't misspelled the module you are importing because module names are case-sensitive. Also, make sure you haven't placed your import...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found