차근차근/Spring

naver api 도로명 주소로 위도.경도 얻기

예쁜꽃이피었으면 2016. 9. 30. 14:50


네이버 지도 API 명세

https://developers.naver.com/docs/map/overview



참고블로그


[Spring] 네이버 주소 API로 주소 -> 좌표 변환하기 + 도로명 주소 API  ★★★★★

http://blog.naver.com/PostView.nhn?blogId=platinasnow&logNo=220732491939&redirect=Dlog&widgetTypeCall=true



Converting BufferedReader to JSONObject or Map

http://stackoverflow.com/questions/26358684/converting-bufferedreader-to-jsonobject-or-map


[Java] JSON 파싱

http://javastudy.tistory.com/80


[JAVA] 자바에서 JSON 사용방법

http://sexy.pe.kr/tc/644


JSON 기초 of 기초

http://luckyyowu.tistory.com/303


org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject

http://stackoverflow.com/questions/18440098/org-json-simple-jsonarray-cannot-be-cast-to-org-json-simple-jsonobject


기본적인 JSONObject, JSONArray 사용법

http://biig.tistory.com/52


JSON 배열 만들고 파싱하기

http://newkie.tistory.com/22







쓸수 있는 데이터로 변화하는데 꽤 시간이 오래 걸렸다.. 




Address_road  = 변환에 필요한 도로명 주소


String resultposition = CommonApiUtils.getLatLongInfo(request, Address_road.replace(" ", "").toString());


if(StringUtils.isNotEmpty(resultposition)){

String[] resultpositionArray = resultposition.split(","); 

String sido = resultpositionArray[0];

String sigugun = resultpositionArray[1];

String dongmyun = resultpositionArray[2];

String rest = resultpositionArray[3];

String lon = resultpositionArray[4];

String lat = resultpositionArray[5];

}







