Repository navigation
Add transit layer in <Map/> #440
Answered
by
usefulthink
MicheleFatticcioni
asked this question in
Q&A
|
Hello, I'm working with '@vis.gl/react-google-maps' and AdvancedMarker. I'm trying to add a transit layer to my map, but I can't find a way to do it. |
Answered by
usefulthink
Jul 3, 2024
Replies: 1 comment 2 replies
|
You can roughly follow this guide here: https://visgl.github.io/react-google-maps/docs/guides/interacting-with-google-maps-api#hooks For example, you create a const TransitLayer = () => {
const map = useMap();
useEffect(() => {
if (!map) return;
const transitLayer = new google.maps.TransitLayer();
transitLayer.setMap(map);
return () => transitLayer.setMap(null);
}, [map]);
return <></>;
}(since this component doesn't need to render anything, it would look almost the same as a custom hook |
2 replies
Answer selected by
MicheleFatticcioni
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

You can roughly follow this guide here: https://visgl.github.io/react-google-maps/docs/guides/interacting-with-google-maps-api#hooks
For example, you create a
TransitLayercomponent and use theuseMap/useEffectconstruct to add the transitLayer:(since this component doesn't need to render anything, it would look almost the same as a custom hook
useTransitLayerif you prefer it that way)