From ae1ff86632f24f45126e3340e0ccb3b57a2aaf74 Mon Sep 17 00:00:00 2001 From: Felix Horvat Date: Tue, 9 Nov 2021 18:37:59 +0100 Subject: [PATCH 1/2] fixed issue with annotation click order on android --- .../mapbox/mapboxgl/MapboxMapController.java | 36 +++-- example/lib/main.dart | 2 + example/lib/overlay_annotations.dart | 148 ++++++++++++++++++ 3 files changed, 173 insertions(+), 13 deletions(-) create mode 100644 example/lib/overlay_annotations.dart diff --git a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java index 06346faf4..f2dc1d636 100644 --- a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java +++ b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java @@ -21,6 +21,7 @@ import android.view.View; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.lifecycle.DefaultLifecycleObserver; import androidx.lifecycle.Lifecycle; import androidx.lifecycle.LifecycleOwner; @@ -79,6 +80,7 @@ import java.util.List; import java.util.ArrayList; import java.util.Map; +import java.util.Collections; import io.flutter.plugin.common.BinaryMessenger; import io.flutter.plugin.common.MethodCall; @@ -303,19 +305,23 @@ public void setStyleString(String styleString) { @Override public void onStyleLoaded(@NonNull Style style) { MapboxMapController.this.style = style; - for(String annotationType : annotationOrder) { + final List orderReversed = new ArrayList(annotationOrder); + Collections.reverse(orderReversed); + String belowLayer = null; + + for(String annotationType : orderReversed) { switch (annotationType) { case "AnnotationType.fill": - enableFillManager(style); + belowLayer = enableFillManager(style, belowLayer); break; case "AnnotationType.line": - enableLineManager(style); + belowLayer = enableLineManager(style, belowLayer); break; case "AnnotationType.circle": - enableCircleManager(style); + belowLayer = enableCircleManager(style, belowLayer); break; case "AnnotationType.symbol": - enableSymbolManager(style); + belowLayer = enableSymbolManager(style, belowLayer); break; default: throw new IllegalArgumentException("Unknown annotation type: " + annotationType + ", must be either 'fill', 'line', 'circle' or 'symbol'"); @@ -378,36 +384,40 @@ private void onUserLocationUpdate(Location location){ methodChannel.invokeMethod("map#onUserLocationUpdated", arguments); } - private void enableSymbolManager(@NonNull Style style) { + private String enableSymbolManager(@NonNull Style style, @Nullable String belowLayer) { if (symbolManager == null) { - symbolManager = new SymbolManager(mapView, mapboxMap, style); + symbolManager = new SymbolManager(mapView, mapboxMap, style, belowLayer); symbolManager.setIconAllowOverlap(true); symbolManager.setIconIgnorePlacement(true); symbolManager.setTextAllowOverlap(true); symbolManager.setTextIgnorePlacement(true); symbolManager.addClickListener(MapboxMapController.this::onAnnotationClick); } + return symbolManager.getLayerId(); } - private void enableLineManager(@NonNull Style style) { + private String enableLineManager(@NonNull Style style, @Nullable String belowLayer) { if (lineManager == null) { - lineManager = new LineManager(mapView, mapboxMap, style); + lineManager = new LineManager(mapView, mapboxMap, style, belowLayer); lineManager.addClickListener(MapboxMapController.this::onAnnotationClick); } + return lineManager.getLayerId(); } - private void enableCircleManager(@NonNull Style style) { + private String enableCircleManager(@NonNull Style style, @Nullable String belowLayer) { if (circleManager == null) { - circleManager = new CircleManager(mapView, mapboxMap, style); + circleManager = new CircleManager(mapView, mapboxMap, style, belowLayer); circleManager.addClickListener(MapboxMapController.this::onAnnotationClick); } + return circleManager.getLayerId(); } - private void enableFillManager(@NonNull Style style) { + private String enableFillManager(@NonNull Style style, @Nullable String belowLayer) { if (fillManager == null) { - fillManager = new FillManager(mapView, mapboxMap, style); + fillManager = new FillManager(mapView, mapboxMap, style, belowLayer); fillManager.addClickListener(MapboxMapController.this::onAnnotationClick); } + return fillManager.getLayerId(); } @Override diff --git a/example/lib/main.dart b/example/lib/main.dart index b30238c2f..e3ca7d720 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -17,6 +17,7 @@ import 'line.dart'; import 'local_style.dart'; import 'map_ui.dart'; import 'move_camera.dart'; +import 'overlay_annotations.dart'; import 'page.dart'; import 'place_circle.dart'; import 'place_source.dart'; @@ -40,6 +41,7 @@ final List _allPages = [ AnnotationOrderPage(), CustomMarkerPage(), BatchAddPage(), + ClickAnnotationPage() ]; class MapsDemo extends StatelessWidget { diff --git a/example/lib/overlay_annotations.dart b/example/lib/overlay_annotations.dart new file mode 100644 index 000000000..6e55e693a --- /dev/null +++ b/example/lib/overlay_annotations.dart @@ -0,0 +1,148 @@ +// Copyright 2018 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; +import 'package:mapbox_gl/mapbox_gl.dart'; +import 'package:mapbox_gl_example/main.dart'; + +import 'page.dart'; + +class ClickAnnotationPage extends ExamplePage { + ClickAnnotationPage() + : super(const Icon(Icons.check_circle), 'Annotation tap'); + + @override + Widget build(BuildContext context) { + return const ClickAnnotationBody(); + } +} + +class ClickAnnotationBody extends StatefulWidget { + const ClickAnnotationBody(); + + @override + State createState() => ClickAnnotationBodyState(); +} + +class ClickAnnotationBodyState extends State { + ClickAnnotationBodyState(); + static const LatLng center = const LatLng(-33.88, 151.16); + + MapboxMapController? controller; + + void _onMapCreated(MapboxMapController controller) { + this.controller = controller; + controller.onFillTapped.add(_onFillTapped); + controller.onCircleTapped.add(_onCircleTapped); + controller.onLineTapped.add(_onLineTapped); + controller.onSymbolTapped.add(_onSymbolTapped); + } + + @override + void dispose() { + controller?.onFillTapped.remove(_onFillTapped); + super.dispose(); + } + + _showSnackBar(String type, String id) { + final snackBar = SnackBar( + content: Text('Tapped $type $id', + style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), + backgroundColor: Theme.of(context).primaryColor); + ScaffoldMessenger.of(context).clearSnackBars(); + ScaffoldMessenger.of(context).showSnackBar(snackBar); + } + + void _onFillTapped(Fill fill) { + _showSnackBar('fill', fill.id); + } + + void _onCircleTapped(Circle circle) { + _showSnackBar('circle', circle.id); + } + + void _onLineTapped(Line line) { + print("line"); + _showSnackBar('line', line.id); + } + + void _onSymbolTapped(Symbol symbol) { + _showSnackBar('symbol', symbol.id); + } + + void _onStyleLoaded() { + controller!.addCircle( + CircleOptions( + geometry: LatLng(-33.881979408447314, 151.171361438502117), + circleStrokeColor: "#00FF00", + circleStrokeWidth: 2, + circleRadius: 16, + ), + ); + controller!.addCircle( + CircleOptions( + geometry: LatLng(-33.894372606072309, 151.17576679759523), + circleStrokeColor: "#00FF00", + circleStrokeWidth: 2, + circleRadius: 30, + ), + ); + controller!.addSymbol( + SymbolOptions( + geometry: LatLng(-33.894372606072309, 151.17576679759523), + iconImage: "fast-food-15", + iconSize: 2), + ); + controller!.addLine( + LineOptions( + geometry: [ + LatLng(-33.874867744475786, 151.170627211986584), + LatLng(-33.881979408447314, 151.171361438502117), + LatLng(-33.887058805548882, 151.175032571079726), + LatLng(-33.894372606072309, 151.17576679759523), + LatLng(-33.900060683994681, 151.15765587687909), + ], + lineColor: "#0000FF", + lineWidth: 20, + ), + ); + + controller!.addFill( + FillOptions( + geometry: [ + [ + LatLng(-33.901517742631846, 151.178099204457737), + LatLng(-33.872845324482071, 151.179025547977773), + LatLng(-33.868230472039514, 151.147000529140399), + LatLng(-33.883172899638311, 151.150838238009328), + LatLng(-33.894158309528244, 151.14223647675135), + LatLng(-33.904812805307806, 151.155999294764086), + LatLng(-33.901517742631846, 151.178099204457737), + ], + ], + fillColor: "#FF0000", + fillOutlineColor: "#000000", + ), + ); + } + + @override + Widget build(BuildContext context) { + return MapboxMap( + accessToken: MapsDemo.ACCESS_TOKEN, + annotationOrder: [ + AnnotationType.fill, + AnnotationType.line, + AnnotationType.circle, + AnnotationType.symbol, + ], + onMapCreated: _onMapCreated, + onStyleLoadedCallback: _onStyleLoaded, + initialCameraPosition: const CameraPosition( + target: center, + zoom: 12.0, + ), + ); + } +} From cf79708aca269695a052a7cc6d5129792a6c15e7 Mon Sep 17 00:00:00 2001 From: Felix Horvat Date: Wed, 10 Nov 2021 10:20:09 +0100 Subject: [PATCH 2/2] refactoring --- .../lib/{overlay_annotations.dart => click_annotations.dart} | 1 - example/lib/main.dart | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) rename example/lib/{overlay_annotations.dart => click_annotations.dart} (99%) diff --git a/example/lib/overlay_annotations.dart b/example/lib/click_annotations.dart similarity index 99% rename from example/lib/overlay_annotations.dart rename to example/lib/click_annotations.dart index 6e55e693a..a0a490126 100644 --- a/example/lib/overlay_annotations.dart +++ b/example/lib/click_annotations.dart @@ -63,7 +63,6 @@ class ClickAnnotationBodyState extends State { } void _onLineTapped(Line line) { - print("line"); _showSnackBar('line', line.id); } diff --git a/example/lib/main.dart b/example/lib/main.dart index e3ca7d720..0cb4e7eda 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -17,7 +17,7 @@ import 'line.dart'; import 'local_style.dart'; import 'map_ui.dart'; import 'move_camera.dart'; -import 'overlay_annotations.dart'; +import 'click_annotations.dart'; import 'page.dart'; import 'place_circle.dart'; import 'place_source.dart';