728x90
예제를 찾아서 테스트를 하다보면 Android폰에서는 잘 동작되는데, 아이폰에서는 동작이 안되기도 한다.
그래서 양쪽에서 모두 동작하는 걸 찾아서 테스트하기가 쉽지 않다.
location 라이브러리를 추가해서 테스트해보니 Android폰에서는 잘 동작하는데 ios 에서는 설정을 잘못했는지 에러가 발생하여 포기했다. 그리고 geolocator 라이브러리를 활용하여 구현했다.
잘 동작되기는 하는데, onMapReady 보다 늦게 결과가 나와 화면에서 바로 현재 위치로 이동시키는 것은 아직 미해결 상태다.
pubspec.yaml
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
dio: ^5.4.0
json_annotation: ^4.8.1
freezed_annotation: ^2.4.1
retrofit: ^4.0.3
logger: ^2.0.2+1
flutter_riverpod: ^2.4.9
flutter_naver_map: ^1.1.2
geolocator: ^10.1.0
permission_handler: ^11.2.0
flutter_secure_storage: ^4.2.1
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
json_serializable: ^6.7.1
build_runner: ^2.4.8
freezed: ^2.4.6
retrofit_generator: ^8.0.6
|
AndroidManifest.xml 추가사항
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
|
ios 설정 추가사항은 https://link2me.tistory.com/2381 게시글의 ios를 참조하면 된다.
import 'package:geolocator/geolocator.dart';
class LocationService {
double latitude = 0;
double longitude = 0;
Future<void> getCurrentLocation() async {
LocationPermission permission = await Geolocator.checkPermission();
// print(permission);
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
}
try {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
latitude = position.latitude;
longitude = position.longitude;
} catch (e) {
print(e);
}
}
}
|
import 'package:flutter/material.dart';
import 'package:flutter_naver_map/flutter_naver_map.dart';
import 'package:geolocator/geolocator.dart';
import 'package:location/location.dart';
import 'package:nmap_test/core/component/default_layout.dart';
import 'package:nmap_test/core/res/palette.dart';
import 'package:nmap_test/presentation/view/location_service.dart';
import 'package:permission_handler/permission_handler.dart';
class NaverMapScreen extends StatefulWidget {
const NaverMapScreen({Key? key}) : super(key: key);
@override
State<NaverMapScreen> createState() => _NaverMapScreenState();
}
class _NaverMapScreenState extends State<NaverMapScreen> {
final location = LocationService();
void _getLocation() async {
await location.getCurrentLocation();
double lat = location.latitude;
double lng = location.longitude;
print('lat:: $lat lng::: $lng');
}
@override
void initState() {
super.initState();
_permission();
WidgetsBinding.instance.addPostFrameCallback((_) {
_getLocation();
});
}
void _permission() async {
var requestStatus = await Permission.location.request();
var status = await Permission.location.status;
if (requestStatus.isPermanentlyDenied || status.isPermanentlyDenied) {
openAppSettings();
}
}
@override
Widget build(BuildContext context) {
return DefaultLayout(
title: 'Naver Map',
appbarBgColor: Palette.primary,
child: NaverMap(
options: const NaverMapViewOptions(locationButtonEnable: true),
onMapReady: (controller) {
print('네이버맵 로딩됨');
},
),
);
}
}
|
Android 에서만 동작이 되는 걸 성공한 코드도 적어둔다.
location: ^5.0.3
class _NaverMapScreenState extends State<NaverMapScreen> {
final Location location = Location();
late double lat;
late double lng;
late LocationData _locationData;
late bool _serviceEnabled;
void _getCurrentLocation() async {
_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await location.requestService();
if (!_serviceEnabled) {
return;
}
}
_locationData = await location.getLocation();
setState(() {
lat = _locationData.latitude!;
lng = _locationData.longitude!;
});
print('lat:: $lat lng::: $lng');
}
@override
void initState() {
super.initState();
_permission();
_getCurrentLocation();
}
void _permission() async {
var requestStatus = await Permission.location.request();
var status = await Permission.location.status;
if (requestStatus.isPermanentlyDenied || status.isPermanentlyDenied) {
openAppSettings();
}
}
|
728x90
'Flutter 앱 > 활용예제' 카테고리의 다른 글
Flutter Image Upload(플러터 파일 서버 업로드) (2) | 2024.02.16 |
---|---|
Flutter QR Code Scan (플러터 QR코드 스캔) (0) | 2024.02.08 |
flutter 네이버 지도 사용하기 (0) | 2024.01.30 |
Flutter News App using riverpod (2) | 2024.01.25 |
Flutter Retrofit GET, POST 예제 구현 (0) | 2023.12.22 |