본문 바로가기
Programming/Android Java

LocationManager를 이용한 위지 확인.

by 개Foot/Dog발?! 2014. 6. 17.

URL : http://ddoddoing.tistory.com/134



.....


// LocationManager 객체 생성
// You do not instantiate this class directly; instead, retrieve it
// through Context.getSystemService(Context.LOCATION_SERVICE).
// LocationManager 객체는
// Context.getSystemService(Context.LOCATION_SERVICE) 로 생성된다.
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

// LocationManaer.NETWORK_PROVIDER : 기지국들로부터 현재 위치 확인
// LocationManaer.GPS_PROVIDER : GPS들로부터 현재 위치 확인
// boolean isProviderEnabled(String provider)
// provider의 상태가 현재 어떠한지 boolean으로 return 해준다.
if (locationManager.isProviderEnabled(locationManager.NETWORK_PROVIDER) == true) {
            // NETWORK_PROVIDER가 Enabled 라면..
            locationProvider = LocationManager.NETWORK_PROVIDER;
}

else {
            // GPS_PROVIDER가 Enabled 라면..
            locationProvider = LocationManager.GPS_PROVIDER;
}

// 현재 위치를 조회한다. 결과는 locationListener를 통해 수신
// public void requestLocationUpdates
// (String provider, 방법
// long minTime, 위치 업데이트하는 최소한의 간격(단위 밀리세컨드)
// float minDistance, 위치 업데이트되는 최소한의 거리(단위 미터)
// LocationListener listener )
locationManager.requestLocationUpdates(locationProvider, 300, 0, this);

// 최근 위치를 조회한다. 결과는 바로 얻을 수 있음
// Location getLastKnownLocation(String provider)
// Returns a Location indicating the data from the last known
// location fix obtained from the given provider.
// Provider를 인자로 받아 마지막으로 알려진 위치를 수정해서 위치를 리턴함
// Provider가 비활성화 되어있는 경우는 null이 return 된다.
Location lastKnowLocation = locationManager.getLastKnownLocation(locationProvider);



Longitude = (int) (arg0.getLongitude() * 1000000);// getLongitude 은 double형을 반환한다.
Latitude = (int) (arg0.getLatitude() * 1000000); //getLatitude은 double형을 반환한다.

.....