티스토리 뷰

이번 포스팅에서는 안드로이드에서 위도,경도 좌표로 주소를 구하는 방법과 주소로 위도,경도를 구하는 방법에 대해서 설명 하도록 하겠습니다. 안드로이드 개발을 하다보면 핸드폰의 GPS를 이용 해서  좌표를 얻어와 활용 하는 경우가 많이 있습니다.이럴때 유용 하게 활용 될 수 있는게 이 좌표로 주소를 구하거나 주소로 좌표를 구하는 것입니다.


Geocoder를 사용 하여 값을 구하는 것으로 소스 코드 참고 하시면 쉽게 구현 하 실 수 있을겁니다.


위도,경도로 주소 구하기


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
 
    /**
     * 위도,경도로 주소구하기
     * @param lat
     * @param lng
     * @return 주소
     */
    public static String getAddress(Context mContext,double lat, double lng) {                                             
        String nowAddress ="현재 위치를 확인 할 수 없습니다."
        Geocoder geocoder = new Geocoder(mContext, Locale.KOREA);
        List <Address> address;
        try {
          if (geocoder != null) {
                  //세번째 파라미터는 좌표에 대해 주소를 리턴 받는 갯수로 
                  //한좌표에 대해 두개이상의 이름이 존재할수있기에 주소배열을 리턴받기 위해 최대갯수 설정
                address = geocoder.getFromLocation(lat, lng, 1);
                
               if (address != null && address.size() > 0) {
                    // 주소 받아오기
                    String currentLocationAddress = address.get(0).getAddressLine(0).toString();
                    nowAddress  = currentLocationAddress;                   
 
                }
            }
             
         } catch (IOException e) {
          Toast.makeText(baseContext, "주소를 가져 올 수 없습니다.", Toast.LENGTH_LONG).show();
 
             e.printStackTrace();
         }
         return nowAddress;
     }
cs


주소로 위도,경도 구하기


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
/**
     * 주소로부터 위치정보 취득
     * 
     * @param address
     *            주소
     */
    public static Location findGeoPoint(Context mcontext, String address) {
        Location loc = new Location("");
        Geocoder coder = new Geocoder(mcontext);
        List<Address> addr = null;// 한좌표에 대해 두개이상의 이름이 존재할수있기에 주소배열을 리턴받기 위해 설정
 
        try {
            addr = coder.getFromLocationName(address, 5);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }// 몇개 까지의 주소를 원하는지 지정 1~5개 정도가 적당
        if (addr != null) {
            for (int i = 0; i < addr.size(); i++) {
                Address lating = addr.get(i);
                double lat = lating.getLatitude(); // 위도가져오기
                double lon = lating.getLongitude(); // 경도가져오기
                loc.setLatitude(lat);
                loc.setLongitude(lon);
            }
        }
        return loc;
    }
cs


댓글