Android/LBS

Geocoder 도분초를 주소로, 주소를 도분초로!

donghune 2019. 7. 29. 16:30

Geocoder는 위 경도를 주소 문자열로 혹은 주소를 위경도 값으로 변형시키기 위한 프로그램

Geocoding : 주소를 위경도로 변환

Reverse Geocoding : 위경도를 주소로 변환

 

내부적으로 Google Server와 Network 통신이 이루어진다.


1. GeoCoder 사용하기 문자주소 -> 위 경도

val geocoder = Geocoder(this)
val results = geocoder.getFromLocationName("서울특별시 동대문구", 1)
val latlng = LatLng(results[0].latitude, results[0].longitude)

서울특별시 동대문구를 기점으로 가장 근사값을 가져와서 출력한다.

lat/lng: (37.574368199999995,127.04001889999999)

2. GeoCoder 사용하기 위경도 -> 문자주소

val geocoder = Geocoder(this)
val results = geocoder.getFromLocationName("서울특별시 동대문구", 1)
val latlng = LatLng(results[0].latitude, results[0].longitude)

val address = geocoder.getFromLocation(latlng.latitude, latlng.longitude, 1)[0]

입력된 주소값을 기점으로 가장 근사값을 가져와서 출력한다.

Address[
	addressLines=[0:"대한민국 서울특별시 동대문구 용신동 39-9"],
    feature=39−9,
    admin=서울특별시,
    sub-admin=null,
    locality=null,
    thoroughfare=용신동,
    postalCode=130-070,
    countryCode=KR,
    countryName=대한민국,
    hasLatitude=true,
    latitude=37.5742015,
    hasLongitude=true,
    longitude=127.03983269999999,
    phone=null,
    url=null,
    extras=null
]

각 나라별로 주소체제가 상이해서 일부값에는 null 값이 반환된다.