Convert Figma logo to code with AI

JustFly1984 logoreact-google-maps-api

React Google Maps API

1,796
438
1,796
191

Top Related Projects

Node.js client library for Google Maps API Web Services

React.js Google Maps integration component

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

React friendly API wrapper around MapboxGL JS

Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL

Quick Overview

React Google Maps API is a comprehensive set of React components for integrating Google Maps into React applications. It provides a declarative approach to using Google Maps, making it easier for developers to implement map functionality while leveraging React's component-based architecture.

Pros

  • Easy integration with React applications
  • Extensive documentation and examples
  • Supports TypeScript for improved type safety
  • Regular updates and active community support

Cons

  • Requires a Google Maps API key, which may incur costs for high-usage applications
  • Learning curve for developers unfamiliar with Google Maps API
  • Some advanced features may require additional configuration

Code Examples

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

function MyMap() {
  const { isLoaded } = useJsApiLoader({
    id: 'google-map-script',
    googleMapsApiKey: "YOUR_API_KEY"
  });

  return isLoaded ? (
    <GoogleMap
      mapContainerStyle={{ width: '400px', height: '400px' }}
      center={{ lat: -3.745, lng: -38.523 }}
      zoom={10}
    />
  ) : <></>
}
  1. Adding a Marker:
import { GoogleMap, Marker } from '@react-google-maps-api';

function MapWithMarker() {
  return (
    <GoogleMap
      mapContainerStyle={{ width: '400px', height: '400px' }}
      center={{ lat: -3.745, lng: -38.523 }}
      zoom={10}
    >
      <Marker position={{ lat: -3.745, lng: -38.523 }} />
    </GoogleMap>
  );
}
  1. Using InfoWindow:
import { GoogleMap, Marker, InfoWindow } from '@react-google-maps-api';

function MapWithInfoWindow() {
  const [isOpen, setIsOpen] = useState(false);

  return (
    <GoogleMap
      mapContainerStyle={{ width: '400px', height: '400px' }}
      center={{ lat: -3.745, lng: -38.523 }}
      zoom={10}
    >
      <Marker position={{ lat: -3.745, lng: -38.523 }} onClick={() => setIsOpen(true)} />
      {isOpen && (
        <InfoWindow position={{ lat: -3.745, lng: -38.523 }} onCloseClick={() => setIsOpen(false)}>
          <div>Hello, World!</div>
        </InfoWindow>
      )}
    </GoogleMap>
  );
}

Getting Started

  1. Install the package:

    npm install @react-google-maps/api
    
  2. Import and use the components in your React application:

    import { GoogleMap, useJsApiLoader } from '@react-google-maps-api';
    
    function App() {
      const { isLoaded } = useJsApiLoader({
        id: 'google-map-script',
        googleMapsApiKey: "YOUR_API_KEY"
      });
    
      return isLoaded ? (
        <GoogleMap
          mapContainerStyle={{ width: '100%', height: '400px' }}
          center={{ lat: -3.745, lng: -38.523 }}
          zoom={10}
        />
      ) : <></>
    }
    
    export default App;
    
  3. Replace "YOUR_API_KEY" with your actual Google Maps API key.

Competitor Comparisons

Node.js client library for Google Maps API Web Services

Pros of google-maps-services-js

  • Official Google Maps library, ensuring better long-term support and compatibility
  • Covers a wider range of Google Maps APIs, including Places, Directions, and Geocoding
  • Designed for server-side use, making it suitable for Node.js applications

Cons of google-maps-services-js

  • Not specifically designed for React applications, requiring additional setup for frontend use
  • Lacks built-in React components, which may lead to more boilerplate code
  • May require more manual handling of map rendering and interactions

Code Comparison

react-google-maps-api:

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

function MyMap() {
  return (
    <GoogleMap center={center} zoom={10}>
      <Marker position={markerPosition} />
    </GoogleMap>
  );
}

google-maps-services-js:

const client = new Client({});
client
  .elevation({
    params: {
      locations: [{ lat: 45, lng: -110 }],
      key: 'YOUR_API_KEY',
    },
    timeout: 1000,
  })
  .then((r) => {
    console.log(r.data.results[0].elevation);
  });

The code examples highlight the different focus of each library. react-google-maps-api provides React components for easy map integration, while google-maps-services-js offers a more general-purpose API client for various Google Maps services.

React.js Google Maps integration component

Pros of react-google-maps

  • More established project with a longer history and larger community
  • Extensive documentation and examples available
  • Supports older React versions (15+)

Cons of react-google-maps

  • Less frequent updates and maintenance
  • May have performance issues with large datasets or complex maps
  • Some users report difficulties with TypeScript integration

Code Comparison

react-google-maps:

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

