๐ป
JavaScript ์ ์ดํด - [AJAX] Kakao ์ง๋ Javscript API ์ด์ฉํด์ ์ง๋๊ทธ๋ฆฌ๊ธฐ ๋ณธ๋ฌธ
JavaScript ์ ์ดํด - [AJAX] Kakao ์ง๋ Javscript API ์ด์ฉํด์ ์ง๋๊ทธ๋ฆฌ๊ธฐ
๋ํจ๋ 2020. 7. 20. 13:55kakao developers ์ฌ์ดํธ(https://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
๋ฌธ์ > ๋ก์ปฌ > ๊ฐ๋ฐ ๊ฐ์ด๋ ๋ฅผ ์ฐธ๊ณ ํ์ฌ 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>
'KITRI > JAVASCRIPT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JavaScript ์ ์ดํด - [jQuery] Selector(2) : ํํฐ (0) | 2020.07.21 |
---|---|
JavaScript ์ ์ดํด - [jQuery] Selector(1) : ๊ธฐ๋ณธ, ์์(์์/ํ์ ) (0) | 2020.07.20 |
JavaScript ์ ์ดํด - [AJAX] ๊ธฐ์์ฒญ ๋ฐ์ดํฐ Parsing (0) | 2020.07.20 |
JavaScript ์ ์ดํด - [AJAX] XMLHttpRequest.responseXML (0) | 2020.07.16 |
JavaScript ์ ์ดํด - [AJAX] ํ์ค ๋ต๊ธ ๋ฌ๊ธฐ(CRUD) (0) | 2020.07.16 |