본문 바로가기
안드로이드

[Android] Geocoding

by 엘딘 2022. 4. 3.

위도, 경도를 입력하여 지도로 표시

위도,경도 <> 주소로 서로 변경해주는 기능

 

AndroidManifest(! 추가)
<manifest  xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.geocoding">
          
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>

	...
</manifest>

 

activity_main.xml
//위도,경도 <> 주소 변환하였을때 표시할 TextView
    <TextView
            android:layout_width="513dp"
            android:layout_height="115dp"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.732" android:id="@+id/textView"/>
    <TextView
            android:id="@+id/textView4"
            android:text="위도"
            android:textSize="20dp"
            android:layout_width="54dp"
            android:layout_height="28dp"
            app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="84dp"
            app:layout_constraintHorizontal_bias="0.122"/>
    <TextView
            android:id="@+id/textView2"
            android:text="경도"
            android:textSize="20dp"
            android:layout_width="54dp"
            android:layout_height="28dp"
            app:layout_constraintTop_toBottomOf="@+id/textView4"
            android:layout_marginTop="20dp" app:layout_constraintStart_toStartOf="@+id/textView4"/>
    <TextView
            android:id="@+id/textView3"
            android:text="주소"
            android:textSize="20dp"
            android:layout_width="54dp"
            android:layout_height="28dp"
            app:layout_constraintTop_toBottomOf="@+id/textView2"
            android:layout_marginTop="144dp" app:layout_constraintStart_toStartOf="@+id/textView2"/>
    <EditText
            android:id="@+id/addrText"
            android:layout_width="339dp"
            android:layout_height="43dp"
            android:inputType="textPersonName"
            android:ems="10"
            app:layout_constraintBottom_toBottomOf="@+id/textView3"
            app:layout_constraintStart_toEndOf="@+id/textView3" android:layout_marginStart="44dp"/>
    <EditText
            android:id="@+id/latText"
            android:layout_width="339dp"
            android:layout_height="43dp"
            android:inputType="textPersonName"
            android:ems="10"
            app:layout_constraintStart_toEndOf="@+id/textView4" android:layout_marginStart="40dp"
            app:layout_constraintBottom_toBottomOf="@+id/textView4"/>
    <EditText
            android:id="@+id/lonText"
            android:layout_width="339dp"
            android:layout_height="43dp"
            android:inputType="textPersonName"
            android:ems="10"
            app:layout_constraintStart_toEndOf="@+id/textView2"
            android:layout_marginStart="40dp" app:layout_constraintBottom_toBottomOf="@+id/textView2"/>
    <Button
            android:id="@+id/addrButton"
            android:text="주소로 변경"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/lonText"
            android:layout_marginTop="20dp" app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>
    <Button
            android:id="@+id/mapbutton1"
            android:text="지도"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toEndOf="@+id/addrButton"
            app:layout_constraintTop_toTopOf="@+id/addrButton"
            android:layout_marginStart="32dp" />
    <Button
            android:id="@+id/latButton"
            android:text="위도/경도 변경"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/addrText"
            android:layout_marginTop="16dp" app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.477"/>
    <Button
            android:id="@+id/mapButton2"
            android:text="지도"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toEndOf="@+id/latButton" 
            app:layout_constraintTop_toTopOf="@+id/latButton"
            android:layout_marginStart="32dp"/>

 

 

MainActivity
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 여행 -> 사진 : 위치 -> 주소 -> 위도, 경도

        val textView = findViewById<TextView>(R.id.textView)
        var addrBtn = findViewById<Button>(R.id.addrButton)
        var mapBtn1 = findViewById<Button>(R.id.mapbutton1)
        var latBtn = findViewById<Button>(R.id.latButton)
        var mapBtn2 = findViewById<Button>(R.id.mapButton2)

        var latEdit = findViewById<EditText>(R.id.latText)
        var lonEdit = findViewById<EditText>(R.id.lonText)
        var addrEdit = findViewById<EditText>(R.id.addrText)

        val geocoder:Geocoder = Geocoder(this)


        // 변환 : 위도, 경도 -> 주소로
        addrBtn.setOnClickListener{
            var list:List<Address>? = null

            try{
                val d1:Double = latEdit.text.toString().toDouble()
                val d2:Double = lonEdit.text.toString().toDouble()

                list = geocoder.getFromLocation(d1, d2, 10)

            }catch (e:IOException){
                Log.d("위도/경도", "입출력 오류")
            }

            if(list != null){
                if(list.isEmpty()){
                    textView.text = "해당되는 주소는 없습니다"
                }else{  // 정상적으로 산출된 값
                    textView.text = list[0].toString()
                }
            }
        }

        mapBtn1.setOnClickListener{
            val d1:Double = latEdit.text.toString().toDouble()
            val d2:Double = lonEdit.text.toString().toDouble()

            val intent = Intent(Intent.ACTION_VIEW, Uri.parse("geo:$d1, $d2"))
            startActivity(intent)

        }


    // 주소 -> 위도, 경도
        latBtn.setOnClickListener{
            var list:List<Address>? = null
            val str = addrEdit.text.toString()

            try {
                list = geocoder.getFromLocationName(str, 10)
            }catch (e:IOException){
            }
                if(list != null){
                    if(list!!.isEmpty()){
                        textView.text =  "해당되는 주소는 없습니다"
                    }else{
                        //textView.text = list!![0].toString()
                        textView.text = list!!.get(0).latitude.toString() + " " +  list!!.get(0).longitude.toString()
                    }
                }
            }
        mapBtn2.setOnClickListener {
            var list: List<Address>? = null
            val str = addrEdit.text.toString()

            try {
                list = geocoder.getFromLocationName(str, 10)
            } catch (e: IOException) {
            }

            if(list != null){
                if(list!!.isEmpty()){
                    textView.text = "해당되는 주소는 없습니다"
                }else{
                    val addr = list!![0]
                    val lat = addr.latitude
                    val lon = addr.longitude
                    val geo = String.format("geo:%f,%f", lat, lon)
                    val intent =  Intent(Intent.ACTION_VIEW, Uri.parse(geo))
                    startActivity(intent)
                }
            }

        }
    }
}

'안드로이드' 카테고리의 다른 글

[Android] 숫자 +/-  (0) 2022.04.05
[Android] GoogleMapsFragment  (0) 2022.04.05
[안드로이드] Camera  (0) 2022.04.03
[Android] 동적버튼  (0) 2022.04.03
[Android] SQLite  (0) 2022.04.02

댓글