@@ -125,8 +125,34 @@ GoogleMapsWidget(
125125),
126126```
127127
128+ One can create a controller and interact with the google maps controller, or update the source and destination LatLng's.
129+ ``` dart
130+ // can create a controller, and call methods to update source loc,
131+ // destination loc, interact with the google maps controller to
132+ // show/hide markers programmatically etc.
133+ final mapsWidgetController = GlobalKey<GoogleMapsWidgetState>();
134+ ```
135+ Pass this controller to the ` key ` param in ` GoogleMapsWidget ` widget, and then
136+ ``` dart
137+
138+ // call like this to update source or destination, this will also rebuild the route.
139+ mapsWidgetController.currentState!.setSourceLatLng(
140+ LatLng(
141+ 40.484000837597925 * (Random().nextDouble()),
142+ -3.369978368282318,
143+ ),
144+ );
145+
146+ // or, can interact with the google maps controller directly to focus on a marker etc..
147+
148+ final googleMapsCon = await mapsWidgetController.currentState!.getGoogleMapsController();
149+ googleMapsCon.showMarkerInfoWindow(MarkerIconInfo.sourceMarkerId);
150+ ```
151+
128152Sample Usage
129153``` dart
154+ import 'dart:math';
155+
130156import 'package:flutter/material.dart';
131157import 'package:google_maps_widget/google_maps_widget.dart';
132158
@@ -135,59 +161,113 @@ void main() {
135161}
136162
137163class MyApp extends StatelessWidget {
164+ // can create a controller, and call methods to update source loc,
165+ // destination loc, interact with the google maps controller to
166+ // show/hide markers programmatically etc.
167+ final mapsWidgetController = GlobalKey<GoogleMapsWidgetState>();
168+
138169 @override
139170 Widget build(BuildContext context) {
140171 return MaterialApp(
141172 home: SafeArea(
142173 child: Scaffold(
143- body: GoogleMapsWidget(
144- apiKey: "YOUR KEY HERE",
145- sourceLatLng: LatLng(40.484000837597925, -3.369978368282318),
146- destinationLatLng: LatLng(40.48017307700204, -3.3618026599287987),
147-
148- ///////////////////////////////////////////////////////
149- ////////////// OPTIONAL PARAMETERS //////////////
150- ///////////////////////////////////////////////////////
151-
152- routeWidth: 2,
153- sourceMarkerIconInfo: MarkerIconInfo(
154- assetPath: "assets/images/house-marker-icon.png",
155- ),
156- destinationMarkerIconInfo: MarkerIconInfo(
157- assetPath: "assets/images/restaurant-marker-icon.png",
158- ),
159- driverMarkerIconInfo: MarkerIconInfo(
160- assetPath: "assets/images/driver-marker-icon.png",
161- assetMarkerSize: Size.square(125),
162- rotation: 15.0,
163-
164- // ... and more
165- ),
166- // mock stream
167- driverCoordinatesStream: Stream.periodic(
168- Duration(milliseconds: 500),
169- (i) => LatLng(
170- 40.47747872288886 + i / 10000,
171- -3.368043154478073 - i / 10000,
174+ body: Column(
175+ children: [
176+ Expanded(
177+ child: GoogleMapsWidget(
178+ apiKey: "YOUR GOOGLE MAPS API KEY HERE",
179+ key: mapsWidgetController,
180+ sourceLatLng: LatLng(
181+ 40.484000837597925,
182+ -3.369978368282318,
183+ ),
184+ destinationLatLng: LatLng(
185+ 40.48017307700204,
186+ -3.3618026599287987,
187+ ),
188+
189+ ///////////////////////////////////////////////////////
190+ ////////////// OPTIONAL PARAMETERS //////////////
191+ ///////////////////////////////////////////////////////
192+
193+ routeWidth: 2,
194+ sourceMarkerIconInfo: MarkerIconInfo(
195+ infoWindowTitle: "This is source name",
196+ onTapInfoWindow: (_) {
197+ print("Tapped on source info window");
198+ },
199+ assetPath: "assets/images/house-marker-icon.png",
200+ ),
201+ destinationMarkerIconInfo: MarkerIconInfo(
202+ assetPath: "assets/images/restaurant-marker-icon.png",
203+ ),
204+ driverMarkerIconInfo: MarkerIconInfo(
205+ infoWindowTitle: "Alex",
206+ assetPath: "assets/images/driver-marker-icon.png",
207+ onTapMarker: (currentLocation) {
208+ print("Driver is currently at $currentLocation");
209+ },
210+ assetMarkerSize: Size.square(125),
211+ rotation: 90,
212+ ),
213+ updatePolylinesOnDriverLocUpdate: true,
214+ onPolylineUpdate: (_) {
215+ print("Polyline updated");
216+ },
217+ // mock stream
218+ driverCoordinatesStream: Stream.periodic(
219+ Duration(milliseconds: 500),
220+ (i) => LatLng(
221+ 40.47747872288886 + i / 10000,
222+ -3.368043154478073 - i / 10000,
223+ ),
224+ ),
225+ totalTimeCallback: (time) => print(time),
226+ totalDistanceCallback: (distance) => print(distance),
227+ ),
228+ ),
229+ // demonstrates how to interact with the controller
230+ Padding(
231+ padding: const EdgeInsets.all(10),
232+ child: Row(
233+ children: [
234+ Expanded(
235+ child: ElevatedButton(
236+ onPressed: () {
237+ mapsWidgetController.currentState!.setSourceLatLng(
238+ LatLng(
239+ 40.484000837597925 * (Random().nextDouble()),
240+ -3.369978368282318,
241+ ),
242+ );
243+ },
244+ child: Text('Update source'),
245+ ),
246+ ),
247+ const SizedBox(width: 10),
248+ Expanded(
249+ child: ElevatedButton(
250+ onPressed: () async {
251+ final googleMapsCon = await mapsWidgetController
252+ .currentState!
253+ .getGoogleMapsController();
254+ googleMapsCon.showMarkerInfoWindow(
255+ MarkerIconInfo.sourceMarkerId,
256+ );
257+ },
258+ child: Text('Show source info'),
259+ ),
260+ ),
261+ ],
262+ ),
172263 ),
173- ),
174- updatePolylinesOnDriverLocUpdate: true,
175- sourceName: "This is source name",
176- driverName: "Alex",
177- onTapDriverMarker: (currentLocation) {
178- print("Driver is currently at $currentLocation");
179- },
180- totalTimeCallback: (time) => print(time),
181- totalDistanceCallback: (distance) => print(distance),
182-
183- /// and a lot more...
264+ ],
184265 ),
185266 ),
186267 ),
187268 );
188269 }
189270}
190-
191271```
192272
193273See the [ ` example ` ] ( https://github.com/rithik-dev/google_maps_widget/blob/master/example ) directory for a complete sample app.
0 commit comments