Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

google-map-react

Package Overview
Dependencies
Maintainers
2
Versions
131
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

google-map-react

Isomorphic component that allows rendering react components on a google map

  • 2.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
291K
increased by5.62%
Maintainers
2
Weekly downloads
 
Created

What is google-map-react?

google-map-react is a library that allows developers to embed Google Maps into their React applications. It provides a simple and flexible API to work with Google Maps, enabling features like custom markers, overlays, and event handling.

What are google-map-react's main functionalities?

Basic Map Rendering

This code sample demonstrates how to render a basic Google Map using google-map-react. You need to replace 'YOUR_API_KEY' with your actual Google Maps API key.

```jsx
import React from 'react';
import GoogleMapReact from 'google-map-react';

const SimpleMap = () => {
  const defaultProps = {
    center: {
      lat: 59.95,
      lng: 30.33
    },
    zoom: 11
  };

  return (
    <div style={{ height: '100vh', width: '100%' }}>
      <GoogleMapReact
        bootstrapURLKeys={{ key: 'YOUR_API_KEY' }}
        defaultCenter={defaultProps.center}
        defaultZoom={defaultProps.zoom}
      >
      </GoogleMapReact>
    </div>
  );
};

export default SimpleMap;
```

Custom Markers

This code sample shows how to add custom markers to the map. The `AnyReactComponent` is used to render a custom marker at a specified latitude and longitude.

```jsx
import React from 'react';
import GoogleMapReact from 'google-map-react';

const AnyReactComponent = ({ text }) => <div>{text}</div>;

const CustomMarkerMap = () => {
  const defaultProps = {
    center: {
      lat: 59.95,
      lng: 30.33
    },
    zoom: 11
  };

  return (
    <div style={{ height: '100vh', width: '100%' }}>
      <GoogleMapReact
        bootstrapURLKeys={{ key: 'YOUR_API_KEY' }}
        defaultCenter={defaultProps.center}
        defaultZoom={defaultProps.zoom}
      >
        <AnyReactComponent
          lat={59.955413}
          lng={30.337844}
          text="My Marker"
        />
      </GoogleMapReact>
    </div>
  );
};

export default CustomMarkerMap;
```

Handling Map Events

This code sample demonstrates how to handle map events such as clicks. The `handleApiLoaded` function is used to add an event listener to the map.

```jsx
import React from 'react';
import GoogleMapReact from 'google-map-react';

const EventHandlingMap = () => {
  const defaultProps = {
    center: {
      lat: 59.95,
      lng: 30.33
    },
    zoom: 11
  };

  const handleApiLoaded = (map, maps) => {
    map.addListener('click', (e) => {
      console.log('Map clicked!', e);
    });
  };

  return (
    <div style={{ height: '100vh', width: '100%' }}>
      <GoogleMapReact
        bootstrapURLKeys={{ key: 'YOUR_API_KEY' }}
        defaultCenter={defaultProps.center}
        defaultZoom={defaultProps.zoom}
        yesIWantToUseGoogleMapApiInternals
        onGoogleApiLoaded={({ map, maps }) => handleApiLoaded(map, maps)}
      >
      </GoogleMapReact>
    </div>
  );
};

export default EventHandlingMap;
```

Other packages similar to google-map-react

Keywords

FAQs

Package last updated on 14 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc