-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from DaigakuNakayoshi/can-use-google-api
GoogleMapのAPIを一旦叩けるようにした
- Loading branch information
Showing
9 changed files
with
214 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FRONTEND_GOOGLE_API_KEY="xxx" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { Box } from "@chakra-ui/react"; | ||
import { createFileRoute } from "@tanstack/react-router"; | ||
import { | ||
APIProvider, | ||
Map as GoogleMap, | ||
useMap, | ||
useMapsLibrary, | ||
} from "@vis.gl/react-google-maps"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export const Route = createFileRoute("/directionMaps/")({ | ||
component: Index, | ||
}); | ||
|
||
const API_KEY = import.meta.env.FRONTEND_GOOGLE_API_KEY; | ||
|
||
export default function Index() { | ||
return ( | ||
<div style={{ width: "100%", height: "100vh" }}> | ||
<APIProvider apiKey={API_KEY}> | ||
<GoogleMap | ||
defaultZoom={8} | ||
gestureHandling={"greedy"} | ||
fullscreenControl={false} | ||
> | ||
<Directions /> | ||
</GoogleMap> | ||
</APIProvider> | ||
</div> | ||
); | ||
} | ||
|
||
// @see: https://developers.google.com/maps/documentation/javascript/directions?hl=ja | ||
function Directions() { | ||
const map = useMap(); | ||
const routesLibrary = useMapsLibrary("routes"); | ||
const [directionsService, setDirectionsService] = | ||
useState<google.maps.DirectionsService>(); | ||
const [directionsRenderer, setDirectionsRenderer] = | ||
useState<google.maps.DirectionsRenderer>(); | ||
const [routes, setRoutes] = useState<google.maps.DirectionsRoute[]>([]); | ||
const [routeIndex, setRouteIndex] = useState(0); | ||
const selected = routes[routeIndex]; | ||
const leg = selected?.legs[0]; | ||
|
||
// Initialize directions service and renderer | ||
useEffect(() => { | ||
if (!routesLibrary || !map) return; | ||
setDirectionsService(new routesLibrary.DirectionsService()); | ||
setDirectionsRenderer(new routesLibrary.DirectionsRenderer({ map })); | ||
}, [routesLibrary, map]); | ||
|
||
// Use directions service | ||
useEffect(() => { | ||
if (!directionsService || !directionsRenderer) return; | ||
|
||
directionsService | ||
.route({ | ||
origin: "東京都新宿区西新宿2丁目8-1", // 都庁 | ||
waypoints: [{ location: "東京都港区赤坂9-7-1", stopover: true }], // ミッドタウン | ||
destination: "東京都千代田区丸の内1丁目9番1号", // 東京駅 | ||
travelMode: google.maps.TravelMode.DRIVING, | ||
provideRouteAlternatives: true, | ||
avoidHighways: true, | ||
}) | ||
.then((response) => { | ||
directionsRenderer.setDirections(response); | ||
setRoutes(response.routes); | ||
}); | ||
|
||
return () => directionsRenderer.setMap(null); | ||
}, [directionsService, directionsRenderer]); | ||
|
||
// Update direction route | ||
useEffect(() => { | ||
if (!directionsRenderer) return; | ||
|
||
directionsRenderer.setRouteIndex(routeIndex); | ||
}, [routeIndex, directionsRenderer]); | ||
|
||
if (!leg) return null; | ||
|
||
return ( | ||
<Box | ||
position="absolute" | ||
top={4} | ||
right={4} | ||
bg="white" | ||
p={4} | ||
borderRadius="md" | ||
shadow="lg" | ||
maxW="sm" | ||
maxH="80vh" | ||
overflowY="auto" | ||
fontSize={10} | ||
> | ||
<div className="directions"> | ||
<h2>{selected.summary || "ルート"}</h2> | ||
|
||
{selected.legs.map((leg) => ( | ||
<div key={`${leg.start_address}-${leg.end_address}`}> | ||
<p> | ||
{leg.start_address.split(",")[0]} | ||
<br />→ {leg.end_address.split(",")[0]} | ||
</p> | ||
<p>距離: {leg.distance?.text}</p> | ||
<p>所要時間: {leg.duration?.text}</p> | ||
</div> | ||
))} | ||
</div> | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { createFileRoute } from "@tanstack/react-router"; | ||
import { | ||
APIProvider, | ||
AdvancedMarker, | ||
Map as GoogleMap, | ||
} from "@vis.gl/react-google-maps"; | ||
|
||
export const Route = createFileRoute("/maps/")({ | ||
component: Index, | ||
}); | ||
|
||
const API_KEY = import.meta.env.FRONTEND_GOOGLE_API_KEY; | ||
|
||
export default function Index() { | ||
const position = { lat: 35.689501375244, lng: 139.69173371705 }; | ||
|
||
return ( | ||
<div style={{ width: "100%", height: "100vh" }}> | ||
<APIProvider apiKey={API_KEY}> | ||
<GoogleMap | ||
defaultCenter={position} | ||
defaultZoom={14} | ||
mapId={"DEMO_MAP_ID"} | ||
style={{ width: "100%", height: "100%" }} | ||
> | ||
<AdvancedMarker position={position} /> | ||
</GoogleMap> | ||
</APIProvider> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
interface ImportMetaEnv { | ||
readonly FRONTEND_GOOGLE_API_KEY: string; | ||
} | ||
|
||
interface ImportMeta { | ||
readonly env: ImportMetaEnv; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters