티스토리 뷰

안드로이드 GPS 정보 가져오기

Dexx 2015. 10. 1. 01:36

[GPS정보 가져오기]

http://www.devblog.kr/r/8y0gFPAvJ2j8MWIVVXucyP9uYvQegfSVbY5XM5LhW




[GPS정보를 기반으로 주소 리턴받기]

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
public String getAddress(double lat, double lng) {    
    String address = null;    
 
    //위치정보를 활용하기 위한 구글 API 객체    
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());    
 
    //주소 목록을 담기 위한 List    
    List<Address> list = null;    
 
    try {        
        //주소 목록을 가져온다. --> 위도, 경도, 조회 갯수        
        list = geocoder.getFromLocation(lat, lng, 1);    
    } catch (IOException e) {        
        e.printStackTrace();    
    }    
 
    if (list == null) {        
        Log.e("getAddress""주소 데이터 얻기 실패");        
        return null;    
    }    
 
    if (list.size() > 0) {        
        Address addr = list.get(0);        
        address = addr.getCountryName() + " "       // 나라                
        + addr.getAdminArea() + " "                 // 시                
        + addr.getLocality() + " "                  // 구                
        + addr.getThoroughfare() + " "              // 동                
        + addr.getFeatureName();                    // 지번    
    }    
    return address;
}
cs



[일부 주소정보만 받기]
1
2
3
4
5
6
7
8
9
10
11
12
public String getAddressLocality(double lat, double lng) {    
    String address = null;    
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());    
    try {        
        List<Address> addr = geocoder.getFromLocation(lat, lng, 1);        
        Address a = addr.get(0);        
        address = a.getThoroughfare();    
    } catch (IOException e) {        
        e.printStackTrace();    
    }    
    return address;
}
cs


[주소 정보 메소드 호출 하기]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
btnShowLocation.setOnClickListener(new View.OnClickListener() {    
    @Override    
    public void onClick(View v) {        
        gps = new GPSInfo(MainActivity.this);        
 
        if (gps.isGetLocation()) {            
            double latitude = gps.getLatitude();            
            double longitude = gps.getLongitude(); 
           
            txtLat.setText(String.valueOf(latitude));            
            txtLon.setText(String.valueOf(longitude));
            
            txtAdress.setText(getAddress(latitude, longitude));            
            txtAdress.setText(getAddressLocality(latitude, longitude));
            
            Toast.makeText(getApplicationContext(), "당신의 위치 - \n위도 : " 
                + latitude + "\n경도 : " + longitude, Toast.LENGTH_SHORT).show(); 
       
        } else {            
            gps.showSettingsAlert();        
        }    
    }
});
cs



댓글