Skip to content
Open
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
10 changes: 10 additions & 0 deletions packages/google_maps_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 0.4.0

* Add support for `mapTypeControlEnabled`, `fullscreenControlEnabled`, `streetViewControlEnabled`, `webCameraControlEnabled`, `webCameraControlPosition`, `webGestureHandling`, and `fortyFiveDegreeImageryEnabled`.
* Fix `consumeTapEvents` for circles, polygons, and polylines.
* Fix initial map objects being added to the map twice.
* Fix `onMapCreated` never being called when a map tile request stalls or fails.
* Fix map events being dropped when the reported coordinates are integers.
* Fix cluster managers passed to the initial `GoogleMap` being ignored.
* Fix `Cluster.markerIds` holding internal ids instead of the markers' `MarkerId`s.

## 0.3.0

* Update `webview_flutter_lwe` to ^0.6.0.
Expand Down
2 changes: 1 addition & 1 deletion packages/google_maps_flutter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This package is not an _endorsed_ implementation of `google_maps_flutter`. There
```yaml
dependencies:
google_maps_flutter: ^2.18.0
google_maps_flutter_tizen: ^0.3.0
google_maps_flutter_tizen: ^0.4.0
```

For detailed usage, see https://pub.dev/packages/google_maps_flutter#sample-usage.
Expand Down
64 changes: 50 additions & 14 deletions packages/google_maps_flutter/lib/src/convert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Map<int, String> _mapTypeToMapTypeId = <int, String>{
// Builds the raw map options map from a [MapConfiguration].
//
// This mirrors the platform interface's `jsonForMapConfiguration` helper
// (which is not exported), plus [MapConfiguration.colorScheme].
// (which is not exported), plus [MapConfiguration.colorScheme] and the web
// control options.
//
// Intentionally omitted:
// * `cloudMapId` — deprecated alias for `mapId`, which is already serialized.
Expand Down Expand Up @@ -53,6 +54,18 @@ Map<String, dynamic> _mapOptionsFromConfiguration(MapConfiguration config) {
if (config.mapId != null) 'mapId': config.mapId,
if (config.style != null) 'style': config.style,
if (config.colorScheme != null) 'colorScheme': config.colorScheme!.index,
if (config.webGestureHandling != null) 'webGestureHandling': config.webGestureHandling!.name,
if (config.fortyFiveDegreeImageryEnabled != null)
'fortyFiveDegreeImageryEnabled': config.fortyFiveDegreeImageryEnabled,
if (config.webCameraControlEnabled != null)
'webCameraControlEnabled': config.webCameraControlEnabled,
if (config.webCameraControlPosition != null)
'webCameraControlPosition': config.webCameraControlPosition!.name,
if (config.mapTypeControlEnabled != null) 'mapTypeControlEnabled': config.mapTypeControlEnabled,
if (config.fullscreenControlEnabled != null)
'fullscreenControlEnabled': config.fullscreenControlEnabled,
if (config.streetViewControlEnabled != null)
'streetViewControlEnabled': config.streetViewControlEnabled,
};
}

