[๐Ÿฆ„ ๋ฆฌ์•กํŠธ] Google Map API ์‚ฌ์šฉํ•˜๊ธฐ

dsfasdยท2022๋…„ 11์›” 20์ผ
0

์—์–ด๋น„์—”๋น„ ํด๋ก ์ฝ”๋”ฉ์„ ์ง„ํ–‰์ค‘์ธ๋ฐ, ์ง€๋„๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•ด์„œ google map api๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.


Google Map API ์‚ฌ์šฉ๋ฐฉ๋ฒ•

1 ) API ํ‚ค ๋ฐœ๊ธ‰ ๋ฐ›๊ธฐ

https://console.cloud.google.com/google/maps-apis/credentials?project=neon-shelter-369206

Google Maps Platform์— ์ ‘์†ํ•˜์—ฌ ์นด๋“œ ์ •๋ณด๋ฅผ ๋“ฑ๋กํ•ด์ค€๋‹ค.
์œ ๋ฃŒ ๊ฒฐ์ œ๋กœ ์—…๋ฐ์ดํŠธ ํ›„์— ์œ ๋ฃŒ ๊ฒฐ์ œ๊ฐ€ ์ด๋ค„์ง€๋ฏ€๋กœ ์•ˆ์‹ฌํ•˜๊ณ  ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค๊ณ  ํ•œ๋‹ค.

์นด๋“œ ์ •๋ณด๋ฅผ ์ž‘์„ฑํ•˜์˜€์œผ๋ฉด API ํ‚ค๊ฐ€ ๋ฐœ๊ธ‰๋œ๋‹ค.

API ํ‚ค๋Š” ์‚ฌ์šฉ ๋ฐ ๊ฒฐ์ œ ๋ชฉ์ ์œผ๋กœ ํ”„๋กœ์ ํŠธ์™€ ๊ด€๋ จ๋œ ์š”์ฒญ์„ ์ธ์ฆํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ณ ์œ  ์‹๋ณ„์ž

์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์ค‘์— ์‚ฌ์šฉ์ž ์ธ์ฆ ์ •๋ณด > ํ‚ค ํ‘œ์‹œ ๋กœ ์ ‘์†ํ•˜์—ฌ API ํ‚ค๋ฅผ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
API ํ‚ค๋ฅผ ๊ณต๊ฐœ์ ์œผ๋กœ ๋…ธ์ถœํ•˜๋ฉด ๊ณ„์ •์— ์˜ˆ์ƒ์น˜ ์•Š์€ ๋น„์šฉ์ด ๋ถ€๊ณผ๋  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค.

2 ) Google Maps API ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

<script async
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>

3 ) ์ฝ”๋“œ react hooks๋กœ ์ˆ˜์ •ํ•˜๊ธฐ

import React, { useCallback, useEffect, useRef } from "react";

function Home() {
  const mapRef = useRef(null);

  const initMap = useCallback(() => {
    new window.google.maps.Map(mapRef.current, {
      center: { lat: -34.397, lng: 150.644 },
      zoom: 8,
    });
  }, [mapRef]);

  useEffect(() => {
    initMap();
  }, [initMap]);

  return (
    <div
      className="map"
      style={{ width: "500px", height: "500px" }}
      ref={mapRef}
    ></div>
  );
}

export default Home;

๊ฒฐ๊ณผ ํ™”๋ฉด

์œ„์น˜๊ฐ€ ์–ผ์ถ” ๋น„์Šทํ•˜๊ฒŒ ๋‚˜์˜จ๋‹ค.

๋ฆฌ์•กํŠธ์—๋Š” GoogleMap ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ง€์›ํ•˜๋ฏ€๋กœ, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.


Google Map ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ

https://react-google-maps-api-docs.netlify.app/
๊ตฌ๊ธ€ ๋งต api ๊ณต์‹ ๋ฌธ์„œ

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ์„ ์œ„ํ•ด ์„ค์น˜ํ•ด์ฃผ์ž.

npm install --save @react-google-maps/api

์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด package-lock.json ํŒŒ์ผ์„ ํ†ตํ•ด ์ •์ƒ์ ์œผ๋กœ ์„ค์น˜๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ณต์‹ ๋ฌธ์„œ ์˜ˆ์ œ์—์„œ ํ‘œ์‹œํ•  ์œ„๋„, ๊ฒฝ๋„ ๊ฐ’๋งŒ ๋ฐ”๊ฟ”์ฃผ์—ˆ๋”๋‹ˆ ์ž˜ ์ถœ๋ ฅ๋œ๋‹ค.

import React from 'react'
import { GoogleMap, LoadScript } from '@react-google-maps/api';

const containerStyle = {
  width: '700px',
  height: '400px'
};

const center = {
  lat: 14.018000,
  lng: 120.835941
};

function MyComponent() {
  return (
    <LoadScript
      googleMapsApiKey="YOUR_API_KEY"
    >
      <GoogleMap
        mapContainerStyle={containerStyle}
        center={center}
        zoom={14}
      >
        <></>
      </GoogleMap>
    </LoadScript>
  )
}

export default React.memo(MyComponent)

Google Map Marker ์‚ฌ์šฉํ•˜๊ธฐ

์ง€๋„์—์„œ ์›ํ•˜๋Š” ์œ„์น˜๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Marker๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
๋ฆฌ์•กํŠธ ๋ฒ„์ „ 18.0 ์ดํ›„์—๋Š” MarkerF ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

GoogleMap ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์— MarkerF ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค.
์™ธ๋ถ€์— ์ž‘์„ฑํ•˜๋ฉด ๋งˆ์ปค ํ‘œ์‹œ๊ฐ€ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๋Š”๋‹ค.

๋งˆ์ปค ์ด๋ฏธ์ง€๋Š” ๊ตฌ๊ธ€๋งต ํ”Œ๋žซํผ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

import { GoogleMap, LoadScript, MarkerF } from '@react-google-maps/api';

...
<GoogleMap ... 
<MarkerF position={center} icon={"https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png"}></MarkerF>
</GoogleMap>

๊ฒฐ๊ณผ ํ™”๋ฉด


์ฐธ๊ณ 
https://cotist.tistory.com/5
์ด ๋ถ„์˜ ๋ธ”๋กœ๊ทธ์—์„œ ๋„์›€์„ ๋งŽ์ด ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค

https://developers.google.com/maps/premium/apikey/directions-apikey?hl=ko
google maps ๊ณต์‹ ๋ฌธ์„œ

profile
๊ธฐ๋ก์„ ์ •๋ฆฌํ•˜๋Š” ๊ณต๊ฐ„!

0๊ฐœ์˜ ๋Œ“๊ธ€