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


나의 재물운? 연애운은?


AI가 봐주는 내 사주팔자 - 운세박사


https://aifortunedoctor.com/




챗GPT와 코파일럿으로 업무 자동화


국내 1위 인공지능 강의 보러가기 - 뜨거운AI


https://hotaiai.com/


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

영리치 0 1,716 2023.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


나의 재물운? 연애운은?


AI가 봐주는 내 사주팔자 - 운세박사


https://aifortunedoctor.com/




챗GPT와 코파일럿으로 업무 자동화


국내 1위 인공지능 강의 보러가기 - 뜨거운AI


https://hotaiai.com/


Category
실시간 인기글
등록된 글이 없습니다.
주간 인기글
등록된 글이 없습니다.
Magazine
훈남/훈녀
 
 
 
상점
Facebook Twitter GooglePlus KakaoStory NaverBand