Skip to content

Commit

Permalink
fixed issue with annotation click order (#748)
Browse files Browse the repository at this point in the history
* fixed issue with annotation click order on android
  • Loading branch information
felix-ht authored Nov 12, 2021
1 parent 3c0ae33 commit 3b46c2c
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 13 deletions.
36 changes: 23 additions & 13 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,6 +96,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;
Expand Down Expand Up @@ -321,19 +323,23 @@ public void setStyleString(String styleString) {
@Override
public void onStyleLoaded(@NonNull Style style) {
MapboxMapController.this.style = style;
for(String annotationType : annotationOrder) {
final List<String> orderReversed = new ArrayList<String>(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'");
Expand Down Expand Up @@ -481,36 +487,40 @@ private void addCircleLayer(String layerName,
}
}

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
Expand Down
147 changes: 147 additions & 0 deletions example/lib/click_annotations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// 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<StatefulWidget> createState() => ClickAnnotationBodyState();
}

class ClickAnnotationBodyState extends State<ClickAnnotationBody> {
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) {
_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,
),
);
}
}
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'line.dart';
import 'local_style.dart';
import 'map_ui.dart';
import 'move_camera.dart';
import 'click_annotations.dart';
import 'page.dart';
import 'place_circle.dart';
import 'place_source.dart';
Expand All @@ -42,6 +43,7 @@ final List<ExamplePage> _allPages = <ExamplePage>[
AnnotationOrderPage(),
CustomMarkerPage(),
BatchAddPage(),
ClickAnnotationPage()
];

class MapsDemo extends StatelessWidget {
Expand Down

0 comments on commit 3b46c2c

Please sign in to comment.