일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Swift
- label
- Android Studio
- 자바
- TextField
- deeplink
- storyboard
- BAEKJOON
- 플러터
- android java
- 안드로이드스튜디오
- 링크드리스트
- 커스텀팝업
- xocde
- IOS
- 예외처리
- Android
- Xcode
- C언어
- FLUTTER
- swift baekjoon
- Firebase
- 준코딩
- 안드로이드
- 연결리스트
- 백준
- 보호와 보안
- 버블정렬
- text to speech
- customPopup
Archives
- Today
- Total
준코딩
GPS를 이용하여 현재 주소 구하기.(Android Java) 본문
아래 코드는 위도와 경도를 구하여서 주소로 변환해 보여주는 코드입니다.
30분마다 업데이트 하면서 위치를 textView에 보여주고 있습니다.
public class MainActivity extends AppCompatActivity {
private TextView txtResult;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtResult = findViewById(R.id.txtResult);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 30분마다 위치 정보 업데이트
final Handler handler = new Handler();
final int delay = 30 * 1000 * 60; // 30분마다 업데이트
handler.postDelayed(new Runnable() {
public void run() {
getLastKnownLocation();
startLocationUpdates();
handler.postDelayed(this, delay);
}
}, delay);
}
private void getLastKnownLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
updateLocation(location);
} else {
showNoLocationToast();
}
}
}
private void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
@SuppressLint("SetTextI18n")
private void updateLocation(Location location) {
double longitude = location.getLongitude();
double latitude = location.getLatitude();
long currentTimeMillis = System.currentTimeMillis();
Date currentTime = new Date(currentTimeMillis);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String formattedTime = sdf.format(currentTime);
LatLng latLng = new LatLng(latitude, longitude);
String address = getAddressFromLatLng(this, latLng);
txtResult.setText("시간 : " + formattedTime + "\n" + "주소 : " + address);
}
private String getAddressFromLatLng(Context context, LatLng latLng) {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses;
try {
addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
StringBuilder sb = new StringBuilder();
// 주소의 각 줄을 StringBuilder에 추가
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i));
if (i < address.getMaxAddressLineIndex()) {
sb.append(", ");
}
}
return sb.toString();
}
} catch (IOException e) {
e.printStackTrace();
}
return "주소를 찾을 수 없음";
}
private void showNoLocationToast() {
Toast.makeText(this, "현재 위치를 가져올 수 없습니다.", Toast.LENGTH_SHORT).show();
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
}
'프로그래밍 > Android(JAVA)' 카테고리의 다른 글
커스텀 팝업 생성하기 / CustomDialog (0) | 2023.06.26 |
---|---|
핑거푸시 연동하기 (FingerPush Android) (0) | 2023.06.26 |
파이어베이스 다이나믹링크 Android13 버전 에러 (0) | 2023.06.26 |
Pull to Refresh 당겨서 새로고침 (0) | 2023.04.26 |
Android(java) 앱 버전 업데이트 빌드 에러[The destination foler does not exist or is not writeable] (2) | 2023.04.05 |
Comments