Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StreetViewPanorama won't show on a standalone page in react-google-map-api #3395

Open
iamhammyboi19 opened this issue Dec 9, 2024 · 2 comments

Comments

@iamhammyboi19
Copy link

Issue template

You can donate or became a sponsor https://opencollective.com/react-google-maps-api#category-CONTRIBUTE

If you want to ask question, please ask it in Github Discussions, Spectrum.chat or Slack channel

Please do not post unformatted code into issues, and please do not ask questions. Only real issues, PR's or feature requests are allowed. Minimal reproduction in codesandbox.io is required.

Help wanted

As mentioned in this issue,
the author of this project will only accept issues with codesandbox reproduction and PR with solution going forward.

Please provide an explanation of the issue

Your Environment

os: mac

node v22.11.0

react v18.2.0

vite v4.4.5

@react-google-maps/api v2.20.3

How does it behave?

StreetViewPanorama won't render when I try to load it from a new page using searchParams for the url http://localhost:5173/viewmap?lat=7.3953418&lng=3.9284954 it will just load the google map only. When I search address with usePlaceAutoComplete inside the same component it displays the StreetViewPanorama. But when I get the lat and lng as url query for the new page to show the StreetViewPanorama and try to render it on the new page it will just only show me the normal google map without displaying StreetViewPanorama as expected

const { isLoaded } = useJsApiLoader({
    id: "google-map-script",
  });

  // eslint-disable-next-line no-unused-vars
  const [searchParams, setSearchParams] = useSearchParams();

  const lat = Number(searchParams.get("lat"));
  const lng = Number(searchParams.get("lng"));
  const center = { lat, lng };

  return (
    <div>
      <>
        {isLoaded && lat && lng ? (
          <GoogleMap
            mapContainerStyle={containerStyle}
            zoom={12}
            center={center}
          >
            <StreetViewPanorama
              containerStyle={containerStyle}
              zoom={12}
              position={center}
              visible={true}
            />
          </GoogleMap>
        ) : (
          <></>
        )}
      </>
    </div>
  );
}

How should it behave correctly?

It should display StreetViewPanorama directly on a new page

Basic implementation of incorrect behavior in codesandbox.com

@mightym
Copy link

mightym commented Dec 10, 2024

I'm having the same issue. I assumed I could use the StreetViewPanorama standalone outside of a map.
In my usecase I have a split view, on the left the Map and on the right the Streetview.

@iamhammyboi19
Copy link
Author

iamhammyboi19 commented Dec 10, 2024

I solved it creating a StreetViewPanorama with new window.google.maps.StreetViewPanorama and linked a ref to an empty div. I saw this on chatgpt

  const streetViewRef = useRef(null);
  const onLoad = useCallback(
    (map) => {
      const panorama = new window.google.maps.StreetViewPanorama(
        streetViewRef.current,
        { position: center }
      );

      panorama.setVisible(true);

      map.setStreetView(panorama);
    },
    [setSearchParams]
  );
  
  // inside jsx
  <div ref={streetViewRef} style={streetViewContainerStyle}></div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants