Skip to content

Commit

Permalink
Merge pull request #188 from GreenTeaProgrammers/feature/frontend/gua…
Browse files Browse the repository at this point in the history
…rdian/map-page-api

feat: マップビューのAPIとの通信を実装
  • Loading branch information
koto623 authored Feb 23, 2024
2 parents f14fbab + e35c322 commit bbc3649
Show file tree
Hide file tree
Showing 20 changed files with 1,372 additions and 661 deletions.

Large diffs are not rendered by default.

43 changes: 40 additions & 3 deletions frontend/where_child_bus_guardian/lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import 'dart:developer' as developer;
import 'package:flutter/material.dart';
import 'package:where_child_bus_api/proto-gen/where_child_bus/v1/resources.pb.dart';
import 'package:where_child_bus_guardian/pages/check_page/check_page.dart';
import 'package:where_child_bus_guardian/pages/daily_page/daily_page.dart';
import 'package:where_child_bus_guardian/pages/map_page/map_page.dart';
import 'package:where_child_bus_guardian/pages/stop_bus_page/stop_bus_page.dart';
import 'package:where_child_bus_guardian/service/get_running_bus_by_guardian_id.dart';
import 'package:where_child_bus_guardian/util/guardian_data.dart';
import 'package:where_child_bus_api/proto-gen/where_child_bus/v1/resources.pb.dart';
import 'package:where_child_bus_guardian/components/utils/half_circle_painter.dart';
import 'package:where_child_bus_guardian/components/utils/custom_app_bar.dart';
import 'package:where_child_bus_guardian/components/utils/custom_bottom_app_bar.dart';
Expand All @@ -16,6 +21,36 @@ class App extends StatefulWidget {

class _AppState extends State<App> {
int _selectedIndex = 0;
GuardianResponse guardian = GuardianResponse();
bool _hasRunningBus = false;

@override
void initState() {
super.initState();
_initializeAppState();
}

Future<void> _initializeAppState() async {
guardian = GuardianData().getGuardian();
await _loadRunningBus();
}

Future<void> _loadRunningBus() async {
try {
var busRes = await getRunningBusByGuardianIdService(guardian.id);
if (mounted) {
setState(() {
_hasRunningBus = true;
});
}
} catch (e) {
if (mounted) {
setState(() {
_hasRunningBus = false;
});
}
}
}

@override
Widget build(BuildContext context) {
Expand All @@ -25,9 +60,11 @@ class _AppState extends State<App> {
),
body: Padding(
padding: EdgeInsets.only(top: 30),
child: IndexedStack(
index: _selectedIndex,
children: [DailyPage(), const MapPage(), CheckPage()])),
child: IndexedStack(index: _selectedIndex, children: [
DailyPage(),
_hasRunningBus ? MapPage() : StopBusPage(),
CheckPage(),
])),
bottomNavigationBar: CustomWaveBottomBar(
selectedIndex: _selectedIndex,
onItemTapped: (index) => setState(() => _selectedIndex = index),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'dart:developer' as developer;
import 'dart:async';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import '../../pages/map_page/map_page.dart';
import 'package:where_child_bus_guardian/util/google_map_manager.dart';

class GoogleMapView extends StatefulWidget {
final List<Waypoint> waypoints;
Expand All @@ -26,26 +28,36 @@ class _GoogleMapView extends State<GoogleMapView> {
const CameraPosition(target: LatLng(0.0, 0.0), zoom: 14);

late GoogleMapController mapController;

Timer? _timer;
Map<MarkerId, Marker> markers = {};
Map<PolylineId, Polyline> polylines = {};
List<LatLng> polylineCoordinates = [];
PolylinePoints polylinePoints = PolylinePoints();
String googleApiKey = dotenv.get("GOOGLE_MAP_API_KEY");

@override
void initState() {
super.initState();
_addMarker(LatLng(widget.nurseryLatitude, widget.nurseryLongitude), "保育園",
BitmapDescriptor.defaultMarker);

_addMarker(LatLng(widget.busLatitude, widget.busLongitude), "バス",
BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueBlue));
if (mounted) {
setState(() {
_addMarker(LatLng(widget.nurseryLatitude, widget.nurseryLongitude),
"保育園", BitmapDescriptor.defaultMarker);

widget.waypoints.forEach((Waypoint waypoint) {
_addMarker(LatLng(waypoint.latitude, waypoint.longitude), waypoint.name,
BitmapDescriptor.defaultMarkerWithHue(90));
});
_addMarker(LatLng(widget.busLatitude, widget.busLongitude), "バス",
BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueBlue));

widget.waypoints.forEach((Waypoint waypoint) {
_addMarker(LatLng(waypoint.latitude, waypoint.longitude),
waypoint.name, BitmapDescriptor.defaultMarkerWithHue(90));
});
});
}
}

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

@override
Expand All @@ -55,83 +67,67 @@ class _GoogleMapView extends State<GoogleMapView> {
child: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.5,
child: GoogleMap(
initialCameraPosition: _initialLocation,
mapType: MapType.normal,
onMapCreated: (GoogleMapController controller) {
mapController = controller;
_getPolyline(widget.waypoints);
},
markers: Set<Marker>.of(markers.values),
polylines: Set<Polyline>.of(polylines.values),
))));
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.5,
child: googleMapBody(),
)));
}

