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

Support filtering on addLayer #1024

Merged
merged 2 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 38 additions & 5 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ private void addSymbolLayer(
if (maxZoom != null) {
symbolLayer.setMaxZoom(maxZoom);
}
if (filter != null) {
symbolLayer.setFilter(filter);
}
if (belowLayerId != null) {
style.addLayerBelow(symbolLayer, belowLayerId);
} else {
Expand Down Expand Up @@ -435,6 +438,9 @@ private void addLineLayer(
if (maxZoom != null) {
lineLayer.setMaxZoom(maxZoom);
}
if (filter != null) {
lineLayer.setFilter(filter);
}
if (belowLayerId != null) {
style.addLayerBelow(lineLayer, belowLayerId);
} else {
Expand Down Expand Up @@ -466,6 +472,9 @@ private void addFillLayer(
if (maxZoom != null) {
fillLayer.setMaxZoom(maxZoom);
}
if (filter != null) {
fillLayer.setFilter(filter);
}
if (belowLayerId != null) {
style.addLayerBelow(fillLayer, belowLayerId);
} else {
Expand Down Expand Up @@ -497,6 +506,9 @@ private void addCircleLayer(
if (maxZoom != null) {
circleLayer.setMaxZoom(maxZoom);
}
if (filter != null) {
circleLayer.setFilter(filter);
}
if (belowLayerId != null) {
style.addLayerBelow(circleLayer, belowLayerId);
} else {
Expand All @@ -505,7 +517,12 @@ private void addCircleLayer(
if (enableInteraction) {
interactiveFeatureLayerIds.add(layerName);
}
;
}

private Expression parseFilter(String filter) {
JsonParser parser = new JsonParser();
JsonElement filterJsonElement = parser.parse(filter);
return filterJsonElement.isJsonNull() ? null : Expression.Converter.convert(filterJsonElement);
}

private void addRasterLayer(
Expand Down Expand Up @@ -825,9 +842,13 @@ public void onError(@NonNull String message) {
final String sourceLayer = call.argument("sourceLayer");
final Double minzoom = call.argument("minzoom");
final Double maxzoom = call.argument("maxzoom");
final String filter = call.argument("filter");
final boolean enableInteraction = call.argument("enableInteraction");
final PropertyValue[] properties =
LayerPropertyConverter.interpretSymbolLayerProperties(call.argument("properties"));

Expression filterExpression = parseFilter(filter);

addSymbolLayer(
layerId,
sourceId,
Expand All @@ -837,7 +858,7 @@ public void onError(@NonNull String message) {
maxzoom != null ? maxzoom.floatValue() : null,
properties,
enableInteraction,
null);
filterExpression);
updateLocationComponentLayer();

result.success(null);
Expand All @@ -851,9 +872,13 @@ public void onError(@NonNull String message) {
final String sourceLayer = call.argument("sourceLayer");
final Double minzoom = call.argument("minzoom");
final Double maxzoom = call.argument("maxzoom");
final String filter = call.argument("filter");
final boolean enableInteraction = call.argument("enableInteraction");
final PropertyValue[] properties =
LayerPropertyConverter.interpretLineLayerProperties(call.argument("properties"));

Expression filterExpression = parseFilter(filter);

addLineLayer(
layerId,
sourceId,
Expand All @@ -863,7 +888,7 @@ public void onError(@NonNull String message) {
maxzoom != null ? maxzoom.floatValue() : null,
properties,
enableInteraction,
null);
filterExpression);
updateLocationComponentLayer();

result.success(null);
Expand All @@ -877,9 +902,13 @@ public void onError(@NonNull String message) {
final String sourceLayer = call.argument("sourceLayer");
final Double minzoom = call.argument("minzoom");
final Double maxzoom = call.argument("maxzoom");
final String filter = call.argument("filter");
final boolean enableInteraction = call.argument("enableInteraction");
final PropertyValue[] properties =
LayerPropertyConverter.interpretFillLayerProperties(call.argument("properties"));

Expression filterExpression = parseFilter(filter);

addFillLayer(
layerId,
sourceId,
Expand All @@ -889,7 +918,7 @@ public void onError(@NonNull String message) {
maxzoom != null ? maxzoom.floatValue() : null,
properties,
enableInteraction,
null);
filterExpression);
updateLocationComponentLayer();

result.success(null);
Expand All @@ -903,9 +932,13 @@ public void onError(@NonNull String message) {
final String sourceLayer = call.argument("sourceLayer");
final Double minzoom = call.argument("minzoom");
final Double maxzoom = call.argument("maxzoom");
final String filter = call.argument("filter");
final boolean enableInteraction = call.argument("enableInteraction");
final PropertyValue[] properties =
LayerPropertyConverter.interpretCircleLayerProperties(call.argument("properties"));

Expression filterExpression = parseFilter(filter);

addCircleLayer(
layerId,
sourceId,
Expand All @@ -915,7 +948,7 @@ public void onError(@NonNull String message) {
maxzoom != null ? maxzoom.floatValue() : null,
properties,
enableInteraction,
null);
filterExpression);
updateLocationComponentLayer();

result.success(null);
Expand Down
2 changes: 1 addition & 1 deletion example/lib/layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class LayerState extends State {
'green'
], fillOpacity: 0.4),
belowLayerId: "water",
filter: ['==', 'id', filteredId],
);

await controller.addLineLayer(
Expand Down Expand Up @@ -143,7 +144,6 @@ class LayerState extends State {
controller.setGeoJsonSource("moving", _movingFeature(t.tick / 2000));
});

controller.setFilter('fills', ['==', 'id', filteredId]);
filterTimer = Timer.periodic(Duration(seconds: 3), (t) {
filteredId = filteredId == 0 ? 1 : 0;
controller.setFilter('fills', ['==', 'id', filteredId]);
Expand Down
Loading