Skip to content
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

Define annotation order #523

Merged
merged 7 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
9 changes: 9 additions & 0 deletions android/src/main/java/com/mapbox/mapboxgl/Convert.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ static CameraPosition toCameraPosition(Object o) {
return builder.build();
}

static List<String> toAnnotationOrder(Object o) {
final List<?> data = toList(o);
List<String> annotations = new ArrayList();
for (int index = 0; index < data.size(); index++) {
annotations.add(toString(data.get(index)));
}
return annotations;
}

static boolean isScrollByCameraUpdate(Object o) {
return toString(toList(o).get(0)).equals("scrollBy");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import io.flutter.plugin.common.PluginRegistry;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.List;
import java.util.ArrayList;


class MapboxMapBuilder implements MapboxMapOptionsSink {
Expand All @@ -28,11 +30,12 @@ class MapboxMapBuilder implements MapboxMapOptionsSink {
private int myLocationTrackingMode = 0;
private int myLocationRenderMode = 0;
private String styleString = Style.MAPBOX_STREETS;
private List<String> annotationOrder = new ArrayList();

MapboxMapController build(
int id, Context context, AtomicInteger state, PluginRegistry.Registrar registrar, String accessToken) {
final MapboxMapController controller =
new MapboxMapController(id, context, state, registrar, options, accessToken, styleString);
new MapboxMapController(id, context, state, registrar, options, accessToken, styleString, annotationOrder);
controller.init();
controller.setMyLocationEnabled(myLocationEnabled);
controller.setMyLocationTrackingMode(myLocationTrackingMode);
Expand Down Expand Up @@ -170,4 +173,8 @@ public void setAttributionButtonMargins(int x, int y) {
(int) y, //bottom
});
}

public void setAnnotationOrder(List<String> annotations) {
this.annotationOrder = annotations;
}
}
30 changes: 25 additions & 5 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.List;
import java.util.ArrayList;

import static com.mapbox.mapboxgl.MapboxMapsPlugin.CREATED;
import static com.mapbox.mapboxgl.MapboxMapsPlugin.DESTROYED;
Expand Down Expand Up @@ -140,6 +142,7 @@ final class MapboxMapController
private LocationEngineCallback<LocationEngineResult> locationEngineCallback = null;
private LocalizationPlugin localizationPlugin;
private Style style;
private List<String> annotationOrder = new ArrayList();

MapboxMapController(
int id,
Expand All @@ -148,7 +151,8 @@ final class MapboxMapController
PluginRegistry.Registrar registrar,
MapboxMapOptions options,
String accessToken,
String styleStringInitial) {
String styleStringInitial,
List<String> annotationOrder) {
MapBoxUtils.getMapbox(context, accessToken);
this.id = id;
this.context = context;
Expand All @@ -165,6 +169,7 @@ final class MapboxMapController
new MethodChannel(registrar.messenger(), "plugins.flutter.io/mapbox_maps_" + id);
methodChannel.setMethodCallHandler(this);
this.registrarActivityHashCode = registrar.activity().hashCode();
this.annotationOrder = annotationOrder;
}

@Override
Expand Down Expand Up @@ -337,10 +342,25 @@ public void setStyleString(String styleString) {
@Override
public void onStyleLoaded(@NonNull Style style) {
MapboxMapController.this.style = style;
enableLineManager(style);
enableSymbolManager(style);
enableCircleManager(style);
enableFillManager(style);
for(String annotationType : annotationOrder) {
switch (annotationType) {
case "AnnotationType.fill":
enableFillManager(style);
break;
case "AnnotationType.line":
enableLineManager(style);
break;
case "AnnotationType.circle":
enableCircleManager(style);
break;
case "AnnotationType.symbol":
enableSymbolManager(style);
break;
default:
throw new IllegalArgumentException("Unknown annotation type: " + annotationType + ", must be either 'fill', 'line', 'circle' or 'symbol'");
}
}

if (myLocationEnabled) {
enableLocationComponent(style);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.List;
import java.util.ArrayList;

public class MapboxMapFactory extends PlatformViewFactory {

Expand All @@ -34,6 +36,10 @@ public PlatformView create(Context context, int id, Object args) {
CameraPosition position = Convert.toCameraPosition(params.get("initialCameraPosition"));
builder.setInitialCameraPosition(position);
}
if (params.containsKey("annotationOrder")) {
List<String> annotations = Convert.toAnnotationOrder(params.get("annotationOrder"));
builder.setAnnotationOrder(annotations);
}
return builder.build(id, context, mActivityState, mPluginRegistrar, (String) params.get("accessToken"));
}
}
159 changes: 159 additions & 0 deletions example/lib/annotation_order_maps.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:mapbox_gl/mapbox_gl.dart';

import 'main.dart';
import 'page.dart';

class AnnotationOrderPage extends ExamplePage {
AnnotationOrderPage()
: super(const Icon(Icons.layers), 'Annotation order maps');

@override
Widget build(BuildContext context) => AnnotationOrderBody();
}

class AnnotationOrderBody extends StatefulWidget {
AnnotationOrderBody();

@override
_AnnotationOrderBodyState createState() => _AnnotationOrderBodyState();
}

class _AnnotationOrderBodyState extends State<AnnotationOrderBody> {
MapboxMapController controllerOne;
MapboxMapController controllerTwo;

final LatLng center = const LatLng(36.580664, 32.5563837);

@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Card(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 0.0),
child: Column(
children: <Widget>[
const Padding(
padding: EdgeInsets.only(bottom: 5.0),
child: Text(
'This map has polygones (fill) above all other anotations (default behavior)'),
),
Center(
child: SizedBox(
width: 250.0,
height: 250.0,
child: MapboxMap(
accessToken: MapsDemo.ACCESS_TOKEN,
onMapCreated: onMapCreatedOne,
onStyleLoadedCallback: () => onStyleLoaded(controllerOne),
initialCameraPosition: CameraPosition(
target: center,
zoom: 5.0,
),
annotationOrder: const [
AnnotationType.line,
AnnotationType.symbol,
AnnotationType.circle,
AnnotationType.fill,
],
),
),
),
],
),
),
),
Card(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 0.0),
child: Column(
children: <Widget>[
const Padding(
padding: EdgeInsets.only(bottom: 5.0, top: 5.0),
child: Text(
'This map has polygones (fill) under all other anotations'),
),
Center(
child: SizedBox(
width: 250.0,
height: 250.0,
child: MapboxMap(
accessToken: MapsDemo.ACCESS_TOKEN,
onMapCreated: onMapCreatedTwo,
onStyleLoadedCallback: () => onStyleLoaded(controllerTwo),
initialCameraPosition: CameraPosition(
target: center,
zoom: 5.0,
),
annotationOrder: const [
AnnotationType.fill,
AnnotationType.line,
AnnotationType.symbol,
AnnotationType.circle,
],
),
),
),
],
),
),
),
],
);
}

