-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Honor clickable for Markers in Google Maps #7318
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
base: v2026.3.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,6 +80,7 @@ | |
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| import kotlin.Pair; | ||
| import timber.log.Timber; | ||
|
|
||
| public class GoogleMapFragment extends MapViewModelMapFragment implements | ||
|
|
@@ -396,25 +397,30 @@ public void onMapLongClick(LatLng latLng) { | |
|
|
||
| @Override | ||
| public boolean onMarkerClick(Marker marker) { | ||
| if (featureClickListener != null) { // FormMapActivity | ||
| featureClickListener.onFeature(findFeature(marker)); | ||
| } else { // GeoWidget | ||
| onMapClick(marker.getPosition()); | ||
| Pair<Integer, MarkerFeature> markerFeature = getFeature(marker); | ||
|
|
||
| if (markerFeature != null) { | ||
| if (featureClickListener != null && markerFeature.getSecond().isClickable()) { | ||
| featureClickListener.onFeature(markerFeature.getFirst()); | ||
| } else { | ||
| onMapClick(marker.getPosition()); | ||
| } | ||
| } | ||
|
|
||
| return true; // consume the event (no default zoom and popup behaviour) | ||
| } | ||
|
|
||
| @Override | ||
| public void onPolylineClick(Polyline polyline) { | ||
| if (featureClickListener != null) { | ||
| featureClickListener.onFeature(findFeature(polyline)); | ||
| featureClickListener.onFeature(getFeatureId(polyline)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onPolygonClick(@NonNull Polygon polygon) { | ||
| if (featureClickListener != null) { | ||
| featureClickListener.onFeature(findFeature(polygon)); | ||
| featureClickListener.onFeature(getFeatureId(polygon)); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -423,7 +429,7 @@ public void onMarkerDragStart(Marker marker) { | |
| // When dragging starts, GoogleMap makes the marker jump up to move it | ||
| // out from under the user's finger; whenever a marker moves, we have | ||
| // to update its corresponding feature. | ||
| updateFeature(findFeature(marker)); | ||
| updateFeature(getFeatureId(marker)); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -432,12 +438,12 @@ public void onMarkerDrag(Marker marker) { | |
| // obtained from a GPS reading, so the altitude and standard deviation | ||
| // fields are no longer meaningful; reset them to zero. | ||
| marker.setSnippet("0;0"); | ||
| updateFeature(findFeature(marker)); | ||
| updateFeature(getFeatureId(marker)); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMarkerDragEnd(Marker marker) { | ||
| int featureId = findFeature(marker); | ||
| int featureId = getFeatureId(marker); | ||
| updateFeature(featureId); | ||
| if (dragEndListener != null && featureId != -1) { | ||
| dragEndListener.onFeature(featureId); | ||
|
|
@@ -528,22 +534,35 @@ private void moveOrAnimateCamera(CameraUpdate movement, boolean animate) { | |
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| private Pair<Integer, MarkerFeature> getFeature(Marker marker) { | ||
| for (int featureId : features.keySet()) { | ||
| MapFeature mapFeature = features.get(featureId); | ||
| if (mapFeature instanceof MarkerFeature && mapFeature.ownsMarker(marker)) { | ||
| return new Pair<>(featureId, (MarkerFeature) mapFeature); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Finds the feature to which the given marker belongs. | ||
| * Finds the feature ID to which the given marker belongs. | ||
| */ | ||
| private int findFeature(Marker marker) { | ||
| private int getFeatureId(Marker marker) { | ||
| for (int featureId : features.keySet()) { | ||
| if (features.get(featureId).ownsMarker(marker)) { | ||
| return featureId; | ||
| } | ||
| } | ||
| return -1; // not found | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| /** | ||
| * Finds the feature to which the given polyline belongs. | ||
| */ | ||
| private int findFeature(Polyline polyline) { | ||
| private int getFeatureId(Polyline polyline) { | ||
| for (int featureId : features.keySet()) { | ||
| if (features.get(featureId).ownsPolyline(polyline)) { | ||
| return featureId; | ||
|
|
@@ -552,7 +571,7 @@ private int findFeature(Polyline polyline) { | |
| return -1; // not found | ||
| } | ||
|
|
||
| private int findFeature(Polygon polygon) { | ||
| private int getFeatureId(Polygon polygon) { | ||
| for (int featureId : features.keySet()) { | ||
| if (features.get(featureId).ownsPolygon(polygon)) { | ||
| return featureId; | ||
|
|
@@ -682,18 +701,29 @@ public interface MapFeature { | |
| } | ||
|
|
||
| private static class MarkerFeature implements MapFeature { | ||
| private final MarkerDescription markerDescription; | ||
| private Marker marker; | ||
| private final Context context; | ||
|
|
||
| MarkerFeature(Context context, MarkerDescription markerDescription, GoogleMap map) { | ||
| this.context = context; | ||
| marker = createMarker(context, markerDescription, map); | ||
| this.markerDescription = markerDescription; | ||
| marker = createMarker(context, this.markerDescription, map); | ||
| } | ||
|
|
||
| public void setIcon(MarkerIconDescription markerIconDescription) { | ||
| marker.setIcon(getBitmapDescriptor(context, markerIconDescription)); | ||
| } | ||
|
|
||
| public boolean isClickable() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be private.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'd prefer to leave it private like the other methods on this class. The outer class can access it because Java (if it was Kotlin we'd have a file private class instead), but it makes more sense to have the inner class be private to the outer class and then have the methods used by things outside the inner class be public. |
||
| MarkerIconDescription iconDescription = markerDescription.getIconDescription(); | ||
| if (iconDescription instanceof MarkerIconDescription.DrawableResource) { | ||
| return ((MarkerIconDescription.DrawableResource) iconDescription).getClickable(); | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public MapPoint getPoint() { | ||
| return fromMarker(marker); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.