-
Notifications
You must be signed in to change notification settings - Fork 3k
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
[google_maps_flutter] Add Advanced Markers support #7882
base: main
Are you sure you want to change the base?
[google_maps_flutter] Add Advanced Markers support #7882
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
High level android code review. I am concerned by having to runtime type check class.
That said thank you for opening this pull request.
packages/google_maps_flutter/google_maps_flutter/example/android/app/build.gradle
Outdated
Show resolved
Hide resolved
*/ | ||
private void initializeRenderer(ClusterManager<MarkerBuilder> clusterManager) { | ||
final ClusterRenderer<MarkerBuilder> renderer = clusterManager.getRenderer(); | ||
if (renderer.getClass() == MarkerClusterRenderer.class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this reflection here? Can we use non null instead as a signal here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed this check - previously initializeRenderer
(it was named differently) was called multiple times, now it's called once and there's no need to check if a renderer has been previously assigned
@@ -225,6 +256,35 @@ protected void onClusterItemRendered(@NonNull T item, @NonNull Marker marker) { | |||
} | |||
} | |||
|
|||
/** AdvancedMarkerClusterRenderer is a ClusterRenderer that supports AdvancedMarkers */ | |||
@VisibleForTesting | |||
static class AdvancedMarkerClusterRenderer<T extends MarkerBuilder> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be a different class? If it does can it extend MarkerClusterRenderer?
I worry about keeping the two in sync.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it does. android-maps-utils
have two different renderers - one for each marker type (legacy and advanced). This new class extends DefaultAdvancedMarkersClusterRenderer which is meant to be used with AdvancedMarker
s. As far as I remember, android-maps-utils
recreates cluster markers under the hood so it has to know which marker type to use
@@ -980,6 +984,18 @@ public Boolean isInfoWindowShown(@NonNull String markerId) { | |||
return lastSetStyleSucceeded; | |||
} | |||
|
|||
@Override | |||
public @NonNull Boolean isAdvancedMarkersAvailable() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be bool instead of Boolean if it is non null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it can't be done like this. This code uses Pigeon-generated code and Dart's bool isAdvancedMarkersAvailable()
is translated into Java's @NonNull Boolean isAdvancedMarkersAvailable();
final String cloudMapId = mapConfig.getCloudMapId(); | ||
if (cloudMapId != null) { | ||
builder.setMapId(cloudMapId); | ||
final String mapId = mapConfig.getMapId(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my own education can you share why we would want to use mapId instead of cloudMapId and why the change is safe (wont cause a regression in existing apps)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a comment about renaming it here.. It's safe because cloudMapId
can still be used by existing clients, it's marked as deprecated and uses mapId
under the hood
@@ -143,6 +143,9 @@ public void setZIndex(float zIndex) { | |||
marker.setZIndex(zIndex); | |||
} | |||
|
|||
@Override | |||
public void setCollisionBehavior(int collisionBehavior) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a javadoc that describes the valid collisionBehavior int's or at least where they are defined.
Better yet convert int to something typesafe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've only looked at the app-facing and platform interface packages so far, but there are some fundamental structure questions at the interface layer.
...ages/google_maps_flutter/google_maps_flutter/example/lib/readme_sample_advanced_markers.dart
Outdated
Show resolved
Hide resolved
...ages/google_maps_flutter/google_maps_flutter/example/lib/readme_sample_advanced_markers.dart
Outdated
Show resolved
Hide resolved
packages/google_maps_flutter/google_maps_flutter/example/lib/clustering.dart
Outdated
Show resolved
Hide resolved
packages/google_maps_flutter/google_maps_flutter/example/lib/main.dart
Outdated
Show resolved
Hide resolved
packages/google_maps_flutter/google_maps_flutter/example/lib/marker_icons.dart
Outdated
Show resolved
Hide resolved
|
||
/// Color of the default glyph (circle) | ||
final Color? color; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are all of the different types of Glyph
a single class with fields that are almost all unique to a single type, rather than using subclasses like, e.g., BitmapDescriptor
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a really good suggestion, thanks. I've changed all three named constructors to be separate classes extending BitmapDescriptor
. They're all now in bitmap.dart
...gle_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/map_configuration.dart
Outdated
Show resolved
Hide resolved
/// | ||
/// See https://developers.google.com/maps/documentation/get-map-id | ||
/// for more details. | ||
@Deprecated('cloudMapId is deprecated. Use mapId instead') | ||
final String? cloudMapId; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intent is to renamed cloudMapId
, right? If so, the field should be removed, and it should store to and read from mapId
, rather than having two potentially conflicting fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the intent is to rename cloudMapId
to mapId
, I've removed the field. Do you think it's okay to rename it and it should be done in this PR?
Why it should be renamed:
mapId
is what Google Maps platform implementations use- "Map ID" is what Cloud Console uses on Map styles page
cloudMapId
feels a bit like it's used for cloud styling which is true but after introduction of Advanced markers map ID is required to make Advanced markers work even if map is not using a custom style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing cloudMapId
from the interface (as in the current version of the PR) is a breaking change, which isn't something we want. What I was suggesting was replacing the field with a getter that reads from mapId
, as that preserves the public API without having two difference sources of truth.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be fixed now
...s_flutter/google_maps_flutter_platform_interface/lib/src/types/map_widget_configuration.dart
Outdated
Show resolved
Hide resolved
packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/marker.dart
Outdated
Show resolved
Hide resolved
Looks like some platform reviewers were dropped at some point on accident. |
@@ -121,6 +121,21 @@ the `GoogleMap`'s `onMapCreated` callback. | |||
The `GoogleMap` widget should be used within a widget with a bounded size. Using it | |||
in an unbounded widget will cause the application to throw a Flutter exception. | |||
|
|||
### Advanced Markers | |||
|
|||
[Advanced Markers](https://developers.google.com/maps/documentation/javascript/advanced-markers/overview) are map markers that offer extra customization options. [Map Id](https://developers.google.com/maps/documentation/get-map-id) is required in order to use Advanced Markers: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: The official docs refer to this as "Map ID" not "Map Id".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed
|
||
/// Whether map supports advanced markers. Null indicates capability check | ||
/// is in progress | ||
bool? _isAdvancedMarkersAvailable; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's unfortunate 😐. I agree though, matching the underlying SDK makes sense here.
} | ||
|
||
@override | ||
int get hashCode => markerId.hashCode; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hrm, maybe that's intentional then. I'd need to dig into the SDK a lot more to be sure, but being consistent for now sounds good.
/// | ||
/// See https://developers.google.com/maps/documentation/get-map-id | ||
/// for more details. | ||
@Deprecated('cloudMapId is deprecated. Use mapId instead') | ||
final String? cloudMapId; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing cloudMapId
from the interface (as in the current version of the PR) is a breaking change, which isn't something we want. What I was suggesting was replacing the field with a getter that reads from mapId
, as that preserves the public API without having two difference sources of truth.
packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/bitmap.dart
Outdated
Show resolved
Hide resolved
packages/google_maps_flutter/google_maps_flutter_platform_interface/lib/src/types/bitmap.dart
Show resolved
Hide resolved
...gle_maps_flutter_platform_interface/lib/src/types/utils/map_configuration_serialization.dart
Show resolved
Hide resolved
@@ -28,14 +31,19 @@ import 'scrolling_map.dart'; | |||
import 'snapshot.dart'; | |||
import 'tile_overlay.dart'; | |||
|
|||
/// Map ID is required for some examples to use advanced markers. | |||
const String? _mapId = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm having trouble reconciling the comment and the line of code; if it's required for the examples, why is it null?
Is the idea that someone should put in their own map ID here and run the modified example? If so, the comment should be explicit about that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the idea is that someone should put their own ID. I've updated the comment to
/// Place your map ID here. Map ID is required for pages that use advanced
/// markers.
Hope it makes it clear what needs to be done and why.
final String? cloudMapId; | ||
|
||
/// Indicates whether map should use [AdvancedMarker]s or [Marker]s. | ||
final MarkerType markerType; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still needs to be updated.
I see Advanced merker icons are not rendering properly on the map. |
Hi, thanks for checking it out. Could please provide more details?
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, thanks for the code! I took a quick look at the core plugin, the platform interface, but specially the web implementation.
Most of the stuff is stylistic choices, but I have a question:
- Why do we even need the
MarkerType
when creating aMap
? It seems that it can be inferred the first time aMarker
is used, and the availability of Advanced Markers?
And an objection to the doXXXOnMarkerType
pattern, which IMO should be proper inheritance (like you did on AdvancedMarker extends Marker
).
Thanks for the PR, this is a highly requested feature!
@override | ||
Marker createMarker( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is smart, and reduces code repetition, but I'm not sure people looking at an example will want to unravel this inheritance. This might be more useful for end users if there's a little bit of duplication across the examples?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated all advanced examples not to do that, should be simpler now. Advanced marker icons
example now shows several pin config variations
packages/google_maps_flutter/google_maps_flutter/example/lib/advanced_markers_clustering.dart
Outdated
Show resolved
Hide resolved
packages/google_maps_flutter/google_maps_flutter/example/lib/clustering.dart
Outdated
Show resolved
Hide resolved
final Marker resetOld = | ||
widget.copyWithSelectedStated(markers[previousMarkerId]!, false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using !
, I'd use an assert(marker != null, 'Some descriptive message')
. Null assertion errors are quite cryptic (and more in the web, and even more in wasm :P), so I tend to avoid them if practical.
packages/google_maps_flutter/google_maps_flutter/example/lib/map_map_id.dart
Show resolved
Hide resolved
packages/google_maps_flutter/google_maps_flutter_web/lib/src/marker.dart
Outdated
Show resolved
Hide resolved
packages/google_maps_flutter/google_maps_flutter_web/lib/src/marker_clustering.dart
Outdated
Show resolved
Hide resolved
packages/google_maps_flutter/google_maps_flutter_web/lib/src/marker_clustering.dart
Show resolved
Hide resolved
packages/google_maps_flutter/google_maps_flutter_web/lib/src/marker_clustering_js_interop.dart
Outdated
Show resolved
Hide resolved
packages/google_maps_flutter/google_maps_flutter_web/lib/src/markers.dart
Outdated
Show resolved
Hide resolved
@aednlaxer, Please find the responses, |
This PR adds Advanced Markers support to
google_maps_flutter
as discussed in #155526. The document from the issue offers several options to implement Advanced Markers support, this PR uses option 1 (Advanced Marker Dart class is a subclass Marker class).Summary of changes:
AdvancedMarker
classMarkerCollisionBehavior
enum to control Advanced Marker's behavior when it collides with another markerPinConfig
bitmap descriptor for customizing Advanced Marker's pin and iconmarkerType
parameter to indicate that Advanced Markers should be usedcloudMapId
tomapId
Notes:
markerType
option when creating aGoogleMap
(could bemarker
oradvancedMarker
). Default option ismarker
cloudMapId
is deprecated in favor ofmapId
. New name follows SDKs, documentation and Cloud Console naming.gmaps.Marker
andgmaps.AdvancedMarkerElement
are not related to each other and should be handled differentlygoogle_maps_flutter
still uses them by default in this PR because of backward-compatibility. #130472 is related, package users will be able to use Advanced Markers to fix the deprecation warningResolves #155526
Pre-launch Checklist
dart format
.)[shared_preferences]
pubspec.yaml
with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.CHANGELOG.md
to add a description of the change, following repository CHANGELOG style, or this PR is exempt from CHANGELOG changes.///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.