๐Ÿ’ป

JavaScript ์˜ ์ดํ•ด - [AJAX] Kakao ์ง€๋„ Javscript API ์ด์šฉํ•ด์„œ ์ง€๋„๊ทธ๋ฆฌ๊ธฐ ๋ณธ๋ฌธ

KITRI/JAVASCRIPT

JavaScript ์˜ ์ดํ•ด - [AJAX] Kakao ์ง€๋„ Javscript API ์ด์šฉํ•ด์„œ ์ง€๋„๊ทธ๋ฆฌ๊ธฐ

๋˜ํšจ๋‹ˆ 2020. 7. 20. 13:55

kakao developers ์‚ฌ์ดํŠธ(https://developers.kakao.com/) ์—์„œ ์นด์นด์˜ค์•„์ด๋””๋กœ ๋กœ๊ทธ์ธ ํ•œ ํ›„ ๋‚ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

 

Kakao Developers

์นด์นด์˜ค API๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•ด๋ณด์„ธ์š”. ์นด์นด์˜ค ๋กœ๊ทธ์ธ, ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ, ์นœ๊ตฌ API, ์ธ๊ณต์ง€๋Šฅ API ๋“ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

developers.kakao.com

์•ฑ์ด๋ฆ„๊ณผ ํšŒ์‚ฌ๋ช…์„ ์ ๊ณ , ํ”Œ๋žซํผ์œผ๋กœ Web๋กœ์ปฌ์ฃผ์†Œ(ex. http://localhost:8181)๋กœ ์„ค์ •ํ•ด์ฃผ๊ณ , ์•ฑ ํ‚ค(๋„ค์ดํ‹ฐ๋ธŒ ์•ฑ ํ‚ค, REST API ํ‚ค, JavaScript ํ‚ค, Admin ํ‚ค)๊ฐ€ ์„ค์ •๋œ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

ํ‚ค ๊ฐ’ ์ •๋ณด ๋’ท๋ถ€๋ถ„์€ ์ง€์› ์Œ

 

์ด๋ ‡๊ฒŒ ์ƒ์„ฑํ•œ ํ‚ค๋Š” API๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํ•„์š”ํ•˜๋‹ค.

 


 

ํ”„๋กœ์ ํŠธ ํด๋” ์ƒ์„ฑํ•œ ํŒŒ์ผ๋“ค

 

์ง€๋„ ๊ทธ๋ ค๋ณด๊ธฐ(์—ฐ์Šต)

 

 

https://apis.map.kakao.com/web/guide/#step2

 

ajax.jsp

์‹œ์ž‘ํŽ˜์ด์ง€

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<c:set var="root" value="${pageContext.request.contextPath}"/>
	
	<h3>์‚ฌ์› ํ…Œ์ด๋ธ”</h3>
	<a href="${root}/sawon/list.do">์‚ฌ์›๋ชฉ๋ก</a>
	<br /><br />
	
	<h3>์‹ค์‹œ๊ฐ„ ๋Œ“๊ธ€</h3>
	<a href="${root}/reply/replyList.do">ํ•œ์ค„๋‹ต๊ธ€์‹œ์ž‘</a>
	<br /><br />
	
	<h3>๊ธฐ์ƒ์ฒญ ๋ฐ์ดํ„ฐ ํŒŒ์‹ฑ</h3>
	<a href="${root}/proxy/parsing.do">ํŒŒ์‹ฑ๋ฐ์ดํ„ฐ</a>
	<br /><br />
	
	<h3>Kakao Map</h3>
	<a href="${root}/map.do">์นด์นด์˜ค๋งต</a>
</body>
</html>

 

commandURL.propertis

/map.do=com.java.parsing.command.MapCommand

 

[com.java.parsing.command] MapCommand.java

package com.java.parsing.command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.java.command.Command;

public class MapCommand implements Command {

    @Override
    public String proRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
	// TODO Auto-generated method stub
	return "/WEB-INF/views/ajax/map/kakaoMap.jsp";
    }
}

 

kakaoMap.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>kakaoMap</title>
<!--REST KEY: 4db15a5d8b21a6f3702bd43929f5f96c -->
<script type="text/javascript" src="//dapi.kakao.com/v2/maps/sdk.js?appkey=20002404caeec65787ea2d0c19935431"></script>
</head>
<body>
	<div><h3>์นด์นด์˜ค ์ง€๋„</h3></div>
	<div id="map" style="width:500px;height:400px;"></div>
	
	<script>
		var container = document.getElementById('map'); //์ง€๋„๋ฅผ ๋‹ด์„ ์˜์—ญ์˜ DOM ๋ ˆํผ๋Ÿฐ์Šค
		var options = { //์ง€๋„๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ•„์š”ํ•œ ๊ธฐ๋ณธ ์˜ต์…˜
			center: new kakao.maps.LatLng(33.450701, 126.570667), //์ง€๋„์˜ ์ค‘์‹ฌ์ขŒํ‘œ.
			level: 3 //์ง€๋„์˜ ๋ ˆ๋ฒจ(ํ™•๋Œ€, ์ถ•์†Œ ์ •๋„)
		};
	
		var map = new kakao.maps.Map(container, options); //์ง€๋„ ์ƒ์„ฑ ๋ฐ ๊ฐ์ฒด ๋ฆฌํ„ด
	</script>
</body>
</html>

 

 


๊ฒ€์ƒ‰์— ๋”ฐ๋ผ ์ง€๋„ ๊ทธ๋ ค์ฃผ๊ธฐ

 

 

https://developers.kakao.com/docs/latest/ko/local/dev-guide

 

Kakao Developers

์นด์นด์˜ค API๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•ด๋ณด์„ธ์š”. ์นด์นด์˜ค ๋กœ๊ทธ์ธ, ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ, ์นœ๊ตฌ API, ์ธ๊ณต์ง€๋Šฅ API ๋“ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

developers.kakao.com

 

๋ฌธ์„œ > ๋กœ์ปฌ > ๊ฐœ๋ฐœ ๊ฐ€์ด๋“œ ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ URI, ํŒŒ๋ผ๋ฏธํ„ฐ, ํ—ค๋”๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค.

 

xhr.js

/**
 * 
 */
function createXHR(){
	if(window.XMLHttpRequest){ 
		return new XMLHttpRequest();
	}else {
		return new ActiveXObject("Microsoft.XMLHTTP")
	}
}

var xhr = null;
var arr = new Array();
function sendRequest(method, url, params, callback){ //get, xhr.txt, msg, fromServer
	//๋Œ€๋ฌธ์ž, ์†Œ๋ฌธ์ž ๋‹ค ๋Œ€๋ฌธ์ž๋กœ
	var httpMethod = method.toUpperCase();
	var httpUrl = url;
	var httpParams = (params==null||params=="") ? null:params;
	if(httpMethod=="GET" && httpParams!=null){
		httpUrl += "?" + httpParams;
	}
	
	arr.push(httpMethod + ","+ httpUrl + "," + httpParams);
	
	xhr = createXHR();
	xhr.open(httpMethod, httpUrl, true);
	//POST์ผ๋•Œ๋งŒ
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
    //kakao map api๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ํ—ค๋” ์„ค์ •
	xhr.setRequestHeader("Authorization", "KakaoAK /*kakao developers ๋‚ด์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ REST API KEY*/");
	xhr.send(httpMethod=="POST"? httpParams:null);
	xhr.onreadystatechange = callback;
	
}

 

commandURL.properties

/addr.do=com.java.parsing.command.AddrCommand

 

AddrCommand.java

package com.java.parsing.command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.java.command.Command;

public class AddrCommand implements Command {

    @Override
    public String proRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
	// TODO Auto-generated method stub
	return "/WEB-INF/views/ajax/map/address.jsp";
    }
}

 

 

