Skip to content

Commit

Permalink
markerを表示する
Browse files Browse the repository at this point in the history
  • Loading branch information
bucchi-kitamura committed Dec 21, 2022
1 parent 912d89f commit 94b199e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
48 changes: 48 additions & 0 deletions src/Components/MarkersOnGoogleMap.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
GoogleMap as GoogleMapComponent,
MarkerF,
} from "@react-google-maps/api";
import React, { FC } from "react";
import { useMap } from "./useMap";

type Props = {
defaultPosition: {
lat: number;
lng: number;
};
};

const MarkersOnGoogleMap: FC<Props> = (props) => {
const { isLoaded, onLoad } = useMap({
defaultPosition: props.defaultPosition,
});

const containerStyle = {
width: "100vw",
height: "75vh",
};

const markerLabel: google.maps.MarkerLabel = {
text: "ミライトデザイン",
fontFamily: "sans-serif",
fontSize: "15px",
fontWeight: "bold",
};

return (
<>
{isLoaded ? (
<GoogleMapComponent mapContainerStyle={containerStyle} onLoad={onLoad}>
<MarkerF
position={props.defaultPosition}
label={markerLabel}
></MarkerF>
</GoogleMapComponent>
) : (
"loading"
)}
</>
);
};

export default React.memo(MarkersOnGoogleMap);
8 changes: 6 additions & 2 deletions src/Components/useMap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useJsApiLoader } from "@react-google-maps/api";
import { googleMapsApiKey } from "./env";
import { useState } from "react";

export type Map = google.maps.Map;

type Props = {
defaultPosition: { lat: number; lng: number };
defaultPosition: google.maps.LatLngLiteral;
};

export const useMap = ({ defaultPosition }: Props) => {
Expand All @@ -14,10 +15,13 @@ export const useMap = ({ defaultPosition }: Props) => {
googleMapsApiKey,
});

const [map, setMap] = useState<Map | null>(null);

const onLoad = (map: Map) => {
const bounds = new window.google.maps.LatLngBounds(defaultPosition);
map.fitBounds(bounds);
setMap(map);
};

return { isLoaded, onLoad };
return { map, isLoaded, onLoad };
};

0 comments on commit 94b199e

Please sign in to comment.