let's get IT with DAVINA ๐Ÿ’ป

KakaoMap Api ์ ์šฉ ๋ณธ๋ฌธ

Project/์™œ ๊ฐ€?

KakaoMap Api ์ ์šฉ

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 2. 9. 05:06

์•ฑํ‚ค ๋ฐ›๊ธฐ

Kakao Developers

  1. ์šฐ์„  ์นด์นด์˜ค ๊ฐœ๋ฐœ์ž ์‚ฌ์ดํŠธ์— ์ ‘์†ํ•ด์„œ ์นด์นด์˜ค ๋กœ๊ทธ์ธ์„ ํ•œ๋‹ค.
  2. ๋‚ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋“ค์–ด๊ฐ€์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ถ”๊ฐ€ํ•˜๊ธฐ๋ฅผ ๋ˆ„๋ฅธ๋‹ค.
  3. ํ”Œ๋žซํผ ์„ค์ •ํ•˜๊ธฐ๋ฅผ ๋ˆ„๋ฅด๊ณ  (๋‚˜๋Š” ์›น์—์„œ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— web ํ”Œ๋žซํผ ๋“ฑ๋ก์„ ์„ ํƒํ–ˆ๋‹ค) ๋„๋ฉ”์ธ ์„ค์ •์„ํ•ด์ค€๋‹ค.
  4. react๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์›น์„ ๊ตฌํ˜„ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— http: // localhost:3000๋„ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.

HTML์— ์ ์šฉ

  • pubilc/index.html
    • head or body ์•„๋ฌด๋ฐ๋‚˜ ์ƒ๊ด€ X
    • ๋‚˜๋Š” head์— ์ ์šฉํ•ด์ฃผ์—ˆ๋‹ค.
<head>
        <script
            type="text/javascript"
            src="//dapi.kakao.com/v2/maps/sdk.js?appkey=[๋ฐœ๊ธ‰๋ฐ›์€ ํ‚ค]"
        ></script>
        <meta charset="utf-8" />
        <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
//...์ƒ๋žต
</head>
  • KakaoMap.js ํŒŒ์ผ ์ƒ์„ฑ
import React, { useEffect } from "react";
import styled from "styled-components";

const MapContainer = styled.div`
    width: 100%;
    height: 400px;
`;

const { kakao } = window;

//๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ถ€๋ชจํŽ˜์ด์ง€์—์„œ props๋กœ location={pageDetail}์ „๋‹ฌ
export const KakaoMap = ({ location }) => {
    useEffect(() => {
        var container = document.getElementById("map"); //์ง€๋„๋ฅผ ๋‹ด์„ ์˜์—ญ์˜ DOM ๋ ˆํผ๋Ÿฐ์Šค
        var options = {
            center: new kakao.maps.LatLng(
                location.location_x,
                location.location_y
            ), //์ง€๋„์˜ ์ค‘์‹ฌ์ขŒํ‘œ
            level: 3,
        };

        var map = new kakao.maps.Map(container, options); //์ง€๋„ ์ƒ์„ฑ ๋ฐ ๊ฐ์ฒด ๋ฆฌํ„ด
        var markerPosition = new kakao.maps.LatLng(
            location.location_x,
            location.location_y
        ); //ํ•ด๋‹น ์œ„์น˜ ๋งˆ์ปค ํ‘œ์‹œ
        var marker = new kakao.maps.Marker({
            position: markerPosition,
        });
        marker.setMap(map);
    }, [location]);

    return (
        <div>
            <MapContainer id="map"></MapContainer>
        </div>
    );
};

env

์•ฑํ‚ค๋Š” ๋‹ค๋ฅธ์‚ฌ๋žŒ์ด ๊ฐ€์ ธ๊ฐ€์„œ ์•…์šฉํ•  ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— .envํŒŒ์ผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

์ œ์ผ ๋ฐ– ํด๋”์— .env ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ์นด์นด์˜ค ์•ฑํ‚ค๋ฅผ ์„ค์ •ํ•ด ์ค€๋‹ค. ๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ๋ฆฌ์•กํŠธ๋Š” REACT_APP์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฆ„์œผ๋กœ ์ง€์–ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ ‡์ง€์•Š์œผ๋ฉด create-react-app์ด ์ด๊ฒƒ์„ ์ธ์ง€ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 ๐Ÿšซ Error Issue
๋˜ํ•œ, kakao developers์—์„œ ํ”Œ๋žซํผ→web→๋„๋ฉ”์ธ ์ฃผ์†Œ ์ถ”๊ฐ€ํ•ด์ค˜์•ผํ•จ!

