๊ณต๋ถ€ ๊ธฐ๋ก โœ๏ธ/Javascript

JS-Do it! JS - #12์ผ (17,18)

๋ฒ„๋‘˜ 2023. 4. 3. 15:21

17. ์›น API ํ™œ์šฉํ•˜๊ธฐ

 

API (Application Programming Interfase)

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ๋•Œ ํ•˜๋‚˜์˜ ํ”„๋กœ๊ทธ๋žจ๊ณผ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ ์‚ฌ์ด์— ์‰ฝ๊ฒŒ ์ •๋ณด๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค

 

์ฟ ํ‚ค์™€ ์›น ์Šคํ† ๋ฆฌ์ง€

์ฟ ํ‚ค

์‚ฌ์šฉ์ž๊ฐ€ ์›น ์‚ฌ์ดํŠธ์— ์ ‘์†ํ•ด์„œ ์›น ์‚ฌ์ดํŠธ๋ฅผ ์„œํ•‘ํ•˜๋Š” ๋™์•ˆ ์‚ฌ์šฉ์ž ์ปดํ“จํ„ฐ์— ์ €์žฅ๋˜๋Š” ํ…์ŠคํŠธ ํŒŒ์ผ

์›น ์‚ฌ์ดํŠธ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ ‘์†ํ–ˆ๋˜ ๊ฐœ์ธ์˜ ์ •๋ณด๊ฐ€ ์ €์žฅ->์‚ฌ์ƒํ™œ ์นจํ•ด ์šฐ๋ ค,๋ณด์•ˆ๋ฌธ์ œ

์›น ์Šคํ† ๋ฆฌ์ง€

์›น์‚ฌ์ดํŠธ์™€ ๊ด€๋ จ๋œ ์ •๋ณด๋ฅผ ์ €์žฅ

์‚ฌ์šฉ์ž๊ฐ€ ์ผ๋ถ€๋Ÿฌ ์Šคํ† ๋ฆฌ์ง€ ์ •๋ณด๋ฅผ ์„œ๋ฒ„๋กœ ์ „์†กํ•˜์ง€ ์•Š๋Š” ์ด์ƒ 

์„œ๋ฒ•์—์„œ ์‚ฌ์šฉ์ž PC๋กœ ๋“ค์–ด์™€ ์Šคํ† ๋ฆฌ์ง€ ์ •๋ณด๋ฅผ ์ฝ์–ด๊ฐ€์ง€ ๋ชปํ•œ๋‹ค

- ๊ตฌ์„ฑ

์„ธ์…˜ ์Šคํ† ๋ฆฌ์ง€: ์›น ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์—ฌ๋Š” ์ˆœ๊ฐ„๋ถ€ํ„ฐ ๋‹ซ์„๋•Œ๊นŒ์ง€๋ฅผ ํ•˜๋‚˜์˜ ์„ธ์…˜์ด๋ผ๊ณ  ํ•œ๋‹ค, ์ฐฝ์ด ๋‹ซํžˆ๋ฉด ๋ฐ์ดํ„ฐ ์‚ญ์ œ

๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€: ์„ธ์…˜์ด ๋๋‚˜๋„ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ด€ํ•˜๋Š” ์Šคํ† ๋ฆฌ์ง€, ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์‚ญ์ œํ•˜๊ธฐ ์ „๊นŒ์ง€ ๋ณด๊ด€

 

.parentElement()

ํ•ด๋‹น ๋…ธ๋“œ์˜ ๋ถ€๋ชจ ์š”์†Œ(Element Object)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๋ถ€๋ชจ ์š”์†Œ๊ฐ€ ์—†๊ฑฐ๋‚˜ ๋ถ€๋ชจ๊ฐ€ DOM ์š”์†Œ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” null์„ ๊ฐ–๋Š”๋‹ค.

 

.children()

์–ด๋–ค ์š”์†Œ์˜ ์ž์‹ ์š”์†Œ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

 

 

