1

Im wondering if it is even possible. Found some libraries on the internet but their docs are poorly described or outdated. Im trying to have fully working streetview using google API key

I was trying using @react-google-maps/api library with docs at https://react-google-maps-api-docs.netlify.app/ - unfortunatelly i dont really know how to implement their representation in my component. For example their code:

const { GoogleMap, LoadScript } = require("../../");
const ScriptLoaded = require("../../docs/ScriptLoaded").default;

const mapContainerStyle = {
  height: "400px",
  width: "800px"
};

const center = {
  lat:  51.5320665,
  lng: -0.177203
};

<ScriptLoaded>
  <GoogleMap
    id="circle-example"
    mapContainerStyle={mapContainerStyle}
    zoom={7}
    center={center}
  >
    <StreetViewPanorama
      position={center}
      visible={true}
    />
  </GoogleMap>
</ScriptLoaded>

Something I managed to create is:

import React from "react";
import {
  GoogleMap,
  LoadScript,
  StreetViewPanorama
} from "@react-google-maps/api";

function Map() {
  const containerStyle = {
    height: "400px",
    width: "800px",
  };

  const center = {
    lat: 54.364442,
    lng: 18.643173,
  };
  return (
    <LoadScript googleMapsApiKey={process.env.REACT_APP_GOOGLE_MAPS_API_KEY}>
      <GoogleMap mapContainerStyle={containerStyle} center={center} zoom={10}>
        <StreetViewPanorama
          id="street-view"
          mapContainerStyle={containerStyle}
          position={center}
          visible={true}
        />
      </GoogleMap>
    </LoadScript>
  );
}

export default React.memo(Map);

Unfortunatelly, this renders only the default map, instead of panorama.

If there is any other way please give me some hint :)

Update:

I'm using react routing and would like the panorama to be working in one of the routes. This is the structure of index.js:

import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { BrowserRouter } from "react-router-dom";
import { applyMiddleware, combineReducers, createStore } from "redux";
import gamesReducer from "./ducks/games/reducers";
import mapsReducer from "./ducks/maps/reducers";
import thunk from "redux-thunk";
import { Provider } from "react-redux";
import logger from "redux-logger";

const store = createStore(
  combineReducers({
    games: gamesReducer,
    maps: mapsReducer,
  }),
  applyMiddleware(thunk, logger)
);

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <Provider store={store}>
      <BrowserRouter>
        <App />
      </BrowserRouter>
    </Provider>
  </React.StrictMode>
);

reportWebVitals();

App.js:

import "./App.css";
import { Routes, Route } from "react-router-dom";
import Home from "./pages/Home";
import NotFound from "./pages/NotFound";
import MapRoutes from "./pages/MapRoutes";
import GameRoutes from "./pages/GameRoutes";
import { LoadScript } from "@react-google-maps/api";

function App() {
  return (
    <Routes>
      <Route path="/" element={<Home />}></Route>
      <Route path="/maps/*" element={<MapRoutes />}></Route>
      <Route path="/game/*" element={<GameRoutes />}></Route>
      <Route path="*" element={<NotFound />}></Route>
    </Routes>
  );
}

export default App;

MapRoutes.js:

import React from "react";
import { Routes, Route } from "react-router-dom";
import MapsList from "./MapsList";
import MapDetails from "./MapDetails";
import NewMap from "./NewMap";
import NotFound from "./NotFound";
import Map from "./Map";

export default function MapRoutes() {
  return (
    <div>
      <Routes>
        <Route index element={<MapsList />} />
        <Route path=":id" element={<MapDetails />} />
        <Route path="new" element={<NewMap />} />
        <Route path="test" element={<Map />} />
        <Route path="*" element={<NotFound />}></Route>
      </Routes>
    </div>
  );
}

1 Answers1

0

I tried reproducing your code and somehow made it work after importing the <LoadScript> on the index.js instead of the Map.js.

Here's what the code looks like:

index.html

<div id="root"></div>

Maps.js

import React from "react";
import { GoogleMap, StreetViewPanorama } from "@react-google-maps/api";

function Map() {
  const containerStyle = {
    height: "400px",
    width: "800px"
  };

  const center = {
    lat: 54.364442,
    lng: 18.643173
  };
  return (
    <GoogleMap mapContainerStyle={containerStyle} center={center} zoom={10}>
      <StreetViewPanorama
        id="street-view"
        mapContainerStyle={containerStyle}
        position={center}
        visible={true}
      />
    </GoogleMap>
  );
}

