Manifest 추가
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
java 코드
/** * LatLng 기반으로 주소를 확인 * @param mContext : Geocoder에 전달하기 위한 context * @param latlng : GPS Latitude, longitude */ public static void getLocation(Context mContext, LatLng latlng){ Geocoder geocoder = new Geocoder(mContext); try { // latitude : the latitude a point for the search // longitude : the longitude a point for the search // maxResults : max number of addresses to return. Smaller numbers (1 to 5) are recommended List<Address> mListAddress = geocoder.getFromLocation(latlng.latitude, latlng.longitude, 5); for(int i=0; i<mListAddress.size(); i++){ Log.e("GPS_Info", " " + i + " : " + mListAddress.get(i).toString()); } } catch (IOException e) { e.printStackTrace(); } }
LatLng(37.151984, 126.844849)의 Log 결과
0 : Address[addressLines=[0:"대한민국 경기도 화성시 팔탄면 노하리 1224-8"],feature=1224−8,admin=경기도,sub-admin=null,locality=화성시,thoroughfare=팔탄면,postalCode=445-910,countryCode=KR,countryName=대한민국,hasLatitude=true,latitude=37.151984,hasLongitude=true,longitude=126.84484989999999,phone=null,url=null,extras=null] 1 : Address[addressLines=[0:"대한민국 경기도 화성시 팔탄면 노하리"],feature=445-909,admin=경기도,sub-admin=null,locality=화성시,thoroughfare=팔탄면,postalCode=445-909,countryCode=KR,countryName=대한민국,hasLatitude=true,latitude=37.1550957,hasLongitude=true,longitude=126.8546282,phone=null,url=null,extras=null] 2 : Address[addressLines=[0:"대한민국 경기도 화성시 팔탄면 노하리"],feature=노하리,admin=경기도,sub-admin=null,locality=화성시,thoroughfare=팔탄면,postalCode=445-910,countryCode=KR,countryName=대한민국,hasLatitude=true,latitude=37.1545293,hasLongitude=true,longitude=126.85712099999999,phone=null,url=null,extras=null] 3 : Address[addressLines=[0:"대한민국 경기도 화성시 팔탄면"],feature=팔탄면,admin=경기도,sub-admin=null,locality=화성시,thoroughfare=팔탄면,postalCode=445-910,countryCode=KR,countryName=대한민국,hasLatitude=true,latitude=37.161111999999996,hasLongitude=true,longitude=126.9032039,phone=null,url=null,extras=null] 4 : Address[addressLines=[0:"대한민국 경기도 화성시"],feature=화성시,admin=경기도,sub-admin=null,locality=화성시,thoroughfare=null,postalCode=null,countryCode=KR,countryName=대한민국,hasLatitude=true,latitude=37.1994932,hasLongitude=true,longitude=126.8311887,phone=null,url=null,extras=null] 5 : Address[addressLines=[0:"대한민국 경기도"],feature=경기도,admin=경기도,sub-admin=null,locality=null,thoroughfare=null,postalCode=null,countryCode=KR,countryName=대한민국,hasLatitude=true,latitude=37.413799999999995,hasLongitude=true,longitude=127.5183,phone=null,url=null,extras=null] 6 : Address[addressLines=[0:"대한민국"],feature=대한민국,admin=null,sub-admin=null,locality=null,thoroughfare=null,postalCode=null,countryCode=KR,countryName=대한민국,hasLatitude=true,latitude=35.907757,hasLongitude=true,longitude=127.76692200000001,phone=null,url=null,extras=null]
GPS주소가 정확히 건물 위치가 아닌 도로한가운데를 가르킨다. Return 되는 address들은 정확히 도로명을 나타내기 보다는
주변에 등록된 가까운 주소를 반환하는 것 같다.
Address 0,1,2 는 그 위치에서 가까운 주소를 반환
Address 3 은 뒤에 자세한 주소를 생략하고 ' ~면 ' 까지만 반환
Address 4 는 ' ~시 ' 까지만 반환
Address 5 는 ' ~도 ' 까지만 반환
Address 6 는 국가 까지만 반환실제 구글지도에서 Lat, Lng 찍은 값과 다른 주소값이 리턴되기 때문에 정확한 주소를 다 사용하기 보다는
Address 0,1,2의 thoroughfare 데이터까지만 사용하면 될것 같다.
'안드로이드' 카테고리의 다른 글
Android와 PHP서버 MySQL 연동 (0) | 2019.07.08 |
---|---|
build.gradle 중복 라이브러리 문제 (0) | 2019.07.08 |
Android webview에서 동영상 재생시키기 (0) | 2016.08.04 |