geolocation ๊ฐ์ฒด

๊ฐ„๋‹จํ•œ ์œ„์น˜๋ฅผ ์•Œ์•„๋‚ด๊ณ  ์›€์ง์ด๋Š” ์‚ฌ์šฉ์ž์˜ ์œ„์น˜๊นŒ์ง€ ์ถ”์ฒ™ ๊ฐ€๋Šฅ

window.navigator๊ฐ์ฒด์˜ ์ž์‹ ๊ฐ์ฒด์ธ geolocation ๊ฐ์ฒด์— ํฌํ•จ๋˜์–ด ์žˆ๋‹ค

 

- ์‚ฌ์šฉ์ž์˜ ํ˜„์žฌ ์œ„์น˜ ํ™•์ธ getCurrentPosition(์„ฑ๊ณต์‹œ์ฝœ๋ฐฑํ•จ์ˆ˜(,์‹คํŒจ์‹œ ์ฝœ๋ฐฑํ•จ์ˆ˜, ์˜ต์…˜))

-์ง€์ •ํ•œ ์‹œ๊ฐ„๋งˆ๋‹ค ์œ„์น˜ ํ™•์ธ watchPosition(์„ฑ๊ณต์‹œ์ฝœ๋ฐฑํ•จ์ˆ˜(,์‹คํŒจ์‹œ ์ฝœ๋ฐฑํ•จ์ˆ˜,์˜ต์…˜)) + clearWatch(id)

๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€์— ์ €์žฅํ•˜์—ฌ TodoList ๋งŒ๋“ค๊ธฐ

const todoInput=document.querySelector("#todo-input");
const addBtn=document.querySelector("#add-button");
const todoList=document.querySelector("#todo-list");

document.addEventListener("DOMContentLoaded",getLocal);
addBtn.addEventListener("click",addTodo);
todoList.addEventListener("click",manageTodo);

function addTodo(e){
    e.preventDefault();
    const newDiv=document.createElement("div");
    newDiv.classList.add("todo");

    const newTodo=document.createElement("li");
    newTodo.innerText=todoInput.value;
    newTodo.classList.add("todo-content");
    newDiv.appendChild(newTodo);

    saveToLocal(todoInput.value);

    const completeBtn=document.createElement("button");
    completeBtn.innerText="์™„๋ฃŒ";
    completeBtn.classList.add('complete-btn');
    newDiv.appendChild(completeBtn);

    const deleteBtn=document.createElement("button");
    deleteBtn.innerText="์‚ญ์ œ";
    deleteBtn.classList.add("delete-btn");
    newDiv.appendChild(deleteBtn);

    todoList.appendChild(newDiv);
    todoInput.value="";
}

function saveToLocal(todo){
    let todos;
    if(localStorage.getItem("todos")===null){
        todos=[];
    }else{
        todos=JSON.parse(localStorage.getItem("todos"));
    }
    todos.push(todo);
    localStorage.setItem("todos",JSON.stringify(todos));

}
function getLocal(){
    let todos;
    if(localStorage.getItem("todos")===null){
        todos=[];
    }else{
        todos=JSON.parse(localStorage.getItem("todos"));
    }
    todos.forEach(function(todo){
        const newDiv=document.createElement("div");
        newDiv.classList.add("todo");
        const newTodo=document.createElement("li");
        newTodo.innerText=todo;
        newTodo.classList.add("todo-content");
        newDiv.appendChild(newTodo);

        const completeBtn=document.createElement("button");
        completeBtn.innerText="์™„๋ฃŒ";
        completeBtn.classList.add("complete-btn");
        newDiv.appendChild(completeBtn);

        const deleteBtn=document.createElement("button");
        deleteBtn.innerText="์‚ญ์ œ";
        deleteBtn.classList.add("delete-btn");
        newDiv.appendChild(deleteBtn);

        todoList.appendChild(newDiv);

        todoInput.value="";
    });

}

