728x90
반응형
1. 뭐하는 위젯인가?
Geolocator는 Flutter 애플리케이션에서 사용자의 현재 위치 정보를 얻고 위치 서비스를 처리하는 데 사용되는 패키지다. 현재 위치 검색, 지속적인 위치 추적, 거리 계산, 위치 서비스 상태 확인, 지오코딩 등의 작업이 가능함.
최신 버전의 Geolocator사용을 위해서 위치 정보 수집에 대한 권한이 허용되어야한다.
2. 위치정보 사용을 위한 필요조건
위치 정보 수집에 대한 권한을 허용하려면, pubspec.yaml 파일에 패키지를 설치한 후, Android / iOS 운영체제별로 별도 설정을 해줘야한다.
- Android
프로젝트/android/app/src/main 에 있는 AndroidManifest.xml 태그의 <manifest> 태그 아래에 둘 중의 하나의 코드를 넣는다
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- iOS
프로젝트/ios/Runner에 있는 Info.plist에서 dict 태그 아래에 밑의 코드를 넣어준다.
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<dict>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
3. 사용 예시
위치 정보 관련 동의 팝업 띄우고, 현재 위치를 구해주는 함수를 호출해주면 된다. 비동기식으로 처리해야 함 주의
Future<void> getMyCurrentLocation() async {
try {
LocationPermission permission = await Geolocator.requestPermission(); //위치 정보 관련 동의 팝업 뜸
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high); //현재 위치 구하기
} catch (e) {
print('internet connection error');
}
}
여기까지 문제가 없었다면, 앱을 restart 할 때 아래와 같은 연결 성공 문구와 위치 정보 접근 허용 팝업이 뜨게 된다.
728x90
반응형
'Flutter' 카테고리의 다른 글
[Flutter] 밀어서 잠금해제 슬라이더(unlock slider) 구현하기 (0) | 2024.11.14 |
---|---|
[Flutter] AlertDialog 에서 TableCalendar 쓰기 (0) | 2024.11.13 |
[Flutter] 안드로이드 에뮬레이터 app:compileDebugKotlin / org.jetbrains.kotlin.compilerRunner 오류 해결하기 (0) | 2024.11.10 |
[Flutter] Stream Builder에서 TypeError (Null check operator used on a null value) 가 뜨는 경우 해결법 (2) | 2024.11.10 |
[Flutter] Firebase 파이어베이스 연동하기 - flutterfire 에러들 (0) | 2024.11.10 |