ELEC60013-ES-CW1/App/lib/view/map/map_page.dart

60 lines
2 KiB
Dart
Raw Normal View History

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
2022-02-09 16:49:50 +00:00
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:leg_barkr_app/service/map_service.dart';
import 'package:shared_preferences/shared_preferences.dart';
2022-02-09 16:49:50 +00:00
class MapPage extends StatefulWidget {
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;
final Map<String, Marker> _markers = {};
Future<void> _onMapCreated(GoogleMapController controller) async {
2022-02-14 16:59:33 +00:00
_mapController = controller;
final prefs = await SharedPreferences.getInstance();
final user = await FirebaseAuth.instance.currentUser!;
final String token = await user.getIdToken();
final String deviceId = prefs.getString("current_device") ?? "";
final lastLocation = await MapService().getPetLastLocation(deviceId, token);
2022-02-14 16:59:33 +00:00
final myLocation = await MapService().getMyLocation();
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-09 16:49:50 +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
),
markers: _markers.values.toSet(),
2022-02-09 16:49:50 +00:00
),
),
);
}
}