Convert Figma logo to code with AI

tomchentw logoreact-google-maps

React.js Google Maps integration component

4,624
938
4,624
272

Top Related Projects

React friendly API wrapper around MapboxGL JS

Companion code to the "How to Write a Google Maps React Component" Tutorial

React Google Maps API

Google map library for react that allows rendering components as markers :tada:

ReactJS Maps without external dependencies

Quick Overview

React-google-maps is a popular React library that provides a set of React components wrapping the Google Maps JavaScript API. It allows developers to easily integrate Google Maps functionality into their React applications, offering a more declarative and React-friendly approach to working with maps.

Pros

  • Seamless integration with React applications
  • Declarative API that aligns well with React's component-based architecture
  • Extensive documentation and community support
  • Supports both controlled and uncontrolled components

Cons

  • Requires a Google Maps API key, which may incur costs for high-usage applications
  • Learning curve for developers unfamiliar with the Google Maps API
  • Some advanced features may require direct interaction with the underlying Google Maps objects
  • Performance can be impacted when rendering large numbers of markers or complex map overlays

Code Examples

  1. Basic Map Component:
import { GoogleMap, LoadScript } from '@react-google-maps/api';

const MapComponent = () => {
  const center = { lat: 40.7128, lng: -74.0060 };
  return (
    <LoadScript googleMapsApiKey="YOUR_API_KEY">
      <GoogleMap
        mapContainerStyle={{ width: '400px', height: '400px' }}
        center={center}
        zoom={10}
      />
    </LoadScript>
  );
};
  1. Adding a Marker:
import { GoogleMap, LoadScript, Marker } from '@react-google-maps/api';

const MapWithMarker = () => {
  const center = { lat: 40.7128, lng: -74.0060 };
  return (
    <LoadScript googleMapsApiKey="YOUR_API_KEY">
      <GoogleMap
        mapContainerStyle={{ width: '400px', height: '400px' }}
        center={center}
        zoom={10}
      >
        <Marker position={center} />
      </GoogleMap>
    </LoadScript>
  );
};
  1. Using InfoWindow:
import { GoogleMap, LoadScript, Marker, InfoWindow } from '@react-google-maps/api';
import { useState } from 'react';

const MapWithInfoWindow = () => {
  const [isOpen, setIsOpen] = useState(false);
  const center = { lat: 40.7128, lng: -74.0060 };

  return (
    <LoadScript googleMapsApiKey="YOUR_API_KEY">
      <GoogleMap
        mapContainerStyle={{ width: '400px', height: '400px' }}
        center={center}
        zoom={10}
      >
        <Marker position={center} onClick={() => setIsOpen(true)} />
        {isOpen && (
          <InfoWindow position={center} onCloseClick={() => setIsOpen(false)}>
            <div>Hello, New York!</div>
          </InfoWindow>
        )}
      </GoogleMap>
    </LoadScript>
  );
};

Getting Started

  1. Install the package:

    npm install @react-google-maps/api
    
  2. Obtain a Google Maps API key from the Google Cloud Console.

  3. Use the LoadScript component to load the Google Maps API and wrap your map components:

    import { LoadScript, GoogleMap } from '@react-google-maps/api';
    
    function App() {
      return (
        <LoadScript googleMapsApiKey="YOUR_API_KEY">
          <GoogleMap
            mapContainerStyle={{ width: '100%', height: '400px' }}
            center={{ lat: 40.7128, lng: -74.0060 }}
            zoom={10}
          />
        </LoadScript>
      );
    }
    
  4. Customize your map by adding markers, info windows, and other Google Maps features as needed.

Competitor Comparisons

React friendly API wrapper around MapboxGL JS

Pros of react-map-gl

  • Built on Mapbox GL JS, offering high-performance vector maps and extensive customization options
  • Supports WebGL rendering for smooth and efficient map interactions
  • Provides a wide range of built-in components for common map features (markers, popups, etc.)

Cons of react-map-gl

  • Requires a Mapbox access token, which may incur costs for high-usage applications
  • Learning curve can be steeper due to the more complex API and additional features

Code Comparison

react-map-gl:

import Map from 'react-map-gl';

<Map
  mapboxAccessToken={MAPBOX_TOKEN}
  initialViewState={{
    longitude: -122.4,
    latitude: 37.8,
    zoom: 14
  }}
  style={{width: 600, height: 400}}
  mapStyle="mapbox://styles/mapbox/streets-v9"
/>

react-google-maps:

import { GoogleMap, LoadScript } from '@react-google-maps/api';

<LoadScript googleMapsApiKey={GOOGLE_MAPS_API_KEY}>
  <GoogleMap
    center={{ lat: 37.8, lng: -122.4 }}
    zoom={14}
    mapContainerStyle={{ width: '600px', height: '400px' }}
  />
</LoadScript>

Companion code to the "How to Write a Google Maps React Component" Tutorial

Pros of google-maps-react

  • Simpler API with fewer components, making it easier to get started
  • Better documentation and examples for basic use cases
  • Smaller package size, potentially leading to faster load times

Cons of google-maps-react

  • Less flexibility and fewer advanced features compared to react-google-maps
  • Not as actively maintained, with fewer recent updates
  • Limited support for custom map styles and advanced Google Maps API features

Code Comparison

react-google-maps:

import { GoogleMap, Marker } from "react-google-maps"

<GoogleMap
  defaultZoom={8}
  defaultCenter={{ lat: -34.397, lng: 150.644 }}
