Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Improve my location
  • Loading branch information
nploi committed May 2, 2023
commit 7220ed3c8f530f779a42ea3171f4fdc365613413
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

part of google_maps_flutter_web;

// Default values for when the get current location fails
final LatLng _nullLatLng = LatLng(0, 0);
Geolocation _geolocation = window.navigator.geolocation;
LatLng? _lastLocationKnown;

// Watch current location and update blue dot
Future<void> _displayAndWatchMyLocation(MarkersController controller) async {
final Marker marker = await _createBlueDotMarker();
_geolocation.watchPosition().listen((Geoposition location) async {
_lastLocationKnown = LatLng(
final LatLng latLng = LatLng(
location.coords!.latitude!.toDouble(),
location.coords!.longitude!.toDouble(),
);
Expand All @@ -21,8 +22,7 @@ Future<void> _displayAndWatchMyLocation(MarkersController controller) async {
// - Render the direction in which we're looking at with a small "cone" using the heading information.
// - Render the current position marker as an arrow when the current position is "moving" (speed > certain threshold), and the direction in which the arrow should point (again, with the heading information).

final Marker markerUpdate =
marker.copyWith(positionParam: _lastLocationKnown);
final Marker markerUpdate = marker.copyWith(positionParam: latLng);
if (controller.markers.containsKey(marker.markerId)) {
controller._changeMarker(markerUpdate);
} else {
Expand All @@ -33,27 +33,25 @@ Future<void> _displayAndWatchMyLocation(MarkersController controller) async {

// Get current location
Future<LatLng> _getCurrentLocation() async {
final Geoposition location = await _geolocation.getCurrentPosition(
timeout: const Duration(seconds: 30),
);
final Geoposition location = await _geolocation.getCurrentPosition();
return LatLng(
location.coords!.latitude!.toDouble(),
location.coords!.longitude!.toDouble(),
location.coords?.latitude?.toDouble() ?? _nullLatLng.latitude,
location.coords?.longitude?.toDouble() ?? _nullLatLng.longitude,
);
}

// Find and move to current location
Future<void> _centerMyCurrentLocation(
GoogleMapController controller,
) async {
controller._myLocationButton?.startAnimation();

try {
_lastLocationKnown ??= await _getCurrentLocation();
if (_lastLocationKnown != null) {
await controller.moveCamera(
CameraUpdate.newLatLng(_lastLocationKnown!),
);
}
final LatLng location = await _getCurrentLocation();

await controller.moveCamera(
CameraUpdate.newLatLng(location),
);
controller._myLocationButton?.doneAnimation();
} catch (e) {
controller._myLocationButton?.disableBtn();
Expand All @@ -65,8 +63,7 @@ void _addMyLocationButton(gmaps.GMap map, GoogleMapController controller) {
controller._myLocationButton = MyLocationButton();
controller._myLocationButton?.addClickListener(
(_) async {
controller._myLocationButton?.startAnimation();
await _centerMyCurrentLocation(controller);
_centerMyCurrentLocation(controller);
},
);
map.addListener('dragend', () {
Expand Down Expand Up @@ -190,6 +187,7 @@ class MyLocationButton {
/// Disable button
void disableBtn() {
_btnChild.disabled = true;
_imageChild.classes.remove('waiting');
_imageChild.style.backgroundPosition = '-24px 0px';
}

Expand Down