Expand Down Expand Up @@ -89,9 +102,11 @@ String? _getCameraBounds(dynamic option) {
// web buildingsEnabled seems to not have an equivalent in web padding seems to
// behave differently in web than mobile. You can't move UI elements in web.
String _rawOptionsToString(Map<String, dynamic> rawOptions) {
// These don't have any rawOptions entry, but they seem to be off in the
// native maps.
var options = 'mapTypeControl: false, fullscreenControl: false, streetViewControl: false';
// These are off by default in the native maps.
var options =
'mapTypeControl: ${rawOptions['mapTypeControlEnabled'] ?? false}'
', fullscreenControl: ${rawOptions['fullscreenControlEnabled'] ?? false}'
', streetViewControl: ${rawOptions['streetViewControlEnabled'] ?? false}';
Comment on lines +107 to +109

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these controls actually rendered on the screen and available for testing?
In my local test environment, the option values ​​are being passed to the Maps JS, but I am unable to see the controls themselves rendered on the screen.


if (_mapTypeToMapTypeId.containsKey(rawOptions['mapType'])) {
options += ", mapTypeId: '${_mapTypeToMapTypeId[rawOptions['mapType']]}'";
Expand All @@ -115,13 +130,31 @@ String _rawOptionsToString(Map<String, dynamic> rawOptions) {
options += ', zoomControl: ${rawOptions['zoomControlsEnabled']}';
}

if (rawOptions['webCameraControlEnabled'] != null) {
options += ', cameraControl: ${rawOptions['webCameraControlEnabled']}';
}

if (rawOptions['webCameraControlPosition'] != null) {
final String position = (rawOptions['webCameraControlPosition'] as String)
.replaceAllMapped(RegExp('[A-Z]'), (Match m) => '_${m[0]}')
.toUpperCase();
options += ', cameraControlOptions: { position: google.maps.ControlPosition.$position }';
}

if (rawOptions['fortyFiveDegreeImageryEnabled'] != null) {
options += ', rotateControl: ${rawOptions['fortyFiveDegreeImageryEnabled']}';
}

if (rawOptions['style'] != null && (rawOptions['style'] as String).isNotEmpty) {
options += ', styles: ${rawOptions['style']}';
} else {
options += ', styles: null';
}

if (rawOptions['scrollGesturesEnabled'] == false || rawOptions['zoomGesturesEnabled'] == false) {
if (rawOptions['webGestureHandling'] != null) {
options += ", gestureHandling: '${rawOptions['webGestureHandling']}'";
} else if (rawOptions['scrollGesturesEnabled'] == false ||
rawOptions['zoomGesturesEnabled'] == false) {
options += ", gestureHandling: 'none'";
} else {
options += ", gestureHandling: 'auto'";
Expand Down Expand Up @@ -223,14 +256,14 @@ LatLngBounds _convertToBounds(String value) {
final dynamic bound = json.decode(value);
if (bound is Map<String, dynamic>) {
assert(
bound['south'] is double &&
bound['west'] is double &&
bound['north'] is double &&
bound['east'] is double,
bound['south'] is num &&
bound['west'] is num &&
bound['north'] is num &&
bound['east'] is num,
);
return LatLngBounds(
southwest: LatLng(bound['south'] as double, bound['west'] as double),
northeast: LatLng(bound['north'] as double, bound['east'] as double),
southwest: LatLng((bound['south'] as num).toDouble(), (bound['west'] as num).toDouble()),
northeast: LatLng((bound['north'] as num).toDouble(), (bound['east'] as num).toDouble()),
);
}
} catch (e) {
Expand Down Expand Up @@ -373,7 +406,8 @@ util.GPolylineOptions _polylineOptionsFromPolyline(Polyline polyline) {
..strokeOpacity = _getCssOpacity(polyline.color)
..visible = polyline.visible
..zIndex = polyline.zIndex
..geodesic = polyline.geodesic;
..geodesic = polyline.geodesic
..clickable = polyline.consumeTapEvents;
}

util.GPolygonOptions _polygonOptionsFromPolygon(Polygon polygon) {
Expand Down Expand Up @@ -405,7 +439,8 @@ util.GPolygonOptions _polygonOptionsFromPolygon(Polygon polygon) {
..fillOpacity = _getCssOpacity(polygon.fillColor)
..visible = polygon.visible
..zIndex = polygon.zIndex
..geodesic = polygon.geodesic;
..geodesic = polygon.geodesic
..clickable = polygon.consumeTapEvents;
}

/// Calculates the direction of a given Polygon
Expand Down Expand Up @@ -440,7 +475,8 @@ util.GCircleOptions _circleOptionsFromCircle(Circle circle) {
..center = LatLng(circle.center.latitude, circle.center.longitude)
..radius = circle.radius
..visible = circle.visible
..zIndex = circle.zIndex;
..zIndex = circle.zIndex
..clickable = circle.consumeTapEvents;
}

util.GGroundOverlayOptions? _groundOverlayOptionsFromGroundOverlay(GroundOverlay groundOverlay) {
Expand Down
61 changes: 44 additions & 17 deletions packages/google_maps_flutter/lib/src/google_maps_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class GoogleMapsController {
_clusterManagersController = ClusterManagersController(
stream: _streamController,
bridge: _bridge,
markerIdForId: (int id) => _markersController?._idToMarkerId[id],
);
_markersController = MarkersController(
stream: _streamController,
Expand Down Expand Up @@ -139,9 +140,9 @@ class GoogleMapsController {
case MapsJsEventType.boundsChanged:
await _onBoundsChanged();
case MapsJsEventType.idle:
_onIdle();
await _onIdle();
case MapsJsEventType.tilesLoaded:
_onTilesloaded();
_onMapReady();
case MapsJsEventType.click:
_onClick(event.message!);
case MapsJsEventType.longPress:
Expand Down Expand Up @@ -215,14 +216,26 @@ class GoogleMapsController {
}
}

void _onIdle() {
Future<void> _onIdle() async {
_mapIsMoving = false;
_streamController.add(CameraIdleEvent(_mapId));
// NOTE: `tilesloaded` never fires if a tile request stalls or fails.
// See https://github.com/flutter/flutter/issues/193452.
if (!_isFirst && await _isMapLaidOut()) {
_onMapReady();
}
}

Future<bool> _isMapLaidOut() async {
final Object value = await _bridge.runJavaScriptReturningResult(
'map.getDiv().offsetWidth > 0 && map.getDiv().offsetHeight > 0',
);
return value.toString() == 'true';
}

void _onTilesloaded() {
void _onMapReady() {
try {
if (_isFirst) {
if (_isFirst || _streamController.isClosed) {
return;
}
_streamController.add(MapReadyEvent(_mapId));
Expand All @@ -237,7 +250,10 @@ class GoogleMapsController {
final dynamic event = json.decode(message);
if (event is Map<String, dynamic>) {
assert(event['latLng'] != null);
final position = LatLng(event['latLng']['lat'] as double, event['latLng']['lng'] as double);
final position = LatLng(
(event['latLng']['lat'] as num).toDouble(),
(event['latLng']['lng'] as num).toDouble(),
);
_streamController.add(MapTapEvent(_mapId, position));
}
} catch (e) {
Expand All @@ -250,7 +266,10 @@ class GoogleMapsController {
final dynamic event = json.decode(message);
if (event is Map<String, dynamic>) {
assert(event['latLng'] != null);
final position = LatLng(event['latLng']['lat'] as double, event['latLng']['lng'] as double);
final position = LatLng(
(event['latLng']['lat'] as num).toDouble(),
(event['latLng']['lng'] as num).toDouble(),
);
_streamController.add(MapLongPressEvent(_mapId, position));
}
} catch (e) {
Expand Down Expand Up @@ -302,8 +321,8 @@ class GoogleMapsController {
final MarkerController? marker = _markersController!._markerIdToController[markerId];

final position = LatLng(
result['event']['latLng']['lat'] as double,
result['event']['latLng']['lng'] as double,
(result['event']['latLng']['lat'] as num).toDouble(),
(result['event']['latLng']['lng'] as num).toDouble(),
);

if (marker?.dragStartEvent != null) {
Expand All @@ -326,8 +345,8 @@ class GoogleMapsController {
final MarkerController? marker = _markersController!._markerIdToController[markerId];

final position = LatLng(
result['event']['latLng']['lat'] as double,
result['event']['latLng']['lng'] as double,
(result['event']['latLng']['lat'] as num).toDouble(),
(result['event']['latLng']['lng'] as num).toDouble(),
);

if (marker?.dragEvent != null) {
Expand All @@ -350,8 +369,8 @@ class GoogleMapsController {
final MarkerController? marker = _markersController!._markerIdToController[markerId];

final position = LatLng(
result['event']['latLng']['lat'] as double,
result['event']['latLng']['lng'] as double,
(result['event']['latLng']['lat'] as num).toDouble(),
(result['event']['latLng']['lng'] as num).toDouble(),
);

if (marker?.dragEndEvent != null) {
Expand Down Expand Up @@ -433,7 +452,13 @@ class GoogleMapsController {
///
/// Failure to call this method would result in not rendering at all,
/// and most of the public methods on this class no-op'ing.
Future<void> init() async {
///
/// Later calls return the same future.
Future<void> init() => _initFuture ??= _init();

Future<void>? _initFuture;

Future<void> _init() async {
if (_webview == null && !_streamController.isClosed) {
_getWebview();
if (!await _bridge.load()) {
Expand All @@ -445,6 +470,8 @@ class GoogleMapsController {
await _createMap();
}
await _attachGeometryControllers();
_initClustering(_clusterManagers);

_renderInitialGeometry(
markers: _markers,
circles: _circles,
Expand All @@ -453,8 +480,6 @@ class GoogleMapsController {
groundOverlays: _groundOverlays,
);

_initClustering(_clusterManagers);

await _setTrafficLayer(_isTrafficLayerEnabled(_rawMapOptions));
}

Expand Down Expand Up @@ -588,7 +613,9 @@ class GoogleMapsController {

Future<double> _getZoom() async {
try {
return (await _callMethod('getZoom', <Object?>[]) as num) + 0.0;
// NOTE: Returns '' if called while the webview is being torn down.
final Object zoom = await _callMethod('getZoom', <Object?>[]);
return zoom is num ? zoom.toDouble() : 0.0;
} catch (e) {
debugPrint('JavaScript Error: $e');
return 0.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class GoogleMapsPlugin extends GoogleMapsFlutterPlatform {
_map(mapId).updateRawOptions(optionsUpdate);
}

/// Updates the configuration of a given `mapId`.
@override
Future<void> updateMapConfiguration(MapConfiguration configuration, {required int mapId}) async {
_map(mapId).updateRawOptions(_mapOptionsFromConfiguration(configuration));
}

/// Applies the passed in `markerUpdates` to the `mapId`.
@override
Future<void> updateMarkers(MarkerUpdates markerUpdates, {required int mapId}) async {
Expand Down Expand Up @@ -289,6 +295,7 @@ class GoogleMapsPlugin extends GoogleMapsFlutterPlatform {
polygons: mapObjects.polygons,
polylines: mapObjects.polylines,
circles: mapObjects.circles,
clusterManagers: mapObjects.clusterManagers,
groundOverlays: mapObjects.groundOverlays,
gestureRecognizers: widgetConfiguration.gestureRecognizers,
mapOptions: _mapOptionsFromConfiguration(mapConfiguration),
Expand Down Expand Up @@ -330,6 +337,7 @@ class GoogleMapsPlugin extends GoogleMapsFlutterPlatform {
Set<Polygon> polygons = const <Polygon>{},
Set<Polyline> polylines = const <Polyline>{},
Set<Circle> circles = const <Circle>{},
Set<ClusterManager> clusterManagers = const <ClusterManager>{},
Set<GroundOverlay> groundOverlays = const <GroundOverlay>{},
Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers =
const <Factory<OneSequenceGestureRecognizer>>{},
Expand All @@ -350,6 +358,7 @@ class GoogleMapsPlugin extends GoogleMapsFlutterPlatform {
polygons: polygons,
polylines: polylines,
circles: circles,
clusterManagers: clusterManagers,
groundOverlays: groundOverlays,
mapOptions: mapOptions,
)..init(); // Initialize the controller
Expand Down
19 changes: 12 additions & 7 deletions packages/google_maps_flutter/lib/src/marker_clustering.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ class ClusterManagersController extends GeometryController {
ClusterManagersController({
required StreamController<MapEvent<Object?>> stream,
required GoogleMapsJsBridge bridge,
required MarkerId? Function(int id) markerIdForId,
}) : _streamController = stream,
_bridge = bridge,
_markerIdForId = markerIdForId,
_idToClusterManagerId = <String, ClusterManagerId>{},
_clusterManagerIdToMarkerClusterer = <ClusterManagerId, util.GMarkerClusterer>{};

Expand All @@ -28,6 +30,8 @@ class ClusterManagersController extends GeometryController {

final GoogleMapsJsBridge _bridge;

final MarkerId? Function(int id) _markerIdForId;

// A cache of [MarkerClusterer]s indexed by their [ClusterManagerId].
final Map<ClusterManagerId, util.GMarkerClusterer> _clusterManagerIdToMarkerClusterer;
final Map<String, ClusterManagerId> _idToClusterManagerId;
Expand Down Expand Up @@ -144,23 +148,24 @@ class ClusterManagersController extends GeometryController {
Map<String, dynamic> markerClustererCluster,
) {
final position = LatLng(
(markerClustererCluster['position'] as dynamic)['lat'] as double,
(markerClustererCluster['position'] as dynamic)['lng'] as double,
((markerClustererCluster['position'] as dynamic)['lat'] as num).toDouble(),
((markerClustererCluster['position'] as dynamic)['lng'] as num).toDouble(),
);

final bounds = LatLngBounds(
southwest: LatLng(
(markerClustererCluster['bounds'] as dynamic)['south'] as double,
(markerClustererCluster['bounds'] as dynamic)['west'] as double,
((markerClustererCluster['bounds'] as dynamic)['south'] as num).toDouble(),
((markerClustererCluster['bounds'] as dynamic)['west'] as num).toDouble(),
),
northeast: LatLng(
(markerClustererCluster['bounds'] as dynamic)['north'] as double,
(markerClustererCluster['bounds'] as dynamic)['east'] as double,
((markerClustererCluster['bounds'] as dynamic)['north'] as num).toDouble(),
((markerClustererCluster['bounds'] as dynamic)['east'] as num).toDouble(),
),
);

final List<MarkerId> markerIds = (markerClustererCluster['markers']! as List<dynamic>)
.map<MarkerId>((dynamic markerId) => MarkerId((markerId as int).toString()))
.map((dynamic id) => _markerIdForId(id as int))
.nonNulls
.toList();

return Cluster(clusterManagerId, markerIds, position: position, bounds: bounds);
Expand Down
Loading