Recent Posts
Recent Comments
«   2024/05   »
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
관리 메뉴

Dev.

간단한 지오코딩 (주소로 좌표얻기) 본문

Android

간단한 지오코딩 (주소로 좌표얻기)

Pppdw 2015. 7. 21. 13:52
 

/** @Author : pppdw
* @Description : 구글 URL을 이용해 간단하게 lng.lat를 뽑는다. new HttpGet 생성자에 사용된 URL로 뽑고자하는 지역의 네임값만 날리면된다.
*                  단 네임값에 공백이 있으면 안되며, 공백이 존재 할 시 공백을 +로 변경하여 리퀘스트 요청을 해야한다.
* @Param : strPlaceName --> 지오코딩 하고자 하는 지역의 이름 (예시 : "서울특별시+강남구+개포동+3421번지")
**/

private void getLatLng(String strPlaceName){
		String strPlaceNameClone = null;
		strPlaceNameClone = strPlaceName.replace(" ", "+");
		JSONObject jsonObject = new JSONObject();
		
	    HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +strPlaceNameClone+"&ka&sensor=false");
	    HttpClient client = new DefaultHttpClient();
	    HttpResponse response;
	    StringBuilder stringBuilder = new StringBuilder();

	    try {
	        response = client.execute(httpGet);
	        HttpEntity entity = response.getEntity();
	        InputStream stream = entity.getContent();
	        int b;
	        while ((b = stream.read()) != -1) {
	            stringBuilder.append((char) b);
	        }
	    } catch (ClientProtocolException e) {
	    } catch (IOException e) {
	    }

	    try {
	        jsonObject = new JSONObject(stringBuilder.toString());
	    } catch (JSONException e) {

	        e.printStackTrace();
	    }
		
	    Double lng = new Double(0);
	    Double lat = new Double(0);
	    
	    try {
	    	lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
	            .getJSONObject("geometry").getJSONObject("location")
	            .getDouble("lng");

	        lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
	            .getJSONObject("geometry").getJSONObject("location")
	            .getDouble("lat");

	    } catch (JSONException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    }
	}
Comments