출처 : http://moonset.tistory.com/102

이번 Project를 하면서 Open Source를 많이 이용하게 됐다.

( = 저가의 생산비용으로 개발자 힘들게 하는 일.)


Linux , Apache, Weblogic, Oracle

파일 업로드 컴포넌트도 없고, 에디터 또한 FCKEditor라는 처음 들어본 것을 사용했다.


이러한 상황에서...

지도 및 차트를 그리는 부분이 있었는데..

고객같은 PM님께서 꼭 Google Code API를 사용해야 한다고 주장하셨다.


Map API는 네이버, 다음, 구글 등에서 제공하고 있다.

구글은 오래되긴 했지만, 영어라는 한계가 있고, 로컬(미국)에서만 되는 기능들이 있다.

(place.AddressDetails.Country.AdministrativeArea 등으로 이루어진 상세 주소를 제공하지 않는다. 

따라서 zipcode 및 기타 정보를 가져올 수 없었다. 

Zipcode는 DB에서 가져왔으며, 기타 정보는 for문 돌려서 잘랐다.)

그런 기능을 제외하면 무난하게 사용할 수 있다.

(단, 가이드가 영어라 읽어 내려가기 귀찮은 부분 있음)



* 시작하기 *

1. Google Map API에서 '키'를 받아야 한다.

   (http://code.google.com/intl/ko-KR/apis/maps/index.html)


2. google 계정이 있으면 쉽게 받을 수 있으나, 클릭수 제한이 있다.

   (50,000번으로 개인이 사용하기에는 무리 없음)


3. 해당 서비스는 무료로 제공하고, 로고 및 맵 지우면 안된다.


4. 또한 사용하는 url에 따라 받아야 하므로, 생각해서 받으시길...

(localhost 와 test.com 다른 key 필요)



* 사용하기 *

검색하면 해당 내용으로 블로깅 한 곳이 많으니 참고하면 된다.


- referencd -

http://code.google.com/intl/ko-KR/apis/maps/documentation/javascript/v2/reference.html

http://code.google.com/intl/ko-KR/apis/maps/documentation/javascript/reference.html


- geocode -

http://code.google.com/intl/ko-KR/apis/maps/documentation/mapplets/services.html


- geocode  detail-

http://www.developer.com/tech/article.php/3615681/Introducing-Googles-Geocoding-Service.htm


- db에서 가져오기 -

http://code.google.com/intl/ko/apis/maps/articles/phpsqlajax.html



* 활용하기 *


- 주소 찾기 -

http://maps.google.com/maps?q=37.484530070872005,127.04534322023392

(latlng로 찾기)


http://maps.google.com/maps?q=37.484530070872005,127.04534322023392&gl=KR&output=xml&key=본인키값넣기&oe=utf-8

(본인키값 및 latlng로 찾기)


http://maps.google.com/maps/api/geocode/json?latlng=37.484530070872005,127.04534322023392 

(json으로 받기)


http://maps.google.com/maps/api/geocode/xml?latlng=37.484530070872005,127.04536736011505&sensor=false&oe=utf-8

(xml로 받기)



* Sample *


간단하게 주소입력 페이지를 만들었다. (IE만 지원됨. Firefox 및 safari 지원X)

zipcode만 추가하면... 클릭하여 입력가능한 페이지가 생성된다.

소스는 첨부파일 참고 (key는 localhost로 받았음) 

 test.html



- 사용 (Firefox에서는 map 부분 이상이 있으므로 IE를 권장) -

방법 1. 각 항목에 주소를 넣고 '검색'을 누른다.

방법 2. 지도를 클릭한다.


 * gCode 확인 *

시 / 도

경기도

구 / 군

구리시

동(읍/면/리)

인창동

기타주소

678-3 검색

 

Longitude

127.1439294

Latitude

37.6031857





* JAVA로는 어떻게? *


사실, 우리가 필요한 정보는 x,y좌표다.

그리고 일을 하면서 꼭 javascript로만 처리할수도 없다.

(file upload를 이용하여 해당 내용을 읽어 처리하는 경우가 생겨버렸다.)


그렇게 삽질은 시작되었고.. 찾았다.

'개발(開)'을 '개발()'로 해서... 소스는 정제되니 않았으니, 필요한 부분을 수정필요하다.

 (소스는 아래 참고)

 AddressBean.java



사용방법은 javascript와 별반 다르지 않다.

다만... 해당 값을 가져오는데 시간이 필요하기도 하고, 오류 정보를 받는대도 시간이 걸린다.

1. time wait를 건다.

2. 해당 값 체크를 하여 Retry 해야한다.



############################################################################

###############                                                                ##################

###############   1. 파일은 UTF-8로 저장되어 있음.              ##################

###############   2. Key 는 'localhost'로 받았음.                 ##################

###############   3. time out 및 retry 고려되지 않음.          ##################

###############                                                                 ##################

############################################################################

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.URL;

import java.net.URLEncoder;

import java.util.HashMap;

import org.json.JSONArray;

import org.json.JSONObject;

import com.ucube.common.util.Log;


public class AddressBean {

 public HashMap getLatLngByAddress(String address, String gmapKey)throws Exception {

  HashMap addrHashmap = new HashMap();

  

  //String address = "서울 강남구 도곡동 41";

  //String gmapKey = "ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA";  

  String BASE_GEOCODER_URL = "http://maps.google.com/maps/geo?";

  String ENCODING = "UTF-8";

  

  String GOOGLE_MAPS_KEY = gmapKey;

  String OUTPUT_FORMAT = "json";

  String GEOCODER_REQUEST = 

            BASE_GEOCODER_URL +

            "oe=utf-8&hl=ko&q=" + URLEncoder.encode(address, ENCODING) +

            "&key=" + GOOGLE_MAPS_KEY +

            "&output=" + OUTPUT_FORMAT;    

   

        BufferedReader reader = new BufferedReader(

                new InputStreamReader(

                    new URL(GEOCODER_REQUEST).openStream()));  

        String line = "";

        String jsondata = "";

        

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

         jsondata += line;         

        }

        Log.debug.printDesc("@@@@@@@@@@@@@@  AddressBean getLatLngByAddress  jsondata  @@@@@@@@@@@@@@" + jsondata );

        try{

         JSONObject jsonObj2 = new JSONObject(jsondata);

         JSONArray jarray = jsonObj2.getJSONArray("Placemark");

          

        JSONArray jsonObj5 = jarray.getJSONObject(0).getJSONObject("Point").getJSONArray("coordinates");

        Log.debug.printDesc("@@@@@@@@@@@@@@  AddressBean getLatLngByAddress  jsonObj5.getString(0)   =    "    +  jsonObj5.getString(0));

        Log.debug.printDesc("@@@@@@@@@@@@@@  AddressBean getLatLngByAddress  jsonObj5.getString(1)   =    "    +  jsonObj5.getString(1));

        addrHashmap.put("x", jsonObj5.getString(0));

        addrHashmap.put("y", jsonObj5.getString(1));

        addrHashmap.put("result","1");

        }catch(Exception e){

         System.out.println("AddressBean getLatLngByAddress  Exception  = ============  " + e.toString());

         addrHashmap.put("result","0");

        }finally{

         return addrHashmap;

        }

}

+ Recent posts