- ํ๋ก์ ํธ ๊ธฐ๊ฐ : 2024/01 ~ 2024/02
- ํ๋ก์ ํธ ์ธ์ : ํ๋ก ํธ์๋ 5๋ช , ๋ฒก์๋ 9๋ช
- Kakao Map API๋ฅผ ํ์ฉํ์ฌ 'ํค์๋๋ก ์ฅ์ ๊ฒ์ํ๊ณ ๋ชฉ๋ก์ผ๋ก ํ์ถํ๊ธฐ' ๊ธฐ๋ฅ์ ๊ตฌํ
- useState ๋ฅผ ์ฌ์ฉํ์ฌ ๋ง์ง ์ ๋ณด์ ๊ฒ์ ํค์๋๋ฅผ ๊ด๋ฆฌ
- useEffect๋ฅผ ์ด์ฉํ์ฌ ๊ฒ์์ด๊ฐ ๋ณ๊ฒฝ๋ ๋ ๋ง๋ค ์ง๋๋ฅผ ๊ฐฑ์ ํ๊ณ ๋ง์ง ์ ๋ณด ๊ด๋ฆฌ
- ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ์ง๋์ ํ์ํ๊ณ ํ์ด์ง๋ค์ด์ ์ ๊ตฌํ
- ๊ฒ์์ด๋ฅผ ์ ๋ ฅ์ด ๋ฐ์ํ ๋๋ง๋ค handleInputChange ํจ์๋ฅผ ํธ์ถํ์ฌ ๊ฒ์ ํค์๋ ์ ๋ฐ์ดํธ
- JSX๋ฅผ ํตํด UI๋ฅผ ๋ ๋๋ง ํ๊ณ ์คํ์ผ๋ง์ styled-components๋ฅผ ์ด์ฉํ์ฌ ์ ์ฉย ย
0211PJTํ๋ก์ ํธ ๊ฐ์ธ ํด๋ ์์ฑํ์ฌ ๋ฏธ๋ฆฌ ํ ์คํธ ํ ํ๋ก ํธ์๋ ํ๋ก์ ํธ์ ์ฎ๊ธด ํ ๋ค์ ํ ์คํธ ํด๋ณด๋ ์์ ์งํSuperCoding PJT03 Figma
$ npm install typescript @types/node @types/react @types/react-dom @types/jestโพ TypeScript์ ๊ด๋ จ ํ์ ์ ์ ํ์ผ์ ์ค์น
TypeScript๋ก ์์ฑ๋ ์ฝ๋์์ ๊ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ๋ TypeScript ์ปดํ์ผ๋ฌ๊ฐ ํ์ ์ฒดํฌ๋ฅผ ์ํ ๊ฐ๋ฅ
TypeScript ๋ฐ ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํ๋ก์ ํธ์ ์ค์น
ํด๋น ํจํค์ง๋ค์ด ํ๋ก์ ํธ์ node_modules ํด๋์ ์ค์น
$ npx create-react-app mapmain โtemplate typescriptโพ TypeScript๋ฅผ ์ฌ์ฉํ์ฌ React ์ ํ๋ฆฌ์ผ์ด์ ์ ์์ฑ
mapmain์ ์ ํ๋ฆฌ์ผ์ด์ ์ด๋ฆ์ด๋ฉฐ --template typescript๋ TypeScript๋ฅผ ์ฌ์ฉํ์ฌ React ์ ํ๋ฆฌ์ผ์ด์ ์ ์์ฑ
React ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ธฐ๋ณธ ๊ตฌ์กฐ์ TypeScript ๊ตฌ์ฑ์ด ์ค์ ๋ ํ๋ก์ ํธ๊ฐ ์์ฑ
$ npm install --save @types/kakaomaps$ npm i --save-dev @types/styled-componentstypescript์์ ์ค๋ฅ์์ด ์ฌ์ฉํ๊ธฐ ์ํด์๋ styled-component์ type๋ค์ ๊ฐ์ง๊ณ ์์ผํ๋ค.
$ npm install react-icons --saveimport { FaMapLocationDot } from "react-icons/fa6";
...
<FaMapLocationDot />
...
https://react-icons.github.io/react-icons/icons/fa6/
- ๊ธฐ์กด ํ๋ก์ ํธ์์ ์ถ๊ฐ ์ค์น
"@fortawesome/react-fontawesome": "^0.2.0",
"@types/kakaomaps": "^1.1.5",
"react-icon": "^1.0.0",
"react-kakao-maps-sdk": "^1.1.26"import React, { useEffect, useState } from "react";
import styled from "styled-components";
import { FaMapLocationDot } from "react-icons/fa6";
import MapGuide from "./MapGuide";
declare global {
interface Window {
kakao: any;
}
}
interface Pagination {
last: number;
current: number;
gotoPage: (pageNumber: number) => void;
}
interface Place {
x: number;
y: number;
place_name: string;
road_address_name?: string;
// address_name?: string;
phone?: number;
}
// MapContainer ์ปดํฌ๋ํธ ์ ์
const MapContainer: React.FC = () => {
// useState ๋ฅผ ์ฌ์ฉํ์ฌ ๋ง์ง ์ ๋ณด places ์ ๊ฒ์ ํค์๋ searchKeyword ๊ด๋ฆฌ
const [places, setPlaces] = useState<Place[]>([]);
const [searchKeyword, setSearchKeyword] =
useState<string>("๋ง์ง์ ๊ฒ์ํด๋ณด์ธ์");
useEffect(() => {
const infowindow = new window.kakao.maps.InfoWindow({ zIndex: 1 });
// const markers = [];
const mapOption = {
center: new window.kakao.maps.LatLng(37.566826, 126.9786567),
level: 3,
};
const map = new window.kakao.maps.Map(
document.getElementById("map"),
mapOption
);
const ps = new window.kakao.maps.services.Places();
ps.keywordSearch(searchKeyword, placesSearchCB);
function placesSearchCB(data: any[], status: any, pagination: any) {
if (status === window.kakao.maps.services.Status.OK) {
const bounds = new window.kakao.maps.LatLngBounds();
setPlaces(data);
for (let i = 0; i < data.length; i++) {
displayMarker(data[i]);
bounds.extend(new window.kakao.maps.LatLng(data[i].y, data[i].x));
}
map.setBounds(bounds);
displayPagination(pagination);
setPlaces(data);
}
}
function displayPagination(pagination: Pagination): void {
const paginationEl = document.getElementById("pagination");
const fragment = document.createDocumentFragment();
while (paginationEl?.hasChildNodes()) {
paginationEl.removeChild(paginationEl.lastChild!);
}
for (let i = 1; i <= pagination.last; i++) {
const el = document.createElement("a");
el.href = "#";
el.innerHTML = i.toString();
if (i === pagination.current) {
el.className = "on";
} else {
el.onclick = (function (pageNumber: number) {
return function () {
pagination.gotoPage(pageNumber);
};
})(i);
}
fragment.appendChild(el);
}
paginationEl?.appendChild(fragment);
}
function displayMarker(place: Place) {
const marker = new window.kakao.maps.Marker({
map: map,
position: new window.kakao.maps.LatLng(place.y, place.x),
});
window.kakao.maps.event.addListener(marker, "click", function () {
infowindow.setContent(
'<div style="text-decoration:none; padding:5px; text-align:center; font-size:12px; font-weight:bold;">' +
'<a href="https://map.kakao.com/link/map/' +
place.place_name +
"," +
place.y +
"," +
place.x +
'">' +
place.place_name +
"</a>" +
"</div>"
);
infowindow.open(map, marker);
});
}
}, [searchKeyword]);
// ๊ฒ์์ด๊ฐ ๋ณ๊ฒฝ๋ ๋๋ง๋ค handleInputChange ํจ์๋ฅผ ํธ์ถํ์ฌ ์ ์ฉํ๊ธฐ
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchKeyword(e.target.value);
};
return (
<div>
<StyledMapMainContainer>
<StyledMapLocationDot>
<FaMapLocationDot />
</StyledMapLocationDot>
<StyledMapTitle>
<span>๋๊ฐ์ง๋</span>
<MapGuide />
</StyledMapTitle>
<StyledInput
type="text"
value={searchKeyword}
onChange={handleInputChange}
placeholder="๋ง์ง์ ๊ฒ์ํด๋ณด์ธ์"
/>
<StyledMap id="map">
<div></div>
</StyledMap>
<StyledList>
<div id="result-list">
{places.map((place, index) => (
<div key={index} style={{ marginTop: "20px" }}>
{/* <span>{index + 1}</span> */}
<Info>
<p className="place_name">๐ด{place.place_name}</p>
{place.road_address_name ? (
<div>
<p className="place_address">{place.road_address_name}</p>
</div>
) : (
<p>{place.road_address_name}</p>
)}
<p className="place_phone">{place.phone}</p>
</Info>
</div>
))}
</div>
</StyledList>
<StyledPagination>
<div id="pagination"></div>
</StyledPagination>
</StyledMapMainContainer>
</div>
);
};
export default MapContainer;