export default Map;

index.js

import React from "react";
import { render } from "react-dom";
import { LoadScript } from "@react-google-maps/api";
import Map from "./Map";
import "./styles.css";

const lib = ["places"];
const key = ""; // PUT GMAP API KEY HERE
class App extends React.Component {
  render() {
    return (
      <LoadScript googleMapsApiKey={key} libraries={lib}>
        <Map />
      </LoadScript>
    );
  }
}

render(<App />, document.getElementById("root"));

I think you can still use the process.env.REACT_APP_GOOGLE_MAPS_API_KEY on the key value for the API key just like on your sample code.

styles.css

h1,
p {
  font-family: Lato;
}

div {
  margin-bottom: 10px;
  margin-top: 10px;
}

Here's a proof of concept fiddle for you to try out.

Hope this helps!

Yrll
  • 1,619
  • 2
  • 5
  • 19
  • Hey buddy, your answer is actually correct and it answers the question, thank you for that! However, I have little more complicated project structure with routing and my question is where should I put the Loadscript component? it doesnt seem to work for me when I put it in index.js, App.js or MapRoutes.js. Ill update my question with that files provided – Jakub Falkiewicz Dec 19 '22 at 02:17
  • Glad it helped! I just checked your update, where did you put your component? – Yrll Dec 19 '22 at 02:29
  • `` should contain `` component as its child. – Yrll Dec 19 '22 at 02:30
  • My Map component is inside MapRoutes in Route path "test". Rednering the path as ```} />``` doesnt render panorama... – Jakub Falkiewicz Dec 19 '22 at 13:25
  • Then is your `` component inside your `` component just like your example above? Did you try removing the `` that wraps the ``? – Yrll Dec 19 '22 at 15:43
  • `````` component is still inside my `````` container as in your answer. Also `````` is in the same place as in [your code](https://codesandbox.io/s/streetview-pano-proof-of-concept-fiddle-wiws7g?file=/src/Map.js) – Jakub Falkiewicz Dec 19 '22 at 17:43
  • Will you be able to provide a code snippet in a codesandbox? So that we could take a look at it on our end? And also to see if there's any errors. Would greatly appreciate that. – Yrll Dec 19 '22 at 23:18
  • Ok, so thats [my code](https://codesandbox.io/s/react-panorama-0o3tbo). If you follow the routing you should find Map component in path ```/maps/test``` – Jakub Falkiewicz Dec 20 '22 at 16:07
  • I can see that your code does not even render anything. I don't have much experience on using react-router-dom. And I can't see any implementation of the react-google-maps/api library using it. I don't even see any error when I did not put my API key. Does this code reproduce the issue? Or does it work in the first place? What's the reason why you need to use the `routes` instead of the standard documented way? – Yrll Dec 21 '22 at 05:35
  • I deleted some unnecessary code so that the issue is reproduced at main path. I want to make [Geoguessr](https://www.geoguessr.com/) clone. I need routing for my service to provide multiple "rooms" for players to play. Every room would have its own ID and its data was stored in the database. – Jakub Falkiewicz Dec 21 '22 at 11:39
  • Thanks for updating it! I was playing around with your code and somehow fixed it and the panorama appeared when I removed `React.Strictmode` that was encapsulating the `BrowserRouter` and `App` elements on your `index.js` folder. Can you confirm it on your end? – Yrll Dec 21 '22 at 23:38
  • Here's a [forked sandbox](https://codesandbox.io/s/react-panorama-forked-t1vk81) of your code where I just removed the `React.Strictmode` and the streetview now working. – Yrll Dec 21 '22 at 23:41
  • 1
    Yooo, I do be working now. How did you even consider the ```React.Strictmode``` to cause the problem? Im impressed. Thank you very much once again! – Jakub Falkiewicz Dec 22 '22 at 01:26
  • I encountered a React question here in Stack Overflow before where the `React.Strictmode` caused them headache. I'm not that familiar with its functionality but I'm glad removing it fixed the issue. – Yrll Dec 22 '22 at 01:28
  • Okay then, I'll note this one haha – Jakub Falkiewicz Dec 22 '22 at 01:39