Kakao Maps API > Sample > ์ง€๋„ ์ƒ์„ฑํ•˜๊ธฐ ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ address.jsp ์— ๋„ฃ์–ด์ค€๋‹ค.

 

 

address.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<c:set var="root" value="${pageContext.request.contextPath}"/>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${root}/XHR/xhr.js"></script>
<script type="text/javascript" src="//dapi.kakao.com/v2/maps/sdk.js?appkey=20002404caeec65787ea2d0c19935431"></script>
<script type="text/javascript">
	var arr = new Array();
	
	function toServer(){
		var addr = document.getElementById("addr").value;
		//arr.push(addr);
		
		var url = "https://dapi.kakao.com/v2/local/search/address.json";
		var params = "query=" + addr;
		
		sendRequest("GET", url, params, fromServer);
	}
	
	function fromServer(){
		//arr.push(xhr.readyState+","+ xhr.status);
		if(xhr.readyState==4 && xhr.status==200){
			//arr.push(xhr.responseText);
			processJson();
		}
		//alert(arr);
	}
	function processJson(){
		var obj = JSON.parse(xhr.responseText);
		var y = obj.documents[0].y;
		var x = obj.documents[0].x;
		arr.push(x +","+y);
		alert(arr);
		
		var mapContainer = document.getElementById('map'), // ์ง€๋„๋ฅผ ํ‘œ์‹œํ•  div 
	    mapOption = { 
	        center: new kakao.maps.LatLng(y, x), // ์ง€๋„์˜ ์ค‘์‹ฌ์ขŒํ‘œ
	        level: 5 // ์ง€๋„์˜ ํ™•๋Œ€ ๋ ˆ๋ฒจ
	    };

		// ์ง€๋„๋ฅผ ํ‘œ์‹œํ•  div์™€  ์ง€๋„ ์˜ต์…˜์œผ๋กœ  ์ง€๋„๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค
		var map = new kakao.maps.Map(mapContainer, mapOption); 
	}
	
	
</script>
</head>
<body>
	<h3>์ฃผ์†Œ</h3>
	<input id="addr" type="text" />
	<input type="button" value="์ฃผ์†Œ๊ฒ€์ƒ‰" onclick="toServer()"/>
	
	<div id="map" style="width:100%;height:400px;"></div>
</body>
</html>

 

 

๋ฐ˜์‘ํ˜•
Comments