* 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 꿀팁]
'android' 카테고리의 다른 글
구글맵 화면에 보이는 범위 위도 경도 (0) | 2020.03.05 |
---|---|
FCM(Firebase Cloud Messaging) 설정하기 (0) | 2020.03.04 |
Geocoder 주소 > 위도, 경도 변환 (0) | 2020.03.02 |
가로세로 전환시 새로고침 안되게 유지 (0) | 2020.02.21 |
YouTube API 플레이어 (0) | 2020.02.19 |