PositionedStreetView.jsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import React, { useEffect, useRef } from 'react';
  2. import { usePanoStartPosition, usePanoStartPov, useTargetPoint } from '../../../domain/gameStore';
  3. import { savePanoPositionToLocalStorage, savePanoPovToLocalStorage } from '../../../domain/localStorageMethods';
  4. import styles from './GamePanel.module.css';
  5. import usePano from './usePano';
  6. export default () => {
  7. const startPosition = usePanoStartPosition();
  8. const startPov = usePanoStartPov();
  9. const resetPosition = useTargetPoint();
  10. const panoDivRef = useRef(null);
  11. const panoRef = usePano(panoDivRef, startPosition, startPov);
  12. useEffect(
  13. () => {
  14. if (panoRef.current) {
  15. panoRef.current.addListener('position_changed', () => {
  16. const { lat, lng } = panoRef.current.getPosition();
  17. savePanoPositionToLocalStorage(lat(), lng());
  18. });
  19. panoRef.current.addListener('pov_changed', () => {
  20. const { heading, pitch } = panoRef.current.getPov();
  21. savePanoPovToLocalStorage(heading, pitch);
  22. });
  23. }
  24. },
  25. [ panoRef ]
  26. );
  27. return (
  28. <>
  29. <div className={styles.fullsize} ref={panoDivRef} />
  30. <div className={styles.resetButton} onClick={() => panoRef.current.setPosition(resetPosition)}>
  31. Reset
  32. </div>
  33. </>
  34. );
  35. };