const MyMapComponent = withScriptjs(withGoogleMap((props) =>
  <GoogleMap defaultZoom={8} defaultCenter={{ lat: -34.397, lng: 150.644 }}>
    {props.isMarkerShown && <Marker position={{ lat: -34.397, lng: 150.644 }} />}
  </GoogleMap>
))

react-google-maps-api:

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

const MyMapComponent = () => (
  <GoogleMap zoom={8} center={{ lat: -34.397, lng: 150.644 }}>
    <Marker position={{ lat: -34.397, lng: 150.644 }} />
  </GoogleMap>
)

The react-google-maps-api library offers a more modern and streamlined API, with better TypeScript support and improved performance. It also provides more frequent updates and active maintenance. However, react-google-maps may be preferred for projects using older React versions or those requiring extensive documentation and community resources.

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 for beginners to get started
  • Lightweight package with minimal dependencies
  • Better documentation and examples for basic use cases

Cons of google-maps-react

  • Less actively maintained, with fewer recent updates
  • Limited support for advanced Google Maps features and customizations
  • Smaller community and fewer third-party resources available

Code Comparison

react-google-maps-api:

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

<GoogleMap
  center={center}
  zoom={10}
  onLoad={onMapLoad}
  options={mapOptions}
>
  <Marker position={markerPosition} />
</GoogleMap>

google-maps-react:

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

<Map
  google={this.props.google}
  zoom={10}
  initialCenter={center}
>
  <Marker position={markerPosition} />
</Map>

The code comparison shows that react-google-maps-api uses a more modular approach with separate imports for GoogleMap and Marker components. It also provides more granular control over map options and events. In contrast, google-maps-react uses a higher-level abstraction with the Map component and requires wrapping the entire component with GoogleApiWrapper.

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
  • Seamless integration with other Uber Vis.gl libraries for advanced data visualization
  • Strong TypeScript support and comprehensive documentation

Cons of react-map-gl

  • Requires a Mapbox account and API key, which may incur costs for high-usage applications
  • Limited built-in UI controls compared to Google Maps API

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-api:

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

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

Both libraries offer React components for integrating maps, but react-map-gl focuses on Mapbox GL JS, while react-google-maps-api wraps the Google Maps JavaScript API. The choice between them depends on specific project requirements, desired features, and budget considerations.

Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL

Pros of mapbox-gl-js

  • More customizable and flexible for advanced mapping needs
  • Better performance for large datasets and complex visualizations
  • Supports 3D terrain and custom layer types

Cons of mapbox-gl-js

  • Steeper learning curve compared to react-google-maps-api
  • Requires more setup and configuration for basic use cases
  • May have higher costs for large-scale applications

Code Comparison

react-google-maps-api:

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

function MyMap() {
  return (
    <GoogleMap center={{ lat: 40.7128, lng: -74.0060 }} zoom={10}>
      <Marker position={{ lat: 40.7128, lng: -74.0060 }} />
    </GoogleMap>
  );
}

mapbox-gl-js:

import mapboxgl from 'mapbox-gl';

mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
const map = new mapboxgl.Map({
  container: 'map',
  style: 'mapbox://styles/mapbox/streets-v11',
  center: [-74.0060, 40.7128],
  zoom: 10
});

The code examples show that react-google-maps-api offers a more React-friendly approach with components, while mapbox-gl-js provides a lower-level API for greater control and customization. mapbox-gl-js requires more setup but allows for more advanced features and styling options.

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

logo

@react-google-maps organization root

You can donate or became a sponsor https://opencollective.com/react-google-maps-api#category-CONTRIBUTE

npm package npm downloads npm bundle size Join the community on Spectrum

README

For API README please navigate to https://github.com/JustFly1984/react-google-maps-api/tree/master/packages/react-google-maps-api

or https://react-google-maps-api-docs.netlify.app

For Maintainers

Join our Slack channel

For Developers and Contributors

Requirements

  • basic git, JavaScript, React knowledge
  • Google Maps API Key from Google Cloud Console
  • git
  • node
  • yarn

To develop locally

Fork original repo at https://github.com/JustFly1984/react-google-maps-api. Clone your fork to local directory of your choice, install dependencies, set up your API Key, and start storybook server. Following commands should do the job:

  • git clone https://github.com/YOUR_USER_NAME/react-google-maps-api.git - clone your fork `
  • cd react-google-maps-api - move to newly created folder
  • cp .storybook/example.maps.config.ts .storybook/maps.config.ts - create file with API Key
  • yarn install - install dependencies
  • yarn bootstrap - setup workspace
  • yarn storybook - run storybook server

Any changes you make to src folders of contained packages should reflect on the storybook server.

To contribute

Create a feature/fix branch on your own fork and make pull request towards develop branch of the original repo.

You can donate or became a sponsor https://opencollective.com/react-google-maps-api#category-CONTRIBUTE

NPM DownloadsLast 30 Days