2022-02-15 11:52:19 +00:00
|
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
2022-02-09 16:06:37 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-02-09 16:49:50 +00:00
|
|
|
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
2022-02-14 14:40:18 +00:00
|
|
|
import 'package:leg_barkr_app/service/map_service.dart';
|
2022-02-15 11:06:39 +00:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2022-02-09 16:06:37 +00:00
|
|
|
|
2022-02-09 16:49:50 +00:00
|
|
|
class MapPage extends StatefulWidget {
|
2022-02-09 16:06:37 +00:00
|
|
|
const MapPage({ Key? key }) : super(key: key);
|
|
|
|
|
2022-02-09 16:49:50 +00:00
|
|
|
@override
|
|
|
|
_MapPageState createState() => _MapPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MapPageState extends State<MapPage> {
|
|
|
|
late GoogleMapController _mapController;
|
2022-02-14 14:40:18 +00:00
|
|
|
final Map<String, Marker> _markers = {};
|
|
|
|
|
|
|
|
Future<void> _onMapCreated(GoogleMapController controller) async {
|
2022-02-14 16:59:33 +00:00
|
|
|
_mapController = controller;
|
2022-02-15 11:06:39 +00:00
|
|
|
final prefs = await SharedPreferences.getInstance();
|
2022-02-15 11:52:19 +00:00
|
|
|
final user = await FirebaseAuth.instance.currentUser!;
|
|
|
|
final String token = await user.getIdToken();
|
2022-02-15 11:06:39 +00:00
|
|
|
final String deviceId = prefs.getString("current_device") ?? "";
|
2022-02-15 11:52:19 +00:00
|
|
|
final lastLocation = await MapService().getPetLastLocation(deviceId, token);
|
2022-02-14 16:59:33 +00:00
|
|
|
final myLocation = await MapService().getMyLocation();
|
2022-02-15 11:06:39 +00:00
|
|
|
|
2022-02-14 14:40:18 +00:00
|
|
|
setState(() {
|
|
|
|
_markers.clear();
|
2022-02-14 17:03:32 +00:00
|
|
|
final petMarker = Marker(
|
|
|
|
markerId: MarkerId("pet_location"),
|
|
|
|
position: LatLng(lastLocation.latitude, lastLocation.longitude),
|
|
|
|
infoWindow: InfoWindow(title: "Pet location"));
|
2022-02-14 16:59:33 +00:00
|
|
|
|
2022-02-14 17:03:32 +00:00
|
|
|
final myMarker = Marker(
|
|
|
|
markerId: MarkerId("my_location"),
|
|
|
|
position: LatLng(myLocation.latitude, myLocation.longitude),
|
|
|
|
infoWindow: InfoWindow(title: "My location"));
|
|
|
|
|
|
|
|
_markers["pet_location"] = petMarker;
|
|
|
|
_markers["my_location"] = myMarker;
|
2022-02-14 16:59:33 +00:00
|
|
|
_mapController.animateCamera(CameraUpdate.newLatLng(LatLng(myLocation.latitude, myLocation.longitude)));
|
2022-02-14 14:40:18 +00:00
|
|
|
});
|
2022-02-09 16:49:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 16:06:37 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-02-09 16:49:50 +00:00
|
|
|
return MaterialApp(
|
|
|
|
home: Scaffold(
|
|
|
|
body: GoogleMap(
|
|
|
|
onMapCreated: _onMapCreated,
|
|
|
|
initialCameraPosition: CameraPosition(
|
2022-02-14 16:59:33 +00:00
|
|
|
target: LatLng(51.5, -0.12),
|
|
|
|
zoom: 12.0,
|
2022-02-09 16:49:50 +00:00
|
|
|
),
|
2022-02-14 14:40:18 +00:00
|
|
|
markers: _markers.values.toSet(),
|
2022-02-09 16:49:50 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2022-02-09 16:06:37 +00:00
|
|
|
}
|
|
|
|
}
|