https://developers.google.com/maps?hl=ko
Google Maps Platform | Google Developers
Google Maps Platform ์ค๋ช
developers.google.com
๋จผ์ ์์ ๋งํฌ๋ก ๋ค์ด๊ฐ์ ์์ํ๊ธฐ๋ฅผ ๋๋ฅธ๋ค
์ฒ์ ๋ฐฉ๋ฌธํ๋ค๋ฉด ๊ฐ์ข ์ฝ๊ด ๋์๊ฐ ๋์ฌ ์ ๋์๋๋ฐ ๋์ํ๊ณ ๋์ด๊ฐ๋ค.
์ค๋ฅธ์ชฝ ์นดํ ๊ณ ๋ฆฌ์์ API๋ฅผ ๋๋ฅธ๋ค
์นด๋๋ฅผ ์ธ์ฆํ์ฌ ๋์ด๊ฐ๋ค,
์นด๋๋ฅผ ์ถ๊ฐํ๋ค๊ณ ๋ฐ๋ก ๋์ด ๋๊ฐ๋ ๊ฒ์ด ์๋ ๋งค์ 28,000ํ์ ํธ์ถ๋ฒ์ ์์์ ๋ฌด๋ฃ๋ก ์ฌ์ฉํ ์ ์๋ค๊ณ ํ๋ค, ์ด์ธ์๋ ์ค์ ์ผ๋ก ํตํด ๊ณผ๊ธ ๋ฒ์๊ฐ ๋ค๊ฐ์ค๋ฉด ์๋์ด๋ ํ๋๋ฅผ ์ค์ ํ์ฌ ์๊ธ์ด ์ฒญ๊ตฌ๋์ง ์๊ฒ ํ ์ ์๋ค.
์ ์ฌ์ง ์ฒ๋ผ ์ฌ์ฉ์ ์ธ์ฆ ์ ๋ณด๋ก ๋ค์ด๊ฐ์ Maps API Key๋ฅผ ํด๋ฆญํ๋ค.
๊ทธ๋ฌ๋ฉด ์ด๋ฆ ์ค๋ฅธ์ชฝ์ API key๋ผ๊ณ ๋์ ์์ํ ๋ฐ ๋ฐ๋ก ๋ฉ๋ชจ์ฅ์ ๋ณต์ฌํด๋๊ณ .
.env ํ์ผ์ ๋ง๋ค์ด์ ๋ณด๊ดํด๋์ ์๋์์๋ NEXT_PUBLIC_GOOGLE_API_KEY๋ผ๋ env ๋ช ์ ํตํด ๋ถ๋ฌ์๋ค.
npm i @react-google-maps/api
npm i react-geocode
์์ ๋๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํด์ฃผ์
@react-google-maps/api
React.js Google Maps API integration. Latest version: 2.18.1, last published: 2 months ago. Start using @react-google-maps/api in your project by running `npm i @react-google-maps/api`. There are 235 other projects in the npm registry using @react-google-m
www.npmjs.com
react-geocode
A module to transform a description of a location (i.e. street address, town name, etc.) into geographic coordinates (i.e. latitude and longitude) and vice versa.. Latest version: 0.2.3, last published: 2 years ago. Start using react-geocode in your projec
www.npmjs.com
import { GoogleMap, LoadScriptNext, MarkerF } from "@react-google-maps/api";
import React, { useMemo, useEffect, useState } from "react";
import styled from "styled-components";
import GeoCode from "@/lib/Google-geocode";
const Wrapper = styled.div`
.map-container {
width: 100%;
height: 540px;
}
`;
const Map = ({ address }: any) => {
const [location, setLocation] = useState({ lat: 0, lng: 0 });
useEffect(() => {
GeoCode(address).then((res) => {
setLocation({
lat: res?.lat,
lng: res?.lng,
});
});
}, [address]);
const defaultCenter: any = {
lat: location ? location.lat : 0.0,
lng: location ? location.lng : 0.0,
};
const defaultZoom = 16;
return (
<Wrapper>
<LoadScriptNext
googleMapsApiKey={`${process.env.NEXT_PUBLIC_GOOGLE_API_KEY}`}
>
<GoogleMap
clickableIcons={true}
zoom={defaultZoom}
center={defaultCenter}
mapContainerClassName="map-container"
>
<MarkerF position={defaultCenter} />
</GoogleMap>
</LoadScriptNext>
</Wrapper>
);
};
export default Map;
Geocode๋ฅผ ์ฌ์ฉํ์ง ์์ผ๋ฉด { lat : , lng: } ์ฒ๋ผ ์ขํ๋ฅผ ๋ด์๋์ ๊ฐ์ฒด๋ฅผ ๋ฐ๋ก GoogleMap์ center props๋ก ๋ฃ์ด์ฃผ๋ฉด ๋๋๋ฐ
Geocode๋ ๋ฌธ์์ด๋ก ์ ๋ ฅํ ์ฃผ์๋ฅผ ( ์ : ์์ธ์์ฒญ ) ์ ๋ ฅํ๋ฉด ์ขํ์ฝ๋๋ก ๋ฐํํด์ค๋ค
import Geocode from "react-geocode";
Geocode.setApiKey(process.env.NEXT_PUBLIC_GOOGLE_API_KEY || "");
Geocode.setLanguage("en");
Geocode.enableDebug();
const GeoCode = async (currentAddr: string) => {
return Geocode.fromAddress(currentAddr)
.then((response) => {
const { lat, lng } = response.results[0].geometry.location;
return { lat, lng };
})
.catch((err) => console.log(err));
};
export default GeoCode;
์์ธํ๊ฑด ์์์ ์ฌ๋ ค๋จ๋ npm ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ฃผ์๋ ๊นํ๋ธ์ ๊ฐ๋ฉด ์์ธํ ๋์์๋ค ์์ฃผ ์ฌ์ฉํ๊ธฐ ์ฝ๋ค.
'โAPI&npm&๋ผ์ด๋ธ๋ฌ๋ฆฌ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฆฌ์กํธ ํน์ ์๊ฐ ์ปดํฌ๋ํธ ์๋ก๊ณ ์นจ!! node-schedule (0) | 2023.07.18 |
---|