>
  <Marker position={{ lat: -34.397, lng: 150.644 }} />
</GoogleMap>

google-maps-react:

import { Map, Marker, GoogleApiWrapper } from 'google-maps-react';

<Map
  google={this.props.google}
  zoom={8}
  initialCenter={{ lat: -34.397, lng: 150.644 }}
>
  <Marker position={{ lat: -34.397, lng: 150.644 }} />
</Map>

Both libraries provide similar basic functionality, but react-google-maps offers more advanced features and greater customization options. google-maps-react is simpler to use for basic map implementations but may be limiting for complex projects.

React Google Maps API

Pros of react-google-maps-api

  • More actively maintained with frequent updates
  • Better TypeScript support and type definitions
  • Improved performance through optimized rendering

Cons of react-google-maps-api

  • Steeper learning curve due to more complex API
  • Some users report issues with documentation clarity
  • Potentially more challenging migration from older versions

Code Comparison

react-google-maps:

import { GoogleMap, Marker } from "react-google-maps"

<GoogleMap
  defaultZoom={8}
  defaultCenter={{ lat: -34.397, lng: 150.644 }}
>
  <Marker position={{ lat: -34.397, lng: 150.644 }} />
</GoogleMap>

react-google-maps-api:

import { GoogleMap, Marker } from "@react-google-maps/api"

<GoogleMap
  mapContainerStyle={containerStyle}
  center={center}
  zoom={10}
>
  <Marker position={position} />
</GoogleMap>

The main differences in the code examples are:

  1. Import statements differ slightly
  2. react-google-maps-api uses mapContainerStyle prop instead of inline styles
  3. react-google-maps-api separates center and zoom props, while react-google-maps uses defaultZoom and defaultCenter

Both libraries provide similar core functionality, but react-google-maps-api offers more modern features and better maintenance. However, it may require more effort to learn and implement, especially for developers familiar with the older react-google-maps library.

Google map library for react that allows rendering components as markers :tada:

Pros of google-map-react

  • Simpler API and easier to use for basic map implementations
  • Better performance for rendering large numbers of markers
  • More flexible for custom overlays and components

Cons of google-map-react

  • Less comprehensive documentation compared to react-google-maps
  • Fewer built-in components for advanced Google Maps features
  • May require more custom code for complex map interactions

Code Comparison

react-google-maps:

import { GoogleMap, Marker } from "@react-google-maps/api";

<GoogleMap
  center={center}
  zoom={10}
>
  <Marker position={markerPosition} />
</GoogleMap>

google-map-react:

import GoogleMapReact from 'google-map-react';

<GoogleMapReact
  center={center}
  zoom={10}
>
  <Marker lat={markerPosition.lat} lng={markerPosition.lng} />
</GoogleMapReact>

Both libraries provide React components for integrating Google Maps, but google-map-react offers a more straightforward approach for basic map implementations. react-google-maps provides a wider range of built-in components for advanced features, while google-map-react excels in performance and flexibility for custom overlays. The choice between the two depends on the specific requirements of your project and the level of customization needed.

ReactJS Maps without external dependencies

Pros of Pigeon Maps

  • Lightweight and fast, with no external dependencies
  • Fully customizable and open-source
  • Supports multiple map providers (OpenStreetMap, Mapbox, etc.)

Cons of Pigeon Maps

  • Less feature-rich compared to React Google Maps
  • Smaller community and fewer resources available
  • May require more manual implementation for advanced features

Code Comparison

React Google Maps:

import { GoogleMap, Marker } from "react-google-maps"

const MyMap = () => (
  <GoogleMap defaultZoom={8} defaultCenter={{ lat: -34.397, lng: 150.644 }}>
    <Marker position={{ lat: -34.397, lng: 150.644 }} />
  </GoogleMap>
)

Pigeon Maps:

import { Map, Marker } from "pigeon-maps"

const MyMap = () => (
  <Map height={300} center={[-34.397, 150.644]} zoom={8}>
    <Marker anchor={[-34.397, 150.644]} />
  </Map>
)

Both libraries offer React components for creating maps, but Pigeon Maps uses a simpler API and doesn't require a Google Maps API key. React Google Maps provides more built-in features and integrations with Google's services, while Pigeon Maps offers more flexibility in terms of map providers and customization.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

react-google-maps

React.js Google Maps integration component

Version Travis CI Quality Coverage Dependencies Gitter

Introduction

Installation

Usage & Configuration

Changelog

The changelog is automatically generated via standard-version and can be found in project root as well as npm tarball.

Demo App

Getting Help

Before doing this, did you:

  1. Read the documentation
  2. Read the source code

You can get someone's help in three ways:

  1. Ask on StackOverflow with a google-maps tag or use react-google-maps as a keyword
  2. Ask in the chat room
  3. Create a Pull Request with your solutions to your problem

Please, be noted, no one, I mean, no one, is obligated to help you in ANY means. Your time is valuable, so does our contributors. Don't waste our time posting questions like “how do I do X with React-Google-Maps” and “my code doesn't work”. This is not the primary purpose of the issue tracker. Don't abuse.

For contributors

Some simple guidelines
  • Don't manually modify lib folder. They're generated during yarn release process
  • Follow conventional-commits-specification
  • standard-version
  • Auto generated: src/macros -> src/components -> lib/components
  • Other components are manually maintained
  • Use yarn and keep yarn.lock updated in PR
  • Discuss! Discuss! Discuss!

NPM DownloadsLast 30 Days