node.js

๊ณต๊ณต API ์‚ฌ์šฉํ•˜์—ฌ ์›นํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ -2

dev_summer 2022. 10. 10. 17:25

์ด ๋ธ”๋กœ๊ทธ๋Š” ๋‚ด ๋งŒ์กฑ์šฉ ๋ธ”๋กœ๊ธฐ์ด๊ธด ํ•˜์ง€๋งŒ ๊ฐ€๋” ์ธ๊ธฐ๊ธ€ ์ˆœ์œ„๋ฅผ ๋ณธ๋‹ค.

๊ณต๋ถ€ํ• ๋•Œ ๊ธฐ๋ก์šฉ์œผ๋กœ ์ž‘์„ฑํ•˜์˜€๋˜ ๊ณต๊ณตapi๋ฅผ ์‚ฌ์šฉํ•œ ์›นํŽ˜์ด์ง€๋งŒ๋“ค๊ธฐ๋ฅผ ๊พธ์ค€ํžˆ ๋งŽ์ด ๋ณด๋Š”๊ฒƒ ๊ฐ™๋‹ค.

๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ๋ฅผ ํ–ˆ์„๋•Œ์—๋„ ๋Œ“๊ธ€์ด ๋งŽ์ด ๋‹ฌ๋ ค์„œ

๊ณต๊ณตapi๋ฅผ ์ด์šฉํ•˜๋ ค๋Š” ํ•™์ƒ๋ถ„๋“ค์ด ๋งŽ๊ตฌ๋‚˜ ์‹ถ์—ˆ๋‹ค.

 

์ด๋ฒˆ์— ํ•™๊ต์—์„œ ๊ฐ„๋‹จํ•œ ๋ฐ์ดํ„ฐํ™œ์šฉ ์›นํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋ผ๊ณ  ํ•ด์„œ ๊ณต๊ณตapi๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง€๋„์— ํ‘œ์‹œํ•˜๋Š” ์›นํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด๋ดค๋Š”๋ฐ

ํ•œ๋•Œ ์—ฌ๋Ÿฌ ๋ธ”๋กœ๊ทธ๋“ค์„ ๋ณด๋ฉด์„œ ๊ณต๊ณต api ํ™œ์šฉํ•˜๋ ค๊ณ  ํ–ˆ๋˜ ๋‚˜๋ฅผ ์ƒ๊ฐํ•˜๋ฉฐ ์ด๋ฒˆ์—” ์ข€ ์ž์„ธํžˆ ํ’€์–ด๋ณด์•„์•ผ๊ฒ ๋‹ค.

3๋…„์ „์˜ ๋‚˜๋ฅผ ๊ฒฉ๋ คํ•˜๋Š” ๊ธ€์ด๋ผ๊ณ  ๋ณด๋Š”๊ฒŒ ์ข‹๊ฒ ๋‹ค.

 

๊ณต๊ณต ์˜คํ”ˆ api๋Š” ์‹ ์ž… ๊ฐœ๋ฐœ์ž๊ฐ€ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ๋งŒ๋“ค๋•Œ ์ฐธ ํ™œ์šฉํ•˜๊ธฐ ์ข‹์€ ๊ฒƒ ๊ฐ™๋‹ค.

ํ•œ์ฐฝ ์ฝ”๋กœ๋‚˜๊ฐ€ ์‹œ์ž‘๋ ๋•Œ์ฏค์ด์—ฌ์„œ ๋ฉด์ ‘๋„ ์•ˆ์žกํžˆ๊ณ  ์•„๋ฌด๊ฒƒ๋„ ์•ˆํ•˜๊ธฐ์—” ๋„ˆ๋ฌด๋‚˜๋„ ๋ถˆ์•ˆํ•ด์„œ ๋งˆ์Šคํฌ๋ฅผ ์‚ด์ˆ˜์žˆ๋Š” ์•ฝ๊ตญ ์ •๋ณด๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ›์•„ ํ”„๋กœ์ ํŠธ๋กœ ํ™œ์šฉ ํ–ˆ์—ˆ๋Š”๋ฐ ์ด๋ฒˆ์—๋Š” ๊ทธ์™€ ์กฐ๊ธˆ์€ ๋น„์Šทํ•œ ์ „๋™ํœ ์ฒด์–ด๊ธ‰์†์ถฉ์ „๊ธฐ๊ฐ€ ์žˆ๋Š” ๊ณณ์˜ ์ •๋ณด๋ฅผ ์ง€๋„๋กœ ํ‘œํ˜„ํ•  ์ˆ˜์žˆ๋Š” ๊ฐ„๋‹จํ•œ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ๋‹ค.

 

