* AndroidManifest.xml

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

* Listener 구현

final LocationListener gpsLocationListener = new LocationListener() {

    public void onLocationChanged(Location location) {
 
        String provider = location.getProvider();
        double longitude = location.getLongitude();
        double latitude = location.getLatitude();
        double altitude = location.getAltitude();
 
        txtResult.setText("위치정보 : " + provider + "\n" +
                "위도 : " + longitude + "\n" +
                "경도 : " + latitude + "\n" +
                "고도  : " + altitude);
 
    }
 
    public void onStatusChanged(String provider, int status, Bundle extras) {
    
    }
 
    public void onProviderEnabled(String provider) {
    
    }
 
    public void onProviderDisabled(String provider) {
    
    }
    
};

* LocationManager

final LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

* 위치 업데이트

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
        1000,
        1,
        gpsLocationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
        1000,
        1,
        gpsLocationListener);

* MainActivity.java

public class MainActivity extends AppCompatActivity {
 
    private Button button1;
    private TextView txtResult;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button)findViewById(R.id.button1);
        txtResult = (TextView)findViewById(R.id.txtResult);
 
        final LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
 
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( Build.VERSION.SDK_INT >= 23 &&
                        ContextCompat.checkSelfPermission( getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) {
                    ActivityCompat.requestPermissions( MainActivity.this, new String[] {  android.Manifest.permission.ACCESS_FINE_LOCATION  },
                            0 );
                }
                else{
                    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    String provider = location.getProvider();
                    double longitude = location.getLongitude();
                    double latitude = location.getLatitude();
                    double altitude = location.getAltitude();
 
                    txtResult.setText("위치정보 : " + provider + "\n" +
                            "위도 : " + longitude + "\n" +
                            "경도 : " + latitude + "\n" +
                            "고도  : " + altitude);
 
                    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                            1000,
                            1,
                            gpsLocationListener);
                    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                            1000,
                            1,
                            gpsLocationListener);
                }
            }
        });
 
    }
    final LocationListener gpsLocationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
 
            String provider = location.getProvider();
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
            double altitude = location.getAltitude();
 
            txtResult.setText("위치정보 : " + provider + "\n" +
                    "위도 : " + longitude + "\n" +
                    "경도 : " + latitude + "\n" +
                    "고도  : " + altitude);
 
        }
 
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
 
        public void onProviderEnabled(String provider) {
        }
 
        public void onProviderDisabled(String provider) {
        }
    };
}
출처: https://bottlecok.tistory.com/54 [잡캐의 IT 꿀팁]
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,