[flutter] 플러터에서 위치 정보를 이용한 주변 약국 검색 앱 만들기

플러터를 사용하여 위치 정보를 활용하여 사용자 주변의 약국을 검색하는 앱을 만들어보겠습니다. 이를 위해 Google Maps API 및 위치 정보에 대한 권한을 사용할 것입니다.

목차

프로젝트 설정

먼저, 새로운 플러터 프로젝트를 생성합니다. 프로젝트를 생성하려면 터미널에서 다음 명령을 실행합니다:

flutter create pharmacy_finder

Google Maps API 키 발급

앱에서 사용할 Google Maps API를 호출하기 위해 API 키가 필요합니다. Google Cloud Console에서 프로젝트를 생성하고 API 키를 발급받아야 합니다. API 키 발급 방법에 대한 자세한 내용은 Google Cloud 공식 문서를 참고하세요.

플러터 앱 개발

플러터 앱 기본 설정

pubspec.yaml 파일을 열고, 다음 의존성을 추가합니다:

dependencies:
  flutter:
    sdk: flutter
  google_maps_flutter: ^2.0.2
  location: ^4.3.0

의존성을 추가한 후에는 터미널에서 flutter pub get 명령을 실행하여 패키지를 설치합니다.

위치 권한 요청

사용자의 현재 위치를 가져오기 위해서는 위치 권한이 필요합니다. 따라서 location 패키지를 사용하여 위치 권한을 요청해야 합니다. 아래는 위치 권한을 요청하는 방법입니다:

import 'package:location/location.dart';

var location = Location();
bool _serviceEnabled;
PermissionStatus _permissionGranted;
LocationData _locationData;

_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
  _serviceEnabled = await location.requestService();
  if (!_serviceEnabled) {
    return;
  }
}

_permissionGranted = await location.hasPermission();
if (_permissionGranted == PermissionStatus.denied) {
  _permissionGranted = await location.requestPermission();
  if (_permissionGranted != PermissionStatus.granted) {
    return;
  }
}

_locationData = await location.getLocation();

주변 약국 검색 기능 구현

위치 권한을 획들한 후, Google Maps API를 사용하여 주변 약국을 검색할 수 있습니다. 검색 결과를 지도에 표시하고 필요에 따라 리스트로 표시하는 기능을 구현할 수 있습니다. 아래는 Google Places API를 사용하여 주변 약국을 검색하는 간단한 예제입니다:

import 'package:google_maps_webservice/places.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

final places = GoogleMapsPlaces(apiKey: 'YOUR_API_KEY');

PlacesSearchResponse response = await places.searchNearbyWithRadius(
  Location(latitide, longitude), 1000,
  type: 'pharmacy',
);

위 코드는 Google Places API를 사용하여 현재 위치 주변 1000m 범위 내의 약국을 검색하는 예제입니다.

이제 여러분은 플러터를 사용하여 위치 정보를 이용하여 주변 약국을 검색하는 앱을 만들 수 있습니다. 해당 앱에서 사용자에게 정확하고 유용한 정보를 제공하여 편리한 서비스를 제공할 수 있습니다.