LocationMap.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { GoogleMap, useJsApiLoader } from '@react-google-maps/api';
  2. import { Card } from '@vendure/admin-ui/react';
  3. import React, { useEffect } from 'react';
  4. const containerStyle = {
  5. width: '100%',
  6. height: '400px',
  7. };
  8. const center = {
  9. lat: 48.212616,
  10. lng: 16.3230408,
  11. };
  12. export function LocationMap() {
  13. const { isLoaded } = useJsApiLoader({
  14. id: 'google-map-script',
  15. googleMapsApiKey: 'AIzaSyCKxhBHUymQG7L57NeRhJRdzlvO4kcymXU',
  16. });
  17. const [map, setMap] = React.useState(null);
  18. const onLoad = React.useCallback(function callback(map) {
  19. // This is just an example of getting and using the map instance!!! don't just blindly copy!
  20. const bounds = new window.google.maps.LatLngBounds(center);
  21. map.fitBounds(bounds);
  22. setMap(map);
  23. new window.google.maps.Marker({
  24. position: center,
  25. map,
  26. title: 'Hello World!',
  27. });
  28. }, []);
  29. useEffect(() => {
  30. setTimeout(() => {
  31. (map as any)?.setZoom(9);
  32. }, 1000);
  33. }, [map]);
  34. const onUnmount = React.useCallback(function callback(map) {
  35. setMap(null);
  36. }, []);
  37. return isLoaded ? (
  38. <div className="mb-4">
  39. <Card title="Location">
  40. <GoogleMap
  41. mapContainerStyle={containerStyle}
  42. center={center}
  43. onLoad={onLoad}
  44. onUnmount={onUnmount}
  45. options={{
  46. zoom: 20,
  47. minZoom: 10,
  48. fullscreenControl: false,
  49. streetViewControl: false,
  50. zoomControl: false,
  51. mapTypeControl: false,
  52. }}
  53. >
  54. {/* Child components, such as markers, info windows, etc. */}
  55. <></>
  56. </GoogleMap>
  57. </Card>
  58. </div>
  59. ) : (
  60. <></>
  61. );
  62. }