New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@googlemaps/react-wrapper

Package Overview
Dependencies
Maintainers
0
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@googlemaps/react-wrapper

React component that wraps the loading of Google Maps JavaScript API.

  • 1.1.42
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
153K
increased by8.32%
Maintainers
0
Weekly downloads
 
Created

What is @googlemaps/react-wrapper?

@googlemaps/react-wrapper is an npm package that provides a simple way to integrate Google Maps into React applications. It acts as a wrapper around the Google Maps JavaScript API, making it easier to use within the React ecosystem.

What are @googlemaps/react-wrapper's main functionalities?

Basic Map Rendering

This code demonstrates how to render a basic Google Map using the @googlemaps/react-wrapper package. The Wrapper component takes an API key and a render function to handle different loading states.

import React from 'react';
import { Wrapper, Status } from '@googlemaps/react-wrapper';

const render = (status) => {
  if (status === Status.LOADING) return <p>Loading...</p>;
  if (status === Status.FAILURE) return <p>Error loading map</p>;
  return null;
};

const MyMapComponent = () => {
  return (
    <Wrapper apiKey={"YOUR_API_KEY"} render={render}>
      <div id="map" style={{ height: '400px', width: '100%' }}></div>
    </Wrapper>
  );
};

export default MyMapComponent;

Adding Markers

This code demonstrates how to add a marker to a Google Map using the @googlemaps/react-wrapper package. The useEffect hook initializes the map and adds a marker to it.

import React, { useEffect, useRef } from 'react';
import { Wrapper, Status } from '@googlemaps/react-wrapper';

const render = (status) => {
  if (status === Status.LOADING) return <p>Loading...</p>;
  if (status === Status.FAILURE) return <p>Error loading map</p>;
  return null;
};

const MyMapComponent = () => {
  const mapRef = useRef(null);

  useEffect(() => {
    if (mapRef.current) {
      const map = new window.google.maps.Map(mapRef.current, {
        center: { lat: -34.397, lng: 150.644 },
        zoom: 8,
      });

      new window.google.maps.Marker({
        position: { lat: -34.397, lng: 150.644 },
        map,
        title: 'Hello World!',
      });
    }
  }, []);

  return (
    <Wrapper apiKey={"YOUR_API_KEY"} render={render}>
      <div id="map" ref={mapRef} style={{ height: '400px', width: '100%' }}></div>
    </Wrapper>
  );
};

export default MyMapComponent;

Handling Map Events

This code demonstrates how to handle map events, such as clicks, using the @googlemaps/react-wrapper package. The useEffect hook initializes the map and sets up an event listener for map clicks.

import React, { useEffect, useRef } from 'react';
import { Wrapper, Status } from '@googlemaps/react-wrapper';

const render = (status) => {
  if (status === Status.LOADING) return <p>Loading...</p>;
  if (status === Status.FAILURE) return <p>Error loading map</p>;
  return null;
};

const MyMapComponent = () => {
  const mapRef = useRef(null);

  useEffect(() => {
    if (mapRef.current) {
      const map = new window.google.maps.Map(mapRef.current, {
        center: { lat: -34.397, lng: 150.644 },
        zoom: 8,
      });

      map.addListener('click', (e) => {
        alert(`Clicked at: ${e.latLng}`);
      });
    }
  }, []);

  return (
    <Wrapper apiKey={"YOUR_API_KEY"} render={render}>
      <div id="map" ref={mapRef} style={{ height: '400px', width: '100%' }}></div>
    </Wrapper>
  );
};

export default MyMapComponent;

Other packages similar to @googlemaps/react-wrapper

Keywords

FAQs

Package last updated on 09 Aug 2024

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