๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โš™API&npm&๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

React/Next๊ตฌ๊ธ€๋งตApi ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•

by ๋…น์ฐจ๋ง›๊ฐœ๊ตฌ๋ฆฌ 2023. 3. 23.

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

 
๊ทธ ํ›„ ์•„๋ž˜์™€ ๊ฐ™์ด Map ์ปค์Šคํ…€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค๊ฑฐ๋‚˜ ์ปดํฌ๋„ŒํŠธ์•ˆ์— ๋ฐ”๋กœ ์ฃผ์ž…ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
 
์•„๋ž˜์˜  react-google-maps/api ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด ๊ฐ€์ ธ์˜ค GoogleMap, LoadScript, MarkerF ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ†ตํ•ด
props๊ฐ’๋งŒ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฐ”๋กœ googlemap์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
 
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 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ฃผ์†Œ๋‚˜ ๊นƒํ—ˆ๋ธŒ์— ๊ฐ€๋ฉด ์ž์„ธํžˆ ๋‚˜์™€์žˆ๋‹ค ์•„์ฃผ ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๋‹ค.

728x90