class _MyHomePageState extends State<MyHomePage> {
late Controller _mapController1;
late Controller _mapController2;
TaskHandler? _routingHandler1;
TaskHandler? _routingHandler2;
@override
void dispose() {
GemKit.release();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple[900],
title: const Text(
'Multi Map Routing',
style: TextStyle(color: Colors.white),
),
leading: IconButton(
onPressed: _removeRoutes,
icon: const Icon(Icons.close, color: Colors.white),
),
actions: [
IconButton(
onPressed: () => _onBuildRouteButtonPressed(true),
icon: const Icon(Icons.route, color: Colors.white),
),
IconButton(
onPressed: () => _onBuildRouteButtonPressed(false),
icon: const Icon(Icons.route, color: Colors.white),
),
],
),
body: Column(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: GemMap(key: ValueKey("GemMap"), onMapCreated: _onMap1Created, appAuthorization: projectApiToken),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: GemMap(onMapCreated: _onMap2Created),
),
),
],
),
);
}
void _showSnackBar(BuildContext context,
{required String message, Duration duration = const Duration(hours: 1)}) {
final snackBar = SnackBar(
content: Text(message),
duration: duration,
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
void _onMap1Created(GemMapController controller) {
_mapController1 = controller;
}
void _onMap2Created(GemMapController controller) {
_mapController2 = controller;
}
void _onBuildRouteButtonPressed(bool isFirstMap) {
final waypoints = <Landmark>[];
if (isFirstMap) {
final departure =
Landmark.withLatLng(latitude: 37.77903, longitude: -122.41991);
final destination =
Landmark.withLatLng(latitude: 37.33619, longitude: -121.89058);
waypoints.add(departure);
waypoints.add(destination);
} else {
final departure =
Landmark.withLatLng(latitude: 51.50732, longitude: -0.12765);
final destination =
Landmark.withLatLng(latitude: 51.27483, longitude: 0.52316);
waypoints.add(departure);
waypoints.add(destination);
}
final routePreferences = RoutePreferences();
_showSnackBar(context,
message: isFirstMap
? 'The first route is calculating.'
: 'The second route is calculating.');
if (isFirstMap) {
_routingHandler1 = RoutingService.calculateRoute(
waypoints,
routePreferences,
(err, routes) => _onRouteBuiltFinished(err, routes, true));
} else {
_routingHandler2 = RoutingService.calculateRoute(
waypoints,
routePreferences,
(err, routes) => _onRouteBuiltFinished(err, routes, false));
}
}
void _onRouteBuiltFinished(
GemError err,
List<Route>? routes,
bool isFirstMap,
) {
if (isFirstMap) {
_routingHandler1 = null;
} else {
_routingHandler2 = null;
}
ScaffoldMessenger.of(context).clearSnackBars();
if (_routingHandler1 != null) {
_showSnackBar(context, message: 'The first route is calculating.');
}
if (_routingHandler2 != null) {
_showSnackBar(context, message: 'The second route is calculating.');
}
if (err == GemError.success) {
final routesMap =
(isFirstMap
? _mapController1.preferences
: _mapController2.preferences)
.routes;
for (final route in routes!) {
routesMap.add(route, route == routes.first, label: route.getMapLabel());
}
if (isFirstMap) {
_mapController1.centerOnRoutes(routes: routes);
} else {
_mapController2.centerOnRoutes(routes: routes);
}
}
}
void _removeRoutes() {
if (_routingHandler1 != null) {
RoutingService.cancelRoute(_routingHandler1!);
_routingHandler1 = null;
}
if (_routingHandler2 != null) {
RoutingService.cancelRoute(_routingHandler2!);
_routingHandler2 = null;
}
_mapController1.preferences.routes.clear();
_mapController2.preferences.routes.clear();
}
}