Skip to content

[google_maps_flutter_web] Add "My Location" Widget #3274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ Aleksandr Yurkovskiy <sanekyy@gmail.com>
Anton Borries <mail@antonborri.es>
Alex Li <google@alexv525.com>
Rahul Raj <64.rahulraj@gmail.com>
Nguyễn Phúc Lợi <nploi1998@gmail.com>
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.1

* Add "My Location" Widget. Issue [#64073](https://github.com/flutter/flutter/issues/64073)

## 0.4.0+8

* Updates minimum Flutter version to 3.3.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import 'google_maps_controller_test.mocks.dart';
MockSpec<PolygonsController>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<PolylinesController>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<MarkersController>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<html.Geolocation>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<html.Geoposition>(onMissingStub: OnMissingStub.returnDefault),
MockSpec<html.Coordinates>(onMissingStub: OnMissingStub.returnDefault),
])

/// Test Google Map Controller
Expand All @@ -31,7 +34,6 @@ void main() {
const int mapId = 33930;
late GoogleMapController controller;
late StreamController<MapEvent<Object?>> stream;

// Creates a controller with the default mapId and stream controller, and any `options` needed.
GoogleMapController createController({
CameraPosition initialCameraPosition =
Expand Down Expand Up @@ -481,6 +483,143 @@ void main() {
expect(controller.trafficLayer, isNotNull);
});
});

group('My Location', () {
testWidgets('by default is disabled', (WidgetTester tester) async {
controller = createController();
controller.init();

expect(controller.myLocationButton, isNull);
});

testWidgets('initializes with my location & display my location button',
(WidgetTester tester) async {
late final MockGeolocation mockGeolocation = MockGeolocation();
late final MockGeoposition mockGeoposition = MockGeoposition();
late final MockCoordinates mockCoordinates = MockCoordinates();
const LatLng currentLocation = LatLng(10.8231, 106.6297);

controller = createController(
mapConfiguration: const MapConfiguration(
myLocationEnabled: true,
myLocationButtonEnabled: true,
));

controller.debugSetOverrides(
createMap: (_, __) => map,
markers: markers,
geolocation: mockGeolocation,
);

when(mockGeoposition.coords).thenReturn(mockCoordinates);

when(mockCoordinates.longitude).thenReturn(currentLocation.longitude);

when(mockCoordinates.latitude).thenReturn(currentLocation.latitude);

when(mockGeolocation.getCurrentPosition(
timeout: anyNamed('timeout'),
)).thenAnswer((_) async => mockGeoposition);

when(mockGeolocation.watchPosition()).thenAnswer((_) {
return Stream<MockGeoposition>.fromIterable(
<MockGeoposition>[mockGeoposition]);
});

controller.init();
await tester.pumpAndSettle();

final Set<Marker> capturedMarkers =
verify(markers.addMarkers(captureAny)).captured[1] as Set<Marker>;

expect(controller.myLocationButton, isNotNull);
expect(capturedMarkers.length, 1);
expect(capturedMarkers.first.position, currentLocation);
expect(capturedMarkers.first.zIndex, 0.5);
});

testWidgets('initializes with my location only',
(WidgetTester tester) async {
late final MockGeolocation mockGeolocation = MockGeolocation();
late final MockGeoposition mockGeoposition = MockGeoposition();
late final MockCoordinates mockCoordinates = MockCoordinates();
const LatLng currentLocation = LatLng(10.8231, 106.6297);

controller = createController(
mapConfiguration: const MapConfiguration(
myLocationEnabled: true,
myLocationButtonEnabled: false,
));
controller.debugSetOverrides(
createMap: (_, __) => map,
markers: markers,
geolocation: mockGeolocation,
);

when(mockGeoposition.coords).thenReturn(mockCoordinates);

when(mockCoordinates.longitude).thenReturn(currentLocation.longitude);

when(mockCoordinates.latitude).thenReturn(currentLocation.latitude);

when(mockGeolocation.getCurrentPosition(timeout: anyNamed('timeout')))
.thenAnswer((_) async => mockGeoposition);

when(mockGeolocation.watchPosition()).thenAnswer((_) {
return Stream<MockGeoposition>.fromIterable(
<MockGeoposition>[mockGeoposition]);
});

controller.init();

await tester.pumpAndSettle();

final Set<Marker> capturedMarkers =
verify(markers.addMarkers(captureAny)).captured[1] as Set<Marker>;

expect(controller.myLocationButton, isNull);
expect(capturedMarkers.length, 1);
expect(capturedMarkers.first.position, currentLocation);
expect(capturedMarkers.first.zIndex, 0.5);
});
});

testWidgets(
'My location button should be disable when dont have permission access to location',
(WidgetTester tester) async {
late final MockGeolocation mockGeolocation = MockGeolocation();

controller = createController(
mapConfiguration: const MapConfiguration(
myLocationEnabled: true,
myLocationButtonEnabled: true,
));

controller.debugSetOverrides(
createMap: (_, __) => map,
markers: markers,
geolocation: mockGeolocation,
);

when(mockGeolocation.getCurrentPosition(timeout: anyNamed('timeout')))
.thenAnswer(
(_) async => throw Exception('permission denied'),
);

when(mockGeolocation.watchPosition()).thenAnswer((_) {
throw Exception('permission denied');
});

controller.init();

await tester.pumpAndSettle();

final Set<Marker> capturedMarkers =
verify(markers.addMarkers(captureAny)).captured[0] as Set<Marker>;
expect(controller.myLocationButton, isNotNull);
expect(controller.myLocationButton?.isDisabled(), true);
expect(capturedMarkers.length, 0);
});
});

// These are the methods that are delegated to the gmaps.GMap object, that we can mock...
Expand Down
Loading