-->

안드로이드

 

 

 

class MainActivity

package kr.co.braverokmc.geocoding;

import android.location.Address;
import android.location.Geocoder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    EditText editText;

    Geocoder geocoder;

    TextView textView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText=(EditText)findViewById(R.id.editText);
        textView2=(TextView)findViewById(R.id.textView2);

        // context , 언어설정
        geocoder=new Geocoder(this, Locale.KOREA);
    }


    public void onButton1Clicked(View v){

        String address =editText.getText().toString();

        //좌표를 이용해서 주소를 가져오기
        //getFromLocation (double latitude, double longitude,)

        //주소를 이용해서 좌표를 가져오기
        try {
            // 3은 최대한 3개까지 표시 - 동일한 것이 여러것이 있을수 있으니깐
            //
           List<Address> addressList= geocoder.getFromLocationName(address, 3);
            if(addressList!=null){
                for(int i=0; i<addressList.size(); i++){
                    Address curAddress=addressList.get(i);
                    StringBuffer buffer=new StringBuffer();
                    //Address 에 여러가지가 들어가 있으니 다시 for 문을 출력
                    for (int k=0; k<= curAddress.getMaxAddressLineIndex(); k++){
                        buffer.append(curAddress.getAddressLine(k));
                    }
                    buffer.append("\n\tlatitude : " +curAddress.getLatitude());
                    buffer.append("\n\tlongitude : " +curAddress.getLongitude());

                    textView2.append("\nAddress #" +i + " : " + buffer.toString());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }


}

 

R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="kr.co.braverokmc.geocoding.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text=""
        android:ems="10"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:text="주소로 찾기"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="95dp"
        android:id="@+id/button"
        android:onClick="onButton1Clicked"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:id="@+id/textView2"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />


</RelativeLayout>

 

권한설정


    <uses-permission android:name="android.permission.INTERNET"/>

 

 

 

 

 

 

android

 

about author

PHRASE

Level 1  머나먼나라

댓글 ( 4)

댓글 남기기

작성