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.
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
Asked: 2022-03-04 11:00:00 +0000
Seen: 9 times
Last updated: Jul 07 '21
What is the process for generating a dynamic subdomain/URL using vue.js?
How can the style of the loader be modified while the form submission is being processed?
I'm attempting to develop a Javascript-based comments section for my website.
What are some feasible methods to enable MIDI file playback on a web browser?
How can I resolve the issue of being unable to use Fetch to POST an array of Selected Checkboxes?
What is the method to hide the scroll button when reaching the bottom?
What is the process of using a custom nunjucks filter to filter collections in an eleventy template?
What is the method to create a dynamic checkbox using API data?
How can the user id from the login object be utilized in the resolvers after logging in?