Advertisement
Fhernd

Untitled

Sep 29th, 2023
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useRef } from 'react';
  2. import { GoogleMap, LoadScript } from '@react-google-maps/api';
  3.  
  4. function MapComponent() {
  5.   const [zoom, setZoom] = useState(8); // Initial zoom level
  6.   const mapRef = useRef(null);
  7.  
  8.   const handleZoomChanged = () => {
  9.     if (mapRef.current) {
  10.       const newZoom = mapRef.current.getZoom();
  11.       setZoom(newZoom);
  12.     }
  13.   };
  14.  
  15.   return (
  16.     <LoadScript googleMapsApiKey="YOUR_GOOGLE_MAPS_API_KEY">
  17.       <GoogleMap
  18.         id="example-map"
  19.         mapContainerStyle={{ width: '400px', height: '400px' }}
  20.         center={{ lat: -34.397, lng: 150.644 }}
  21.         zoom={zoom}
  22.         onLoad={map => mapRef.current = map} // Store the map object in ref once loaded
  23.         onZoomChanged={handleZoomChanged}
  24.       />
  25.     </LoadScript>
  26.   );
  27. }
  28.  
  29. export default MapComponent;
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement