본문 바로가기

Android

Android Studio GPS 정보로 위치 경도 값 받아오기

반응형

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



위치 관리자 객체 생성.

 

@Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

       

 

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

 

        

            

                    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                    String provider = location.getProvider();

                    double longitude = location.getLongitude();

                    double latitude = location.getLatitude();

                    double altitude = location.getAltitude();

 

                   

 

                    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();

 

 

 

            

 

        }

 

        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

 

        public void onProviderEnabled(String provider) {

        }

 

        public void onProviderDisabled(String provider) {

        }

    };

 




위도 경도 값은 전역으로 선언후에 LatLng 객체 만들어서 생성자의 매개변수로 입력 시켜줘서 필요할 때 사용가능.

 

반응형