.env

REACT_APP_KAKAO_MAP_KEY=[์นด์นด์˜ค ์•ฑํ‚ค]

๐Ÿ‘‡ .env๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ”๋€ ์ฝ”๋“œ

  • %%๋ฅผ ์–‘๋์— ์ถ”๊ฐ€
<body>
    <script
      type="text/javascript"
      src="//dapi.kakao.com/v2/maps/sdk.js?appkey=%REACT_APP_KAKAO_MAP_KEY%"
    ></script>
  </body>
๐Ÿšซ Error Issue
“sdk.js?appkey=[๋ฐœ๊ธ‰๋ฐ›์€ํ‚ค] A parser-blocking, cross site (i.e. different eTLD+1) script, http://t1.daumcdn.net/mapjsapi/js/main/4.4.8/kakao.js, is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message. See https://www.chromestatus.com/feature/5718547946799104 for more details.”๋ผ๋Š” warning์ด ๋–ด์Œ

→ ๊ฒ€์ƒ‰ํ•ด๋ณด๋‹ˆ “ํ•ด๋‹น ๋ฌธ๊ตฌ๋Š” document.write๋กœ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์€ ๊ฐ€๊ธ‰์  ์ž์ œํ•ด ๋‹ฌ๋ผ๋Š” ๋ฌธ๊ตฌ์ด๋ฉฐ API ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋œจ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. (2G ๊ฐ™์€)๋Š๋ฆฐ ๋„คํŠธ์›Œํฌ ์ƒ์—์„œ ์Šคํฌ๋ฆฝํŠธ ๋ธ”๋ฝ์ด ์ผ์–ด๋‚  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ฌธ๊ตฌ์ธ๋ฐ ์‹ค์ œ ์ง€๋„ ๋™์ž‘์—๋Š” ๋ฌธ์ œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ •์ฑ…์ ์œผ๋กœ ๋Œ€์‘ํ•˜์ง€ ์•Š๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.”๋ผ๋Š” ์นด์นด์˜ค๋‹ต๋ณ€์ด ์žˆ์—ˆ๋‹ค๊ณ ํ•ด์„œ ์ผ๋‹จ ๋ฌด์‹œํ•˜๊ณ  ์ง„ํ–‰

.gitignore์— .env ์ถ”๊ฐ€ํ•˜๊ธฐ

๋ณดํ†ต Github์— ๋ณธ์ธ์˜ ์ฝ”๋“œ๋ฅผ pushํ•ด์„œ ๊ด€๋ฆฌํ•œ๋‹ค. ์ด ๋•Œ, .env ํŒŒ์ผ์ด ๊ฐ™์ด ์˜ฌ๋ผ๊ฐ€๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ? ๊ฐœ์ธ API KEY๋ฅผ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ๋‚˜์œ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

๋”ฐ๋ผ์„œ, .gitignore์— .env ํŒŒ์ผ์„ ์ž…๋ ฅํ•ด์„œ push๋ฅผ ๋ฏธ์—ฐ์— ๋ฐฉ์ง€ํ•˜์ž.


API KEY ์‚ฌ์šฉํ•˜๊ธฐ

  1. public/index.html์—์„œ <script> ํƒœ๊ทธ์˜ src ์†์„ฑ์— ์ž…๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ
  2. src ํด๋”์˜ js, jsx, ts, tsx ํŒŒ์ผ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

public/index.html

public/index.html์—์„œ <script> ํƒœ๊ทธ์˜ src ์†์„ฑ์— ์ž…๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” % ๊ธฐํ˜ธ๋ฅผ ํ™œ์šฉํ•œ๋‹ค.

%.env์— ์ €์žฅํ•ด๋†“์€ ์ด๋ฆ„%

src/ ~.js, ~.jsx, ~.ts, ~.tsx

src ํด๋”์˜ js, jsx, ts, tsx ํŒŒ์ผ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” process.env๋ฅผ ํ™œ์šฉํ•œ๋‹ค.

// App.js

function App() {
  console.log(process.env.REACT_APP_GOOGLE_MAP_API_KEY); // API KEY ์ถœ๋ ฅ
}

export default App;
Comments