Skip to content

Commit 66acaa8

Browse files
authored
[google_maps_flutter_web] Issue 64073 implement my location (flutter#12186)
Implement my location button, add blue dot location Fixes flutter/flutter#64073 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 4a08a54 commit 66acaa8

14 files changed

Lines changed: 644 additions & 7 deletions

‎packages/google_maps_flutter/google_maps_flutter_web/AUTHORS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ Alex Li <[email protected]>
6666
Rahul Raj <[email protected]>
6767
Justin Baumann <[email protected]>
6868
Joonas Kerttula <[email protected]>
69+
Zubsacu Ionut <[email protected]>

‎packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.6.5
2+
3+
* Adds support for `myLocationEnabled` and `myLocationButtonEnabled`.
4+
15
## 0.6.4+2
26

37
* Fixes `onMapCreated` never being called when a map tile request stalls or fails.

‎packages/google_maps_flutter/google_maps_flutter_web/README.md‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,6 @@ The following map options are not available in web, because the map doesn't rota
100100

101101
There's no "Map Toolbar" in web, so the `mapToolbarEnabled` option is unused.
102102

103-
There's no "My Location" widget in web ([tracking issue](https://github.com/flutter/flutter/issues/64073)), so the following options are ignored, for now:
104-
105-
* `myLocationButtonEnabled`
106-
* `myLocationEnabled`
107-
108103
There's no `defaultMarkerWithHue` in web. If you need colored pins/markers, you may need to use your own asset images.
109104

110105
Indoor and building layers are still not available on the web. Traffic is.
1.71 KB
Loading
1.54 KB
Loading

‎packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/google_maps_controller_test.dart‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ gmaps.Map mapShim() => throw UnimplementedError();
3333
),
3434
MockSpec<TileOverlaysController>(fallbackGenerators: <Symbol, Function>{#googleMap: mapShim}),
3535
MockSpec<GroundOverlaysController>(fallbackGenerators: <Symbol, Function>{#googleMap: mapShim}),
36+
MockSpec<MyLocationController>(fallbackGenerators: <Symbol, Function>{#googleMap: mapShim}),
37+
MockSpec<GeolocationApi>(fallbackGenerators: <Symbol, Function>{#googleMap: mapShim}),
3638
])
3739
/// Test Google Map Controller
3840
void main() {
@@ -1146,5 +1148,112 @@ void main() {
11461148
verify(mock.isInfoWindowShown(markerId));
11471149
});
11481150
});
1151+
1152+
group('My Location', () {
1153+
late MockMyLocationController mockMyLocationController;
1154+
late MockGeolocationApi mockGeolocationApi;
1155+
1156+
setUp(() {
1157+
mockMyLocationController = MockMyLocationController();
1158+
mockGeolocationApi = MockGeolocationApi();
1159+
when(mockGeolocationApi.isAvailable).thenReturn(true);
1160+
});
1161+
1162+
testWidgets('by default is disabled', (WidgetTester tester) async {
1163+
controller = createController();
1164+
controller.init();
1165+
expect(mockMyLocationController.myLocationButton, isNull);
1166+
});
1167+
1168+
testWidgets('does not show my location button when geolocation is unavailable', (
1169+
WidgetTester tester,
1170+
) async {
1171+
when(mockGeolocationApi.isAvailable).thenReturn(false);
1172+
final map = gmaps.Map(createDivElement());
1173+
final myLocationController = MyLocationController(geolocationApi: mockGeolocationApi);
1174+
1175+
controller = createController(
1176+
mapConfiguration: const MapConfiguration(
1177+
myLocationEnabled: true,
1178+
myLocationButtonEnabled: true,
1179+
),
1180+
);
1181+
controller.debugSetOverrides(createMap: (_, _) => map, myLocation: myLocationController);
1182+
1183+
controller.init();
1184+
await tester.pumpAndSettle();
1185+
1186+
expect(myLocationController.myLocationButton, isNull);
1187+
expect(map.getControlsAt(gmaps.ControlPosition.RIGHT_BOTTOM).length, equals(0));
1188+
});
1189+
1190+
testWidgets('initializes with my location & display my location button', (
1191+
WidgetTester tester,
1192+
) async {
1193+
const currentLocation = LatLng(10.8231, 106.6297);
1194+
final map = gmaps.Map(createDivElement());
1195+
1196+
controller = createController(
1197+
mapConfiguration: const MapConfiguration(
1198+
myLocationEnabled: true,
1199+
myLocationButtonEnabled: true,
1200+
),
1201+
);
1202+
1203+
final myLocationController = MyLocationController(geolocationApi: mockGeolocationApi);
1204+
1205+
controller.debugSetOverrides(createMap: (_, _) => map, myLocation: myLocationController);
1206+
1207+
when(mockGeolocationApi.watchPosition(any, any)).thenAnswer((inv) {
1208+
final onSuccess = inv.positionalArguments[0] as void Function(double, double);
1209+
onSuccess(currentLocation.latitude, currentLocation.longitude);
1210+
return 0;
1211+
});
1212+
when(
1213+
mockGeolocationApi.getCurrentPosition(any, any, timeoutMs: anyNamed('timeoutMs')),
1214+
).thenAnswer((inv) {
1215+
final onSuccess = inv.positionalArguments[0] as void Function(double, double);
1216+
onSuccess(currentLocation.latitude, currentLocation.longitude);
1217+
});
1218+
1219+
controller.init();
1220+
await tester.pumpAndSettle();
1221+
1222+
expect(map.getControlsAt(gmaps.ControlPosition.RIGHT_BOTTOM).length, equals(1));
1223+
});
1224+
1225+
testWidgets('initializes with my location only', (WidgetTester tester) async {
1226+
final map = gmaps.Map(createDivElement());
1227+
const currentLocation = LatLng(10.8231, 106.6297);
1228+
1229+
controller = createController(
1230+
mapConfiguration: const MapConfiguration(
1231+
myLocationEnabled: true,
1232+
myLocationButtonEnabled: false,
1233+
),
1234+
);
1235+
1236+
final myLocationController = MyLocationController(geolocationApi: mockGeolocationApi);
1237+
1238+
controller.debugSetOverrides(createMap: (_, _) => map, myLocation: myLocationController);
1239+
1240+
when(mockGeolocationApi.watchPosition(any, any)).thenAnswer((inv) {
1241+
final onSuccess = inv.positionalArguments[0] as void Function(double, double);
1242+
onSuccess(currentLocation.latitude, currentLocation.longitude);
1243+
return 0;
1244+
});
1245+
when(
1246+
mockGeolocationApi.getCurrentPosition(any, any, timeoutMs: anyNamed('timeoutMs')),
1247+
).thenAnswer((inv) {
1248+
final onSuccess = inv.positionalArguments[0] as void Function(double, double);
1249+
onSuccess(currentLocation.latitude, currentLocation.longitude);
1250+
});
1251+
1252+
controller.init();
1253+
await tester.pumpAndSettle();
1254+
1255+
expect(map.getControlsAt(gmaps.ControlPosition.RIGHT_BOTTOM).length, equals(0));
1256+
});
1257+
});
11491258
});
11501259
}

‎packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/google_maps_controller_test.mocks.dart‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,89 @@ class MockGroundOverlaysController extends _i1.Mock implements _i2.GroundOverlay
498498
returnValueForMissingStub: null,
499499
);
500500
}
501+
502+
/// A class which mocks [MyLocationController].
503+
///
504+
/// See the documentation for Mockito's code generation for more information.
505+
class MockMyLocationController extends _i1.Mock implements _i2.MyLocationController {
506+
@override
507+
set myLocationButton(_i2.MyLocationButton? value) => super.noSuchMethod(
508+
Invocation.setter(#myLocationButton, value),
509+
returnValueForMissingStub: null,
510+
);
511+
512+
@override
513+
void displayAndWatchMyLocation(_i4.Map? map) => super.noSuchMethod(
514+
Invocation.method(#displayAndWatchMyLocation, [map]),
515+
returnValueForMissingStub: null,
516+
);
517+
518+
@override
519+
_i6.Future<void> centerMyCurrentLocation(_i2.GoogleMapController? controller) =>
520+
(super.noSuchMethod(
521+
Invocation.method(#centerMyCurrentLocation, [controller]),
522+
returnValue: _i6.Future<void>.value(),
523+
returnValueForMissingStub: _i6.Future<void>.value(),
524+
)
525+
as _i6.Future<void>);
526+
527+
@override
528+
void addMyLocationButton(_i4.Map? map, _i2.GoogleMapController? controller) => super.noSuchMethod(
529+
Invocation.method(#addMyLocationButton, [map, controller]),
530+
returnValueForMissingStub: null,
531+
);
532+
533+
@override
534+
void removeMyLocationButton(_i4.Map? map) => super.noSuchMethod(
535+
Invocation.method(#removeMyLocationButton, [map]),
536+
returnValueForMissingStub: null,
537+
);
538+
539+
@override
540+
void removeBlueDot() =>
541+
super.noSuchMethod(Invocation.method(#removeBlueDot, []), returnValueForMissingStub: null);
542+
543+
@override
544+
void stopWatchingMyLocation() => super.noSuchMethod(
545+
Invocation.method(#stopWatchingMyLocation, []),
546+
returnValueForMissingStub: null,
547+
);
548+
}
549+
550+
/// A class which mocks [GeolocationApi].
551+
///
552+
/// See the documentation for Mockito's code generation for more information.
553+
class MockGeolocationApi extends _i1.Mock implements _i2.GeolocationApi {
554+
@override
555+
bool get isAvailable =>
556+
(super.noSuchMethod(
557+
Invocation.getter(#isAvailable),
558+
returnValue: false,
559+
returnValueForMissingStub: false,
560+
)
561+
as bool);
562+
563+
@override
564+
int? watchPosition(void Function(double, double)? onSuccess, void Function(dynamic)? onError) =>
565+
(super.noSuchMethod(
566+
Invocation.method(#watchPosition, [onSuccess, onError]),
567+
returnValueForMissingStub: null,
568+
)
569+
as int?);
570+
571+
@override
572+
void getCurrentPosition(
573+
void Function(double, double)? onSuccess,
574+
void Function(dynamic)? onError, {
575+
int? timeoutMs = 30000,
576+
}) => super.noSuchMethod(
577+
Invocation.method(#getCurrentPosition, [onSuccess, onError], {#timeoutMs: timeoutMs}),
578+
returnValueForMissingStub: null,
579+
);
580+
581+
@override
582+
void clearWatch(int? watchId) => super.noSuchMethod(
583+
Invocation.method(#clearWatch, [watchId]),
584+
returnValueForMissingStub: null,
585+
);
586+
}

‎packages/google_maps_flutter/google_maps_flutter_web/example/latest/integration_test/google_maps_plugin_test.mocks.dart‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class MockGoogleMapController extends _i1.Mock implements _i4.GoogleMapControlle
120120
_i6.ClusterManagersController<Object?>? clusterManagers,
121121
_i4.TileOverlaysController? tileOverlays,
122122
_i4.GroundOverlaysController? groundOverlays,
123+
_i4.MyLocationController? myLocation,
123124
}) => super.noSuchMethod(
124125
Invocation.method(#debugSetOverrides, [], {
125126
#createMap: createMap,
@@ -132,6 +133,7 @@ class MockGoogleMapController extends _i1.Mock implements _i4.GoogleMapControlle
132133
#clusterManagers: clusterManagers,
133134
#tileOverlays: tileOverlays,
134135
#groundOverlays: groundOverlays,
136+
#myLocation: myLocation,
135137
}),
136138
returnValueForMissingStub: null,
137139
);
@@ -297,3 +299,51 @@ class MockGoogleMapController extends _i1.Mock implements _i4.GoogleMapControlle
297299
void dispose() =>
298300
super.noSuchMethod(Invocation.method(#dispose, []), returnValueForMissingStub: null);
299301
}
302+
303+
/// A class which mocks [MyLocationController].
304+
///
305+
/// See the documentation for Mockito's code generation for more information.
306+
class MockMyLocationController extends _i1.Mock implements _i4.MyLocationController {
307+
@override
308+
set myLocationButton(_i4.MyLocationButton? value) => super.noSuchMethod(
309+
Invocation.setter(#myLocationButton, value),
310+
returnValueForMissingStub: null,
311+
);
312+
313+
@override
314+
void displayAndWatchMyLocation(_i5.Map? map) => super.noSuchMethod(
315+
Invocation.method(#displayAndWatchMyLocation, [map]),
316+
returnValueForMissingStub: null,
317+
);
318+
319+
@override
320+
_i3.Future<void> centerMyCurrentLocation(_i4.GoogleMapController? controller) =>
321+
(super.noSuchMethod(
322+
Invocation.method(#centerMyCurrentLocation, [controller]),
323+
returnValue: _i3.Future<void>.value(),
324+
returnValueForMissingStub: _i3.Future<void>.value(),
325+
)
326+
as _i3.Future<void>);
327+
328+
@override
329+
void addMyLocationButton(_i5.Map? map, _i4.GoogleMapController? controller) => super.noSuchMethod(
330+
Invocation.method(#addMyLocationButton, [map, controller]),
331+
returnValueForMissingStub: null,
332+
);
333+
334+
@override
335+
void removeMyLocationButton(_i5.Map? map) => super.noSuchMethod(
336+
Invocation.method(#removeMyLocationButton, [map]),
337+
returnValueForMissingStub: null,
338+
);
339+
340+
@override
341+
void removeBlueDot() =>
342+
super.noSuchMethod(Invocation.method(#removeBlueDot, []), returnValueForMissingStub: null);
343+
344+
@override
345+
void stopWatchingMyLocation() => super.noSuchMethod(
346+
Invocation.method(#stopWatchingMyLocation, []),
347+
returnValueForMissingStub: null,
348+
);
349+
}

‎packages/google_maps_flutter/google_maps_flutter_web/lib/google_maps_flutter_web.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ library google_maps_flutter_web;
77
import 'dart:async';
88
import 'dart:convert';
99
import 'dart:js_interop';
10+
import 'dart:js_interop_unsafe';
1011
import 'dart:ui_web' as ui_web;
1112

1213
import 'package:collection/collection.dart';
@@ -34,6 +35,7 @@ import 'src/utils.dart';
3435
part 'src/circle.dart';
3536
part 'src/circles.dart';
3637
part 'src/convert.dart';
38+
part 'src/geolocation_api.dart';
3739
part 'src/google_maps_controller.dart';
3840
part 'src/google_maps_flutter_web.dart';
3941
part 'src/ground_overlay.dart';
@@ -42,6 +44,7 @@ part 'src/heatmap.dart';
4244
part 'src/heatmaps.dart';
4345
part 'src/marker.dart';
4446
part 'src/markers.dart';
47+
part 'src/my_location.dart';
4548
part 'src/overlay.dart';
4649
part 'src/overlays.dart';
4750
part 'src/polygon.dart';

0 commit comments

Comments
 (0)