Ask Your Question
3

How can the Googlemaps react component be used to prevent the occurrence of the error message "included the Google Maps JavaScript API multiple times" when dealing with multiple maps on a single page?

asked 2022-09-20 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-04-06 04:00:00 +0000

nofretete gravatar image

To prevent this error message, you can use the Googlemaps react component in the following way:

  1. Define the Google Maps API key as a constant outside of the component. This ensures that the API key is only loaded once, instead of multiple times for each instance of the component.

  2. In the component's constructor, check if the Google Maps API script has already been loaded on the page. If it has not, load the script by adding it to the head of the document.

  3. During each map instance, instantiate it with the react-google-maps package and reference the Google Maps API key constant defined outside of the component.

This way, the script will only be loaded once and all map instances will share the same instance of the Google Maps JavaScript API, thereby preventing the error message.

Here's an example code snippet to illustrate the approach:

import React, { Component } from 'react';
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from 'react-google-maps';

const GOOGLE_MAPS_API_KEY = 'your_api_key_here';

class Map extends Component {
  constructor(props) {
    super(props);

    this.state = {
      scriptLoaded: false,
    };
  }

  componentDidMount() {
    if (window.google === undefined) {
      const script = document.createElement('script');
      script.src = `https://maps.googleapis.com/maps/api/js?key=${GOOGLE_MAPS_API_KEY}`;
      script.addEventListener('load', () => {
        this.setState({ scriptLoaded: true });
      });
      document.head.appendChild(script);
    } else {
      this.setState({ scriptLoaded: true });
    }
  }

  render() {
    const { scriptLoaded } = this.state;

    if (!scriptLoaded) {
      return <div>Loading...</div>;
    }

    const MyMap = withScriptjs(
      withGoogleMap(() => (
        <GoogleMap defaultCenter={{ lat: 37.7749, lng: -122.4194 }} defaultZoom={12}>
          <Marker position={{ lat: 37.7749, lng: -122.4194 }} />
        </GoogleMap>
      ))
    );

    return (
      <div style={{ height: '500px' }}>
        <MyMap
          googleMapURL={`https://maps.googleapis.com/maps/api/js?key=${GOOGLE_MAPS_API_KEY}`}
          loadingElement={<div style={{ height: `100%` }} />}
          containerElement={<div style={{ height: `100%` }} />}
          mapElement={<div style={{ height: `100%` }} />}
        />
      </div>
    );
  }
}

export default Map;
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-09-20 11:00:00 +0000

Seen: 11 times

Last updated: Apr 06 '23