์์น ์ ๋ณด ์ป๊ธฐ
https://pub.dev/packages/geolocator
android ํด๋ > app ํด๋ > src ํด๋ > profile ํด๋ > AndroidManifest.xml ํ์ผ
์๋๋ก์ด๋์์ geolocator๋ฅผ ์ด์ฉํ๋ ค๋ฉด AndroidManifest.xml
ํ์ผ์ ํผ๋ฏธ์
์ ์ ์ธํด์ผ ํจ. AndroidManifest.xml
์ ์๋๋ก์ด๋ ์ฑ์ ์คํํ๋ ๋ฐ ํ์ํ ๋ค์ํ ์ ๋ณด๋ฅผ ์ค์ ํ๋ ํ์ผ์ด๋ฉฐ ์ด๊ณณ์ ํผ๋ฏธ์
์ ์ค์ ํจ. ์๋๋ก์ด๋์์ ์คํ๋๋ ์ฑ์ด ์ฌ์ฉ์์ ํ์ฌ ์์น๋ฅผ ์ป์ผ๋ ค๋ฉด AndroidManifest.xml
์ ํผ๋ฏธ์
์ ๋ค์์ฒ๋ผ ์ ์ธํด์ผ ํจ.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
ios ํด๋ > Runner ํด๋ > info.plist ํ์ผ
iOS์์ ์คํ๋๋ ์ฑ์ด ์ฌ์ฉ์์ ํ์ฌ ์์น๋ฅผ ์ป์ผ๋ ค๋ฉด iOS ์ฑ์ info.plist ํ์ผ์ ๋ค์์ฒ๋ผ ํค๋ฅผ ๋ฑ๋กํด์ผ ํจ.
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
์๋๋ก์ด๋์ iOS ์ค์ ์ ํ์ผ๋ฉด ์ด์ ๋คํธ ์ฝ๋์์ geolocator
ํจํค์ง๋ฅผ ์ด์ฉํด ํ์ฌ ์์น๋ฅผ ์ป์ผ๋ฉด ๋จ. geolocator
์ ๋ชจ๋ ๊ธฐ๋ฅ์ Geolocator
ํด๋์ค์ ํจ์๋ก ์ ๊ณตํจ.
๋จผ์ ํ์ฌ ์์น๋ฅผ ์ป๊ธฐ ์ ์ ๊ธฐ๊ธฐ์์ ์์น ์ ๋ณด ํ๋ ๊ธฐ๋ฅ์ด ํ์ฑํ๋์๋์ง๋ฅผ ํ๋จํด์ผ ํจ. ์ํฉ์ ๋ฐ๋ผ ๊ธฐ๊ธฐ ์ค์ ๋ฑ์ผ๋ก ์์น ์ถ์ ์ด ๋ถ๊ฐ๋ฅํ ์ ์๊ธฐ ๋๋ฌธ. ์ด๋ Geolocator.isLocationServiceEnabled()
ํจ์๋ฅผ ํธ์ถํ๋ฉด ์ฝ๊ฒ ์์๋ผ ์ ์์. ์ด ํจ์์ ๋ฐํ๊ฐ์ด true
๋ฉด ํ์ฑ ์ํ, false
์ด๋ฉด ๋นํ์ฑ ์ํ๋ผ๋ ์๋ฏธ์.
// ์์น ์ ๋ณด ํ๋ ๊ฐ๋ฅํ์ง ํ์ธ
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!servieEnabled) {
return Future.error('Location services are disabled.');
}
๊ธฐ๊ธฐ์์ ์์น ์ถ์ ๊ธฐ๋ฅ์ด ํ์ฑ ์ํ๋ผ๊ณ ํ๋ฉด ๊ทธ๋ค์์ผ๋ก ํผ๋ฏธ์
์ค์ ์ ํ์ธํจ. ์์น ์ถ์ ์ ์ฌ์ฉ์์๊ฒ ๋ฏผ๊ฐํ ๊ธฐ๋ฅ์ด๋ฏ๋ก ํ์ฉํ ์ง๋ฅผ ์ฌ์ฉ์๊ฐ ์ ํํ ์ ์๊ฒ ํ๊ณ ์์. ๋ง์ฝ ์ฌ์ฉ์๊ฐ ์ฑ์ ์์น ์ถ์ ๊ธฐ๋ฅ์ ๊ฑฐ๋ถํ๋ค๋ฉด ์ฝ๋์์ ์์น๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์. ์ฑ์ ์์น ์ถ์ ํผ๋ฏธ์
์ด ์ค์ ๋๋์ง๋ Geolocator.checkPermission()
ํจ์๋ก ์ฝ๊ฒ ์ ์ ์์. ์ด ํจ์์ ๋ฐํ ๊ฐ์ LocationPermission
๊ฐ์ฒด์ด๋ฉฐ ์ด ๊ฐ์ด Location.denied
๋ผ๋ฉด ํผ๋ฏธ์
์ด ๊ฑฐ๋ถ๋ ์ํ์.
// ์์น ์ถ์ ํผ๋ฏธ์
ํ์ธ
LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error('permissions are denied');
}
}
ํผ๋ฏธ์
์ด ๊ฑฐ๋ถ๋ ์ํ์์๋ ์์น๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ผ๋ฏ๋ก ์ฌ์ฉ์์๊ฒ ํผ๋ฏธ์
์ ํ์ฉํด ๋ฌ๋ผ๊ณ ๋ค์ด์ผ๋ก๊ทธ๋ฅผ ๋์์ผ ํจ. ํผ๋ฏธ์
์กฐ์ ๋ค์ด์ผ๋ก๊ทธ๋ฅผ ๋์ฐ๋ ํจ์๋ GeoLocatior.requestPermission()
. ๊ทธ๋ฐ๋ฐ ์ฌ์ฉ์๊ฐ ๋ค์ ๊ฑฐ๋ถํ ์ ์์ผ๋ฏ๋ก ๋ค์ด์ผ๋ก๊ทธ๊ฐ ๋ซํ ๋ ์ฌ์ฉ์๊ฐ ์ด๋ป๊ฒ ์กฐ์ ํ๋์ง๋ฅผ GeoLocation. requestPersmission()
ํจ์์ ๊ฒฐ๊ด๊ฐ์ผ๋ก ๋ฐ์ ๋ค์ ํ๋ฒ ํผ๋ฏธ์
์ ํ์ธํด์ผ ํจ.
๋ง์ฝ ์์น ์ถ์ ํผ๋ฏธ์
์ ์ ์์ผ๋ก ๋ถ์ฌํ๋ค๋ฉด ์ด์ ์์น๋ฅผ ์ป์ ์ ์์. ์ด๋ Geolocator.getCurrentPosition()
ํจ์๋ฅผ ์ด์ฉํจ. ์ด ํจ์์ ๋ฐํ๊ฐ์ธ Position
๊ฐ์ฒด๊ฐ ํ์ฌ ์์น์ ํด๋นํ๋ ๊ฐ์. ์์น๋ ์๋์ ๊ฒฝ๋ ๊ฐ์ด๋ฉฐ Position
๊ฐ์ฒด์ latitude
, longitude
์์ฑ์ผ๋ก ์ป์. latitude
์ Longitude
๋ double
ํ์
์ ์ค์์.
// ํ์ฌ ์์น ๊ตฌํ๊ธฐ
Position position = await Geolocator.getCurrentPosition();
setState(() {
latitude = position.latitude.toString();
longitude = position.longitude.toString();
});
gelolocator ํจํค์ง๋ฅผ ํ์ฉํด ํ์ฌ ์์น๋ฅผ ์๋ ค์ฃผ๋ ์ฑ์ ๋ง๋ค์ด ๋ณผ ๊ฒ์. ๋ค์ดํฐ๋ธ ์ฝ๋์ ์ฐ๋ํด์ผ ํ๋ฏ๋ก ์๋๋ก์ด๋์ iOS ๊ฐ๊ฐ์์ ํ ์คํธํด๋ณด๋ ค ํจ.
pubspec.yaml
ํ์ผ์ ์ด๊ณ dependencies
ํญ๋ชฉ์ ๋ค์์ฒ๋ผ ํจํค์ง๋ฅผ ๋ฑ๋กํ ํ <Pub Get>
์ ํด๋ฆญ.
dependencies:
geolocator: ^8.2.1
lib
ํ์์ ์๋main.dart
ํ์ผ์ ๋ค์์ฒ๋ผ ์ฝ๋๋ฅผ ์์ฑ.
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return new MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: NativePluginWidget(),
);
}
}
class NativePluginWidget extends StatefulWidget {
NativePluginWidgetState createState() => NativePluginWidgetState();
}
class NativePluginWidgetState extends State<NativePluginWidget> {
String? latitude;
String? longitude;
getGeoData() async {
LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error('permissions are denied');
}
}
Position position await Geolocator.getCurrentPosition();
setState(() {
latitude.position.latitude.toString();
longitude.position.longitude.toString();
});
}
void initState() {
super.initState();
getGeoData();
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Geolocator")),
body: Container(
color: Colors.indigo,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: (<Widget>[
Text(
'MyLocation',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
Text(
'latitude : ${latitude}',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
Text(
'longitude: ${longitude}',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
]),
),
),
),
);
}
}
์๋๋ก์ด๋ ๋ฉ์ธ ํ๊ฒฝ ํ์ผ์ ์ด๊ณ ํผ๋ฏธ์
์ ์ ์ธํ๋ค. ์ด ํ์ผ์ android/app/src/main
๋๋ ํฐ๋ฆฌ์ ์์. ์ด ํ์ผ์ ์ด์ด ๋ค์์ฒ๋ผ ํผ๋ฏธ์
์ ์ ์ธํจ.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.flutter_lab">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
...(์๋ต)...
</manifest>
๊ทธ๋ฐ๋ค์ ์๋๋ก์ด๋ AVD๋ก ํ
์คํธํ๊ธฐ ์ํด ๊ฐ์์ ์์น ์ ๋ณด๋ฅผ ์ค์ ํจ. AVD ์๋จ์ ๋๊ตฌ์์ ๊ฐ์ฅ ์ค๋ฅธ์ชฝ์ Extended Controls
๋ผ๊ณ ํ๋ ์์ด์ฝ์ ํด๋ฆญํจ.
๋ค์์ฒ๋ผ ์๋ฎฌ๋ ์ดํฐ ์ค์ ์ฐฝ์ด ๋จ๋ฉด ์ผ์ชฝ ๋ฉ๋ด์์ Location
์ ์ ํํ๊ณ ์ง๋์์ ์์์ ์ง์ ์ ํด๋ฆญํจ. ๊ทธ๋ฆฌ๊ณ ์ง๋ ์๋์ SAVE POINT
๋ฅผ ํด๋ฆญํ ํ Set Location
์ ํด๋ฆญํ๋ฉด ์ง๋์์ ์ ํํ ์ง์ ์ ์์น๊ฐ ํ์ฌ ๊ธฐ๊ธฐ์ ์์น๋ก ์ค์ ๋จ.
1๋จ๊ณ์์ ์์ฑํ main.dart
ํ์ผ์ ์๋๋ก์ด๋ ์๋ฎฌ๋ ์ดํฐ์์ ์คํํด ๊ฒฐ๊ณผ๋ฅผ ํ์ธํจ. ์ฑ์ ์คํํ๋ฉด ๋ค์ ๊ทธ๋ฆผ์ฒ๋ผ ๊ธฐ๊ธฐ์ ์์น ์ ๋ณด์ ์ ๊ทผ์ ํ์ฉํ ๊ฒ์ธ์ง ๋ฌป๋ ์ฐฝ์ด ๋ํ๋จ. ์ฌ๊ธฐ์ While using the app (์ฑ ์ฌ์ฉ ์ค์๋ง ํ์ฉ)
์ด๋ Only this time(์ด๋ฒ๋ง ํ์ฉ)
์ ํด๋ฆญํ๋ฉด ํ์ฌ ์์น๊ฐ ์ถ๋ ฅ๋๊ณ , Don't allow(ํ์ฉ ์ ํจ)
์ ํด๋ฆญํ๋ฉด 'permissions are denied' ๋ฉ์์ง๊ฐ ์ถ๋ ฅ๋จ.
iOS์ info.plist
ํ์ผ์ ํค๋ฅผ ์ ์ธํจ. ์ด ํ์ผ์ iOS/Runner
๋๋ ํฐ๋ฆฌ์ ์์. info.plist
ํ์ผ์ ์ด๊ณ ๋ง์ง๋ง ๋ถ๋ถ์ ๋ค์์ฒ๋ผ ํค๋ฅผ ๋ฑ๋กํจ
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
...(์๋ต)...
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
</dict>
</plist>
๊ทธ๋ฐ ๋ค์ iOS ์๋ฎฌ๋ ์ดํฐ๋ก ํ
์คํธํ๊ธฐ ์ํด ๊ฐ์์ ์์น ์ ๋ณด๋ฅผ ์ค์ ํจ. ์๋ฎฌ๋ ์ดํฐ์์ Features > Location > Custom Location
๋ฉ๋ด๋ฅผ ํด๋ฆญํจ.
1๋จ๊ณ์์ ์์ฑํ main.dart
ํ์ผ์ Xcode์ iOS ์๋ฎฌ๋ ์ดํฐ์์ ์คํํด ๊ฒฐ๊ณผ๋ฅผ ํ์ธํจ.
์ถ์ฒ: https://product.kyobobook.co.kr/detail/S000200832185
๊ฐ์ฌํฉ๋๋ค, ์ ์ฉํ์์ต๋๋ค!