Post List

태그

2019년 1월 24일 목요일

카카오 지도 api 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
 
import javax.net.ssl.HttpsURLConnection;
public class KakaoGeoApi1 {
 
    public static void main(String[] args) throws Exception {
         
     
     System.out.println(addrToCoord(URLEncoder.encode("주소입력","UTF-8")));
     System.out.println(coordToAddr("37.5665958","126.9783813"));
    }
     
    public static String addrToCoord(String addr){
      
        String json = "";
        try{
            json = getJSONData(url);
        }catch(Exception e){
             
            e.printStackTrace();
        }
        return json;
    }
 
    public static String coordToAddr(String x, String y){
      
     String url = "https://dapi.kakao.com/v2/local/geo/coord2address.json?x="+x+"&y="+y+"&input_coord=WGS84";
        String json = "";
        try{
            json = getJSONData(url);
        }catch(Exception e){
             
            e.printStackTrace();
        }
        return json;
    }
     
     
    private static String getJSONData(String apiUrl) throws Exception {
        String jsonString = new String();
        String buf;
        String apikey = "apikey"; //apikey
         
        URL url = new URL(apiUrl);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        String auth = "KakaoAK "+apikey;
        conn.setRequestMethod("GET");
        conn.setRequestProperty("X-Requested-With", "curl");
        conn.setRequestProperty("Authorization", auth);
         
        BufferedReader br = new BufferedReader(new InputStreamReader(
                conn.getInputStream(), "UTF-8"));
        while ((buf = br.readLine()) != null) {
            jsonString += buf;
        }
        return jsonString;
    }
}

댓글 없음:

댓글 쓰기