function manageTodo(e){
    const whichButton=e.target.classList[0];
    if(whichButton ==='complete-btn'){
        const todo=e.target.parentElement;
        todo.children[0].toggle('completed');
    }else if(whichButton ==='delete-btn'){
        const todo=e.target.parentElement;
        removeLocal(todo);
        todo.remove();
    }


    function removeLocal(todo){
        let todos;
        if(localStorage.getItem("todos")===null){
            todos=[];
        }else{
            todos=JSON.parse(localStorage.getItem("todos"));
        }
        const index=todos.indexOf(todo.children[0].innerText);
        //console.log(index);
        todos.splice(index,1);
        localStorage.setItem("todos",JSON.stringify(todos));
    }
}

watchPosition ์„ ์ด์šฉํ•ด ์œ„๋„,๊ฒฝ๋„

const geoBtn=document.querySelector("#getLocation");
const result=document.querySelector("#result");

geoBtn.addEventListener("click",(e)=>{
    e.preventDefault();

    if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(showPosition,errorPosition);
    
        const options={
            enableHighAccuracu:true,
            timeout:5000,
            maximumAge:0
        };

        let watchId=navigator.geolocation.watchPosition(showPosition,errorPosition,options);

        setTimeout(function(){
            navigator.geolocation.clearWatch(watchId);
        },30000)
    
    }else{

        alert("์ง€์˜ค๋กœ์ผ€์ด์…˜์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
    }
});

function showPosition(position){
    result.innerHTML=`
    <b>์œ„๋„ : </b> ${position.coords.latitude},
    <b>๊ฒฝ๋„ : </b> ${position.coords.longitude}`;
};

function errorPosition(err){
    alert(err.message);
}

 


18.๊ณต๊ฐœ API ํ™œ์šฉํ•˜๊ธฐ

 

 

๊ณต๊ณต๋ฐ์ดํ„ฐ์˜ ์ข…๋ฅ˜

ํŒŒ์ผ๋ฐ์ดํ„ฐ

ํŒŒ์ผํ˜•ํƒœ๋กœ ์ œ๊ณต, ๋กœ๊ทธ์ธํ•˜์ง€ ์•Š์•„๋„ ๋‹ค์šด๋กœ๋“œํ•ด์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

์˜คํ”ˆ API

ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ์ค€ํ™”ํ•˜๊ณ  ๊ณต์œ ํ•˜๋Š” ํ˜•ํƒœ,์‹ค์‹œ๊ฐ„ ์ •๋ณด ์ˆ˜์ •๊ฐ€๋Šฅ

ํ•ญ์ƒ ์ตœ์‹  ์ •๋ณด ์œ ์ง€

ํ‘œ์ค€๋ฐ์ดํ„ฐ

๊ณต๊ณต๊ธฐ๊ด€๋งˆ๋‹ค ์„œ๋กœ ๋‹ค๋ฅธ ํ˜•์‹์œผ๋กœ ์ œ๊ณตํ•˜๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๊ฐ€์ง€ ํ‘œ์ค€์œผ๋กœ ํ†ต์ผํ•˜์—ฌ ์ œ๊ณต

 

 