ํ™œ์šฉํ•  ๊ณต๊ณต api 

https://data.seoul.go.kr/dataList/OA-15813/S/1/datasetView.do?tab=A 

 

์—ด๋ฆฐ๋ฐ์ดํ„ฐ๊ด‘์žฅ ๋ฉ”์ธ

๋ฐ์ดํ„ฐ๋ถ„๋ฅ˜,๋ฐ์ดํ„ฐ๊ฒ€์ƒ‰,๋ฐ์ดํ„ฐํ™œ์šฉ

data.seoul.go.kr

๋‹ค๋ฅธ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋“ค๋„ ํ™œ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์นด์นด๋„ ์ง€๋„ api

https://apis.map.kakao.com/web/

 

 

1. ์ง€๋„ ์ƒ์„ฑ

 ์นด์นด์˜ค ์ƒ˜ํ”Œ๊ฐ€์ด๋“œ๋Š” ์ฐธ ์นœ์ ˆํ•˜๋‹ค.

 ์นด์นด์˜ค์ง€๋„ ์ƒ˜ํ”Œ์—์„œ ์ง€๋„ ์ƒ์„ฑํ•˜๊ธฐ๋ฅผ ๊ทธ๋ƒฅ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐํ–ˆ๋‹ค.

<script type="text/javascript" src="//dapi.kakao.com/v2/maps/sdk.js?appkey=๋ฐœ๊ธ‰๋ฐ›์€ APP KEY"></script>

<body>
	<div id="map" style="width:100%;height:350px;"></div>
</body>

<script>
	// id๊ฐ€ map์ธ ๊ฒƒ์„ ์ฐพ์•„ mapContainer๋กœ ๋ฐ›์Œ
    let mapContainer = document.getElementById('map'),
	
    // ์ตœ์ดˆ ์ง–๋„์˜ ์ค‘์‹ฌ์ขŒํ‘œ, ๋ ˆ๋ฒจ ๋“ฑ์„ ์ง€์ •
    mapOption = { 
        center: new kakao.maps.LatLng(33.450701, 126.570667),
        level: 3
    };
    
	// ์นด์นด์˜ค ๋งต์„ ๋„์›€
    let map = new kakao.maps.Map(mapContainer, mapOption); 
</script>

*์นด์นด์˜ค ์ง€๋„๋ฅผ ๋„์šฐ๊ธฐ ์œ„ํ•ด์„œ๋Š” web์šฉ app key๋ฅผ ๋ฐœ๊ธ‰ ๋ฐ›์•„์•ผํ•œ๋‹ค. ๋˜ํ•œ url์„ ์—ด์–ด์ฃผ์–ด์•ผํ•œ๋‹ค. 

 

2. ์œ„๋„ ๊ฒฝ๋„ ๋ฐ›๊ธฐ

 ๋‚ด๊ฐ€ ํ™œ์šฉํ•˜๊ณ ์‹ถ์€ ์ •๋ณด๊ฐ€ ์ ์ ˆํžˆ ์ž˜ ๋„˜์–ด์˜ค๋Š”์ง€ ํ™•์ธํ•ด๋ณธ๋‹ค.

ajax๋กœ ํ†ต์‹ ํ• ์ˆ˜๋„์žˆ์ง€๋งŒ ๋‚˜๋Š” ์„œ๋ฒ„๋‹จ์—์„œ axios๋กœ ํ†ต์‹ ํ•˜์—ฌ ์ •๋ณด๋ฅผ ๋ฐ›์•„์™€ ์ถœ๋ ฅ ํ•  ์˜ˆ์ •์ด๋‹ค.

