Flutter 에서 내위치 가져오기

모바일에서 내 위치를 가져오는 건 필수 기능 중 하나!!

관련 플러그인은 https://pub.dartlang.org/packages/geolocator#-readme-tab- 에서 확인 가능하다.

그럼 설정은?

pubspec.yaml

geolocator: '^2.0.1'

Geolocator 라는 메인 클래스를 통해서 위치를 가져 올 수 있다.

Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high)

리턴 값은 Future 비동기로 받는다. 그래서 then이나 await 로 사용이 가능하다.

Future<Position> getCurrentUserLocation() async {
    return Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
        .then((location) {      
      return location;
    });
  }

추후 이 함수(getCurrentUserLocation) 을 FutureBuilder 와 연계해서 UI 부분에서 핸들링 가능해 보인다.

이상으로 내 위치를 가져오는 방법에 대해서 알아보았다.

Flutter 개발자 오픈 채팅방 
https://open.kakao.com/o/gsshoXJ


+ Recent posts