_addMarker(LatLng position, String id, BitmapDescriptor descriptor) {
Widget googleMapBody() {
_addMarker(LatLng(widget.busLatitude, widget.busLongitude), "バス",
BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueBlue));

return GoogleMap(
initialCameraPosition: _initialLocation,
mapType: MapType.normal,
onMapCreated: (GoogleMapController controller) {
mapController = controller;
_getPolyline(widget.waypoints);
},
markers: Set<Marker>.of(markers.values),
polylines: Set<Polyline>.of(polylines.values),
);
}

void _addMarker(LatLng position, String id, BitmapDescriptor descriptor) {
MarkerId markerId = MarkerId(id);
Marker marker =
Marker(markerId: markerId, icon: descriptor, position: position);
markers[markerId] = marker;
setState(() {
markers[markerId] = marker;
});
}

_addPolyline() {
void _addPolyline() {
PolylineId id = const PolylineId('poly');
Polyline polyline = Polyline(
polylineId: id,
color: Colors.blue,
points: polylineCoordinates,
width: 5);
polylines[id] = polyline;
setState(() {});
setState(() {
polylines[id] = polyline;
});
}

_getPolyline(List<Waypoint> waypoints) async {
List<PolylineWayPoint> polylineWayPoints = waypoints
.map((waypoint) => PolylineWayPoint(
location: "${waypoint.latitude},${waypoint.longitude}"))
.toList();
void _getPolyline(List<Waypoint> waypoints) async {
try {
PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
googleApiKey,
PointLatLng(widget.nurseryLatitude, widget.nurseryLongitude),
PointLatLng(widget.nurseryLatitude, widget.nurseryLongitude),
travelMode: TravelMode.driving,
avoidTolls: true,
avoidHighways: false,
avoidFerries: false,
wayPoints: polylineWayPoints,
polylineCoordinates = await GoogleMapAPIManager().getPolylinePoints(
startLat: widget.nurseryLatitude.toString(),
startLng: widget.nurseryLongitude.toString(),
endLat: widget.nurseryLatitude.toString(),
endLng: widget.nurseryLongitude.toString(),
waypoints: waypoints,
);

if (result.points.isNotEmpty) {
result.points.forEach((PointLatLng point) {
polylineCoordinates.add(LatLng(point.latitude, point.longitude));
});
}
developer.log("polylineCoordinates: $polylineCoordinates",
name: "GetPolyline");
_addPolyline();
} catch (e) {
print(e);
}

if (polylineCoordinates.isNotEmpty) {
double minLat = polylineCoordinates.first.latitude;
double maxLat = polylineCoordinates.first.latitude;
double minLng = polylineCoordinates.first.longitude;
double maxLng = polylineCoordinates.first.longitude;

for (LatLng point in polylineCoordinates) {
if (point.latitude < minLat) minLat = point.latitude;
if (point.latitude > maxLat) maxLat = point.latitude;
if (point.longitude < minLng) minLng = point.longitude;
if (point.longitude > maxLng) maxLng = point.longitude;
}

LatLngBounds bounds = LatLngBounds(
southwest: LatLng(minLat, minLng),
northeast: LatLng(maxLat, maxLng),
);
mapController.animateCamera(CameraUpdate.newLatLngBounds(bounds, 100));
}
mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
target: LatLng(widget.nurseryLatitude, widget.nurseryLongitude),
zoom: 14)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ class _AuthPageState extends State<AuthPage> {
}
} catch (error) {
developer.log("Login Failed for error: ", error: error);
setState(() => _loginError = GuardianLoginError.invalidCredentials);
if (mounted) {
setState(() => _loginError = GuardianLoginError.invalidCredentials);
}
}
}

Expand Down
Loading

0 comments on commit bbc3649

Please sign in to comment.