let url = `http://openapi.seoul.go.kr:8088/${process.env.SECRET_KEY}/json/tbElecWheelChrCharge/1/1000`;
const data = await axios.get(url, {headers: {"Content-type": "application/x-www-form-urlencoded; charset=UTF-8", Accept: "*/*"}});

* ๊ณต๊ณต api ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ secret key๋ฅผ ๋ฐ›์•„ ์š”์ฒญ์‹œ๋งˆ๋‹ค ์ด ํ‚ค๋ฅผ ํ•จ๊ป˜ ๋ณด๋‚ด์ฃผ์–ด์•ผํ•œ๋‹ค.

 

3. ์ง€๋„์— ๋งˆ์ปค์ฐ๊ธฐ + ๋งˆ์ปค์— ํด๋ฆญ ์ด๋ฒคํŠธ ๋‹ฌ๊ธฐ

 

 ์นด์นด์˜ค์— ๋งˆ์ปค์ƒ์„ฑํ•˜๊ธฐ ๋ถ€๋ถ„์„ ์ฐธ๊ณ ํ•˜์—ฌ ์„œ๋ฒ„์—์„œ ๋„˜์–ด์˜จ n๊ฐœ์˜ ์œ„๋„์™€ ๊ฒฝ๋„์ •๋ณด๋ฅผ ํ†ตํ•ด for๋ฅผ ๋Œ๋ฉด์„œ ๋งˆ์ปค๋ฅผ ์ฐ์–ด์ค€๋‹ค.

์นด์นด์˜ค์— ๋งˆ์ปค์— ํด๋ฆญ ์ด๋ฒคํŠธ ๋“ฑ๋กํ•˜๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํด๋ฆญ์ด๋ฒคํŠธ๋„ ํ•จ๊ป˜ ๋‹ฌ์•„์ค€๋‹ค.

let positions = [
    <% for(let i=0; i<list.length; i++){ %>
    {
      title: '<%= list[i].FCLTYNM %>',
      latlng: new kakao.maps.LatLng(<%= list[i].LATITUDE %>, <%= list[i].LONGITUDE %>),
    }<% if(i<list.length-1){%>,<%}%>
    <% } %>
  ];

  var imageSrc = "https://t1.daumcdn.net/localimg/localimages/07/mapapidoc/markerStar.png";

  for (let i = 0; i < positions.length; i ++) {
    let imageSize = new kakao.maps.Size(24, 35);
    let markerImage = new kakao.maps.MarkerImage(imageSrc, imageSize);
    let marker = new kakao.maps.Marker({
      map: map,
      position: positions[i].latlng,
      title : positions[i].title,
      image : markerImage,
      // clickable๋ฅผ true๋กœ ํ•˜๋ฉด ํด๋ฆญ์ด๋ฒคํŠธ๋ฅผ ๋‹ฌ ์ˆ˜ ์žˆ๋‹ค. 
      clickable:true
    });

    kakao.maps.event.addListener(marker, 'click', function() {
      document.getElementById("title").innerText = positions[i].title;
    });
  }

 

 

ejs ๋ฌธ๋ฒ•์ด๋ž‘ ์„ž์—ฌ์žˆ์–ด์„œ ์กฐ๊ธˆ ํ—ท๊ฐˆ๋ฆฌ๊ธด ํ•˜์ง€๋งŒ, ์„œ๋ฒ„์—์„œ ๊ฐ€์ ธ์˜จ ์ด๋ฆ„, ์œ„๋„, ๊ฒฝ๋„ ์ •๋ณด๋ฅผ for๋ฌธ์„ ๋Œ๋ฉด์„œ ๋งˆ์ปค๋ฅผ ์ฐ์–ด์ค€ ๊ฒƒ์ด๊ณ 

ํด๋ฆญ์ด๋ฒคํŠธ๋„ ์ƒ์„ฑํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

 

 

์ธ๊ฐ• ๋“ฃ๋‹ค๊ฐ€ ๊ณต๋ถ€ํ•˜๊ธฐ์‹ซ์–ด์„œ ์ ์€ ๋ป˜๊ธ€ ๋