void onMapCreatedOne(MapboxMapController controller) {
this.controllerOne = controller;
}

void onMapCreatedTwo(MapboxMapController controller) {
this.controllerTwo = controller;
}

void onStyleLoaded(MapboxMapController controller) {
controller.addSymbol(
SymbolOptions(
geometry: LatLng(
center.latitude,
center.longitude,
),
iconImage: "airport-15",
),
);
controller.addLine(
LineOptions(
draggable: false,
lineColor: "#ff0000",
lineWidth: 7.0,
lineOpacity: 1,
geometry: [
LatLng(35.3649902, 32.0593003),
LatLng(34.9475098, 31.1187944),
LatLng(36.7108154, 30.7040582),
LatLng(37.6995850, 33.6512083),
LatLng(35.8648682, 33.6969227),
LatLng(35.3814697, 32.0546447),
],
),
);
controller.addFill(
FillOptions(
draggable: false,
fillColor: "#008888",
fillOpacity: 0.3,
geometry: [
[
LatLng(35.3649902, 32.0593003),
LatLng(34.9475098, 31.1187944),
LatLng(36.7108154, 30.7040582),
LatLng(37.6995850, 33.6512083),
LatLng(35.8648682, 33.6969227),
LatLng(35.3814697, 32.0546447),
]
],
),
);
}
}
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:mapbox_gl_example/full_map.dart';
import 'package:mapbox_gl_example/offline_regions.dart';

import 'animate_camera.dart';
import 'annotation_order_maps.dart';
import 'full_map.dart';
import 'line.dart';
import 'map_ui.dart';
Expand All @@ -32,6 +33,7 @@ final List<ExamplePage> _allPages = <ExamplePage>[
PlaceFillPage(),
ScrollingMapPage(),
OfflineRegionsPage(),
AnnotationOrderPage(),
];

class MapsDemo extends StatelessWidget {
Expand Down
42 changes: 27 additions & 15 deletions ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
private var lineAnnotationController: MGLLineAnnotationController?
private var fillAnnotationController: MGLPolygonAnnotationController?

private var annotationOrder = [String]()

func view() -> UIView {
return mapView
}
Expand Down Expand Up @@ -63,6 +65,9 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
mapView.setCenter(camera.centerCoordinate, zoomLevel: zoom, direction: camera.heading, animated: false)
initialTilt = camera.pitch
}
if let annotationOrderArg = args["annotationOrder"] as? [String] {
annotationOrder = annotationOrderArg
}
}
}

Expand Down Expand Up @@ -665,22 +670,29 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
mapView.setCamera(camera, animated: false)
}

lineAnnotationController = MGLLineAnnotationController(mapView: self.mapView)
lineAnnotationController!.annotationsInteractionEnabled = true
lineAnnotationController?.delegate = self

symbolAnnotationController = MGLSymbolAnnotationController(mapView: self.mapView)
symbolAnnotationController!.annotationsInteractionEnabled = true
symbolAnnotationController?.delegate = self

circleAnnotationController = MGLCircleAnnotationController(mapView: self.mapView)
circleAnnotationController!.annotationsInteractionEnabled = true
circleAnnotationController?.delegate = self
for annotationType in annotationOrder {
switch annotationType {
case "AnnotationType.fill":
fillAnnotationController = MGLPolygonAnnotationController(mapView: self.mapView)
fillAnnotationController!.annotationsInteractionEnabled = true
fillAnnotationController?.delegate = self
case "AnnotationType.line":
lineAnnotationController = MGLLineAnnotationController(mapView: self.mapView)
lineAnnotationController!.annotationsInteractionEnabled = true
lineAnnotationController?.delegate = self
case "AnnotationType.circle":
circleAnnotationController = MGLCircleAnnotationController(mapView: self.mapView)
circleAnnotationController!.annotationsInteractionEnabled = true
circleAnnotationController?.delegate = self
case "AnnotationType.symbol":
symbolAnnotationController = MGLSymbolAnnotationController(mapView: self.mapView)
symbolAnnotationController!.annotationsInteractionEnabled = true
symbolAnnotationController?.delegate = self
default:
print("Unknown annotation type: \(annotationType), must be either 'fill', 'line', 'circle' or 'symbol'")
}
}

fillAnnotationController = MGLPolygonAnnotationController(mapView: self.mapView)
fillAnnotationController!.annotationsInteractionEnabled = true
fillAnnotationController?.delegate = self

mapReadyResult?(nil)
if let channel = channel {
channel.invokeMethod("map#onStyleLoaded", arguments: nil)
Expand Down
Loading