์บ ํ•‘์žฅ ๋งˆ์ปค ํด๋Ÿฌ์Šคํ„ฐ

 <script>
    const lat = 37.55684;
    const lng = 126.91404;

    var mapContainer = document.getElementById('map'), // ์ง€๋„๋ฅผ ํ‘œ์‹œํ•  div 
        mapOption = { 
            center: new kakao.maps.LatLng(lat, lng), // ์ง€๋„์˜ ์ค‘์‹ฌ์ขŒํ‘œ - ์ด์ง€์Šคํผ๋ธ”๋ฆฌ์‹ฑ
            level: 14 // ์ง€๋„์˜ ํ™•๋Œ€ ๋ ˆ๋ฒจ
        };

    // ์ง€๋„๋ฅผ ํ‘œ์‹œํ•  div์™€  ์ง€๋„ ์˜ต์…˜์œผ๋กœ  ์ง€๋„๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค
    var map = new kakao.maps.Map(mapContainer, mapOption); 

    // ๋งˆ์ปค ํด๋Ÿฌ์Šคํ„ฐ๋Ÿฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค 
    var clusterer = new kakao.maps.MarkerClusterer({
      map: map, // ๋งˆ์ปค๋“ค์„ ํด๋Ÿฌ์Šคํ„ฐ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ํ‘œ์‹œํ•  ์ง€๋„ ๊ฐ์ฒด 
      averageCenter: true, // ํด๋Ÿฌ์Šคํ„ฐ์— ํฌํ•จ๋œ ๋งˆ์ปค๋“ค์˜ ํ‰๊ท  ์œ„์น˜๋ฅผ ํด๋Ÿฌ์Šคํ„ฐ ๋งˆ์ปค ์œ„์น˜๋กœ ์„ค์ • 
      minLevel: 10 // ํด๋Ÿฌ์Šคํ„ฐ ํ•  ์ตœ์†Œ ์ง€๋„ ๋ ˆ๋ฒจ 
    });

    // ์„œ๋ฒ„์—์„œ ๊ฐ€์ ธ์˜ค๊ธฐ (๊ด€๊ด‘๊ณต์‚ฌ API)
    const  url = '(์ž‘์„ฑ)';
  
    fetch(url)
    .then(res => res.json())
    .then(json => {
      const data = json.response.body.items.item;

    // ๋งˆ์ปค๋“ค์„ ๋ชจ์•„๋†“์„ ๋ณ€์ˆ˜
    var markers = [];

    for(let i = 0; i < data.length; i++) {  
      // ๋งˆ์ปค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค
      var marker = new kakao.maps.Marker({
        map: map,
        position: new kakao.maps.LatLng(data[i].mapY, data[i].mapX)
      });

      markers.push(marker);   // ๋งˆ์ปค๋ฅผ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค

      var infowindow = new kakao.maps.InfoWindow( {
          content : data[i].facltNm   // ์ธํฌ์œˆ๋„์šฐ์— ํ‘œ์‹œํ•  ๋‚ด์šฉ
      });

      // ๋งˆ์ปค์— ์ด๋ฒคํŠธ๋ฅผ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค
      // ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋กœ๋Š” ํด๋กœ์ €๋ฅผ ๋งŒ๋“ค์–ด ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค 
      // ํด๋กœ์ €๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์ง€ ์•Š์œผ๋ฉด ๋งˆ์ง€๋ง‰ ๋งˆ์ปค์—๋งŒ ์ด๋ฒคํŠธ๊ฐ€ ๋“ฑ๋ก๋ฉ๋‹ˆ๋‹ค

      // ๋งˆ์ปค์— ๋งˆ์šฐ์Šค์˜ค๋ฒ„ํ•˜๋ฉด makeOverListener() ์‹คํ–‰
      kakao.maps.event.addListener(marker, 'mouseover', makeOverListener(map, marker, infowindow));  
      // ๋งˆ์ปค์—์„œ ๋งˆ์šฐ์Šค์•„์›ƒํ•˜๋ฉด makeOutListener() ์‹คํ–‰
      kakao.maps.event.addListener(marker, 'mouseout', makeOutListener(infowindow));   
    }

    // ์ธํฌ์œˆ๋„์šฐ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ํด๋กœ์ €๋ฅผ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค 
    function makeOverListener(map, marker, infowindow) {
        return function() {
            infowindow.open(map, marker);          
        };
    }

    // ์ธํฌ์œˆ๋„์šฐ๋ฅผ ๋‹ซ๋Š” ํด๋กœ์ €๋ฅผ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค 
    function makeOutListener(infowindow) {
        return function() {
            infowindow.close();
        };
    }

    clusterer.addMarkers(markers);
  });  // fetch ๋
  </script>