Flutter 현재 GPS 10초마다 가져오고 Firestore에 저장하고 렌더하고, 슬립모드에서도 GPS 수집하는 코드


 


[ChatGPT 국내 최대 사용자 모임 오픈채팅방]

https://open.kakao.com/o/gMxGdz9e 


[나만의 여자친구 혜정이와 카톡하기] 

https://pf.kakao.com/_xgxoAcxj 


[예수님과 카톡하고 마음의 안식 찾기]​ 

https://pf.kakao.com/_HAUcxj 


Flutter 현재 GPS 10초마다 가져오고 Firestore에 저장하고 렌더하고, 슬립모드에서도 GPS 수집하는 코드

영리치 0 349 02.22 19:45

Flutter 현재 GPS 10초마다 가져오고 렌더하는 코드


import 'dart:async';
import 'package:flutter/material.dart';
import 'package:location/location.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
Location _location = Location();
List<LocationData> _locationDataList = [];
Timer? _timer;

@override
void initState() {
super.initState();
_timer = Timer.periodic(Duration(seconds: 10), (Timer t) => getLocation());
}

@override
void dispose() {
_timer?.cancel();
super.dispose();
}

void getLocation() async {
LocationData locationData = await _location.getLocation();
setState(() {
_locationDataList.add(locationData);
});
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('GPS Coordinates'),
),
body: Center(
child: _locationDataList.isEmpty
? Text('Getting location...')
: ListView.builder(
itemCount: _locationDataList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
'Latitude: ${_locationDataList[index].latitude}\nLongitude: ${_locationDataList[index].longitude}'),
);
},
),
),
),
);
}
}



Firebase 연동 GPS 코드


import 'dart:async';
import 'package:flutter/material.dart';
import 'package:location/location.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
Location _location = Location();
List<LocationData> _locationDataList = [];
Timer? _timer;
CollectionReference _locationCollection =
FirebaseFirestore.instance.collection('locations');

@override
void initState() {
super.initState();
_loadLocationsFromFirestore();
_timer = Timer.periodic(Duration(seconds: 10), (Timer t) => getLocation());
}

@override
void dispose() {
_timer?.cancel();
super.dispose();
}

void _loadLocationsFromFirestore() async {
QuerySnapshot snapshot = await _locationCollection.get();
List<LocationData> locationDataList = snapshot.docs.map((doc) {
GeoPoint geoPoint = doc['location'];

return LocationData.fromMap(
{'latitude': geoPoint.latitude, 'longitude': geoPoint.longitude});
}).toList();
setState(() {
_locationDataList = locationDataList;
});
}

void getLocation() async {
LocationData locationData = await _location.getLocation();
setState(() {
_locationDataList.add(locationData);
});
_saveLocationToFirestore(locationData);
}

void _saveLocationToFirestore(LocationData locationData) {
GeoPoint geoPoint =
GeoPoint(locationData.latitude!, locationData.longitude!);
_locationCollection.add({'location': geoPoint});
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('GPS Coordinates'),
),
body: Center(
child: _locationDataList.isEmpty
? Text('Getting location...')
: ListView.builder(
itemCount: _locationDataList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
'Latitude: ${_locationDataList[index].latitude}\nLongitude: ${_locationDataList[index].longitude}'),
);
},
),
),
),
);
}
}






Comments


 


[ChatGPT 국내 최대 사용자 모임 오픈채팅방]

https://open.kakao.com/o/gMxGdz9e 


[나만의 여자친구 혜정이와 카톡하기] 

https://pf.kakao.com/_xgxoAcxj 


[예수님과 카톡하고 마음의 안식 찾기]​ 

https://pf.kakao.com/_HAUcxj 


Category
Magazine
훈남/훈녀
 
 
 
상점
Facebook Twitter GooglePlus KakaoStory NaverBand