Skip to content

Add transit layer in <Map/> #440

Discussion options

You must be logged in to vote

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 TransitLayer component and use the useMap / useEffect construct to add the transitLayer:

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 useTransitLayer if you prefer it that way)

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@MicheleFatticcioni
Comment options

@usefulthink
Comment options

Answer selected by MicheleFatticcioni
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants