Ask Your Question
3

Is it possible to utilize OverlayView in JavaScript API Google Maps Reactjs without a class component?

asked 2022-03-04 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-07-07 22:00:00 +0000

bukephalos gravatar image

Yes, it is possible to utilize OverlayView in JavaScript API Google Maps Reactjs without a class component by using a functional component with hooks such as useEffect and useRef. Here is an example:

import React, { useEffect, useRef } from 'react';
import { GoogleMap, OverlayView } from '@react-google-maps/api';

const mapContainerStyle = {
  height: '400px',
  width: '100%'
};

const center = {
  lat: 37.7749,
  lng: -122.4194
};

const radius = 50; // in meters

const MyOverlay = ({ map }) => {
  const overlayRef = useRef(null);

  useEffect(() => {
    const overlay = overlayRef.current;

    overlay.draw = function () {
      const projection = this.getProjection();
      const center = projection.fromLatLngToDivPixel(this.props.center);

      const radiusInPixels = (radius / 6371000) * (2 ** 21) * Math.cos((this.props.center.lat * Math.PI) / 180);

      const bounds = new window.google.maps.Circle({
        center: this.props.center,
        radius: radiusInPixels
      }).getBounds();

      const position = {
        top: center.y - radiusInPixels,
        left: center.x - radiusInPixels,
        width: radiusInPixels * 2,
        height: radiusInPixels * 2
      };

      Object.assign(overlay.style, position);
    };

    overlay.setMap(map);

    return () => {
      overlay.setMap(null);
    };
  }, [map]);

  return <div ref={overlayRef} style={{ position: 'absolute', zIndex: 10, cursor: 'default' }} />;
};

const MyMap = () => {
  const mapRef = useRef();

  const onLoad = (map) => {
    mapRef.current = map;
  };

  const onUnmount = () => {
    mapRef.current = null;
  };

  return (
    <GoogleMap
      mapContainerStyle={mapContainerStyle}
      center={center}
      zoom={14}
      onLoad={onLoad}
      onUnmount={onUnmount}
    >
      <MyOverlay map={mapRef.current} center={center} />
    </GoogleMap>
  );
};

export default MyMap;

In this example, we use a functional component MyOverlay to create a custom overlay that represents a circle with a given radius around a center point. We use the useRef hook to get a reference to the div element that will be the container for our custom overlay. We use the useEffect hook to initialize the overlay and set its position and size based on the center point and radius. We also use the setMap and setMap(null) methods to show and hide the overlay when the map is loaded and unloaded.

We then use MyOverlay component as a child of the GoogleMap component in another functional component MyMap. We use the useRef hook to get a reference to the map instance and pass it along with the center point as props to the MyOverlay component. We also use the onLoad and onUnmount props to initialize and clean up the map instance.

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-03-04 11:00:00 +0000

Seen: 9 times

Last updated: Jul 07 '21