public static String getLatLongInfo(HttpServletRequest request, String params) throws Exception{

String sido = "";

    String sigugun = "";

    String dongmyun  = "";

    String rest = "";

String lat = "";  //위도

String lon = "";  //경도

String murl = ""; //연결할 URL

String data = "";


StringBuilder html = new StringBuilder();

murl = "https://openapi.naver.com/v1/map/geocode?query=" + params;


    HttpClient client = HttpClientBuilder.create().build();

    HttpGet requestUrl = new HttpGet(murl);

    requestUrl.addHeader("X-Naver-Client-Id", CLIENTID);  //헤더에 Clinet Id와 Client Secret을 넣습니다

    requestUrl.addHeader("X-Naver-Client-Secret", CLIENTSECRET);

    try {

   

        HttpResponse response = client.execute((HttpUriRequest) requestUrl);

        BufferedReader reader = new BufferedReader(

        new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

        

    System.out.println("====== 검색결과는 잘 받아옴 =========");

        

     

        String current = "";

        while ((current = reader.readLine()) != null) {

        html.append(current);

        }

        reader.close();


        

        JSONParser parser = new JSONParser();

        Object object = parser.parse(html.toString());

        

        //convert Object to JSONObject

        JSONObject jsonObject = (JSONObject)object;

         

        

        JSONObject  result = (JSONObject) jsonObject.get("result");   

        JSONArray items = (JSONArray) result.get("items");    

  

        JSONObject dataObj = (JSONObject) items.get(0);

  

        JSONObject  addrdetail = (JSONObject) dataObj.get("addrdetail");    

        JSONObject  point = (JSONObject) dataObj.get("point");    

 

        sido = (String) addrdetail.get("sido");    

        sigugun = (String) addrdetail.get("sigugun");   

        dongmyun = (String) addrdetail.get("dongmyun");    

        rest = (String) addrdetail.get("rest");    

                 

    lon = (String) point.get("x").toString();    

         lat = (String) point.get("y").toString();    

                

        sido = sido == null ? null : sido;

        sigugun = sigugun == null ? null : sigugun;

        dongmyun = dongmyun == null ? null : dongmyun;

        rest = rest == null ? null : rest;

        lon = lon == null ? null : lon;

        lat = lat == null ? null : lat;

                         

        data = sido.replaceAll(" ", "")+","+sigugun+","+dongmyun.replaceAll(" ", "")+

","+rest.replaceAll(" ", "")+","+lon.replaceAll(" ", "")+","+lat.replaceAll(" ", "");

    

 } catch (ClientProtocolException e) {

  e.printStackTrace();

 } catch (IOException e) {

  e.printStackTrace();

 }

    return data;

}






1

org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject 에러


  소스코드

JSONParser parser = new JSONParser();

Object object = parser.parse(html.toString());

 

 //convert Object to JSONObject

 JSONObject jsonObject = (JSONObject)object;

 

 //Reading the String

//   String result = (String) jsonObject.get("result").toString();    

//   System.out.println("1. "+result);// OK

 

 JSONObject  result = (JSONObject) jsonObject.get("result");    

 System.out.println("1. "+result.toString());

 JSONObject  items = (JSONObject) result.get("items");    <-----------------------이 부분에서

 System.out.println("2. "+items.toString());



해결방법 

http://okky.kr/article/247575


이렇게 수정함 -> 

  JSONParser parser = new JSONParser();

  Object object = parser.parse(html.toString());

        

   //convert Object to JSONObject

   JSONObject jsonObject = (JSONObject)object;

        

   //Reading the String

   //   String result = (String) jsonObject.get("result").toString();    

   //   System.out.println("1. "+result);// OK

        

   JSONObject  result = (JSONObject) jsonObject.get("result");    

   System.out.println("1. "+result.toString());

   JSONArray items = (JSONArray) result.get("items");    

   System.out.println("2. "+items.toString());

    

      


1. {"total":1,"userquery":"충청북도충주시대소원면대학로25","items":[{"point":{"y":36.9651698,"x":127.8709602},"address":"충청북도 충주시 대소원면 대학로  25","addrdetail":{"sigugun":"충주시 대소원면","sido":"충청북도","rest":" 25","dongmyun":"대학로","country":"대한민국"},"isRoadAddress":true}]}


2. [{"point":{"y":36.9651698,"x":127.8709602},"address":"충청북도 충주시 대소원면 대학로  25","addrdetail":{"sigugun":"충주시 대소원면","sido":"충청북도","rest":" 25","dongmyun":"대학로","country":"대한민국"},"isRoadAddress":true}]





2.


JSONObject  result = (JSONObject) jsonObject.get("result");    
System.out.println("1. "+result.toString());
JSONArray items = (JSONArray) result.get("items");    
System.out.println("2. "+items.toString());
        
JSONObject dataObj = (JSONObject) items.get(0);
System.out.println("dataObj : " + dataObj);
//dataObj : {"point":{"y":36.9651698,"x":127.8709602},"address":"충청북도 충주시 대소원면 대학로  25","addrdetail":{"sigugun":"충주시 대소원면","sido":"충청북도","rest":" 25","dongmyun":"대학로","country":"대한민국"},"isRoadAddress":true}

JSONObject  addrdetail = (JSONObject) dataObj.get("addrdetail");    
System.out.println("3. "+addrdetail.toString());
JSONObject  point = (JSONObject) dataObj.get("point");    
System.out.println("4. "+point.toString());

1. {"total":1,"userquery":"충청북도충주시대소원면대학로25","items":[{"point":{"y":36.9651698,"x":127.8709602},"address":"충청북도 충주시 대소원면 대학로  25","addrdetail":{"sigugun":"충주시 대소원면","sido":"충청북도","rest":" 25","dongmyun":"대학로","country":"대한민국"},"isRoadAddress":true}]}

2. [{"point":{"y":36.9651698,"x":127.8709602},"address":"충청북도 충주시 대소원면 대학로  25","addrdetail":{"sigugun":"충주시 대소원면","sido":"충청북도","rest":" 25","dongmyun":"대학로","country":"대한민국"},"isRoadAddress":true}]
dataObj : {"point":{"y":36.9651698,"x":127.8709602},"address":"충청북도 충주시 대소원면 대학로  25","addrdetail":{"sigugun":"충주시 대소원면","sido":"충청북도","rest":" 25","dongmyun":"대학로","country":"대한민국"},"isRoadAddress":true}

3. {"sigugun":"충주시 대소원면","sido":"충청북도","rest":" 25","dongmyun":"대학로","country":"대한민국"}

4. {"y":36.9651698,"x":127.8709602}





3.


JSONParser parser = new JSONParser();

Object object = parser.parse(html.toString());

        

//convert Object to JSONObject

JSONObject jsonObject = (JSONObject)object;

         

//Reading the String

//String result = (String) jsonObject.get("result").toString();    

//System.out.println("1. "+result);// OK

        

//JSONObject  result = (JSONObject) jsonObject.get("result");    

//System.out.println("1. "+result.toString());

//JSONArray items = (JSONArray) result.get("items");    

//System.out.println("2. "+items.toString());// OK

        

JSONObject  result = (JSONObject) jsonObject.get("result");    

System.out.println("1. "+result.toString());

JSONArray items = (JSONArray) result.get("items");    

System.out.println("2. "+items.toString());

        

JSONObject dataObj = (JSONObject) items.get(0);

System.out.println("dataObj : " + dataObj);

//dataObj : {"point":{"y":36.9651698,"x":127.8709602},"address":"충청북도 충주시 대소원면 대학로  25","addrdetail":{"sigugun":"충주시 대소원면","sido":"충청북도","rest":" 25","dongmyun":"대학로","country":"대한민국"},"isRoadAddress":true}


JSONObject  addrdetail = (JSONObject) dataObj.get("addrdetail");    

System.out.println("3. "+addrdetail.toString());

JSONObject  point = (JSONObject) dataObj.get("point");    

System.out.println("4. "+point.toString());


sido = (String) addrdetail.get("sido");    

System.out.println("5-1. "+sido.toString());

sigugun = (String) addrdetail.get("sigugun");    

System.out.println("5-2. "+sigugun.toString());

dongmyun = (String) addrdetail.get("dongmyun");    

System.out.println("5-3. "+dongmyun.toString());

 rest = (String) addrdetail.get("rest");    

System.out.println("5-4. "+rest.toString());

        

      

String x = (String) point.get("x").toString();    

System.out.println("6-1. "+x.toString());

        

 String y = (String) point.get("y").toString();    

 System.out.println("6-2. "+y.toString());



1. {"total":1,"userquery":"충청북도충주시대소원면대학로25","items":[{"point":{"y":36.9651698,"x":127.8709602},"address":"충청북도 충주시 대소원면 대학로  25","addrdetail":{"sigugun":"충주시 대소원면","sido":"충청북도","rest":" 25","dongmyun":"대학로","country":"대한민국"},"isRoadAddress":true}]}


2. [{"point":{"y":36.9651698,"x":127.8709602},"address":"충청북도 충주시 대소원면 대학로  25","addrdetail":{"sigugun":"충주시 대소원면","sido":"충청북도","rest":" 25","dongmyun":"대학로","country":"대한민국"},"isRoadAddress":true}]

dataObj : {"point":{"y":36.9651698,"x":127.8709602},"address":"충청북도 충주시 대소원면 대학로  25","addrdetail":{"sigugun":"충주시 대소원면","sido":"충청북도","rest":" 25","dongmyun":"대학로","country":"대한민국"},"isRoadAddress":true}


3. {"sigugun":"충주시 대소원면","sido":"충청북도","rest":" 25","dongmyun":"대학로","country":"대한민국"}


4. {"y":36.9651698,"x":127.8709602}

5-1. 충청북도

5-2. 충주시 대소원면

5-3. 대학로

5-4.  25

6-1. 127.8709602

6-2. 36.9651698








반응형

'차근차근 > Spring' 카테고리의 다른 글

MySQL FULLTEXT 검색  (0) 2020.02.18
java.lang.IllegalStateException: Can't stop StopWatch: it's not running  (0) 2016.10.20
sendmail test - htmlemail ( 1 )  (0) 2016.04.22
게시판  (0) 2015.06.15
@SuppressWarnings  (0) 2015.06.15