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

fixed layer based feature selection #765

Merged
merged 3 commits into from
Nov 17, 2021
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
28 changes: 25 additions & 3 deletions android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import com.mapbox.mapboxsdk.plugins.annotation.SymbolOptions;
import com.mapbox.mapboxsdk.plugins.localization.LocalizationPlugin;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.layers.RasterLayer;
import com.mapbox.mapboxsdk.style.sources.ImageSource;
import com.mapbox.mapboxsdk.style.layers.LineLayer;
Expand Down Expand Up @@ -523,6 +524,27 @@ private String enableFillManager(@NonNull Style style, @Nullable String belowLay
return fillManager.getLayerId();
}

private Feature firstFeatureOnLayers(RectF in) {
if(style != null){
final List<Layer> layers = style.getLayers();
final List<String> layersInOrder = new ArrayList<String>();
for (Layer layer : layers){
String id = layer.getId();
if(featureLayerIdentifiers.contains(id))
layersInOrder.add(id);
}
Collections.reverse(layersInOrder);

for(String id: layersInOrder){
List<Feature> features = mapboxMap.queryRenderedFeatures(in, id);
if(!features.isEmpty()){
return features.get(0);
}
}
}
return null;
}

@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
switch (call.method) {
Expand Down Expand Up @@ -1276,10 +1298,10 @@ public boolean onMapClick(@NonNull LatLng point) {
pointf.x + 10,
pointf.y + 10
);
List<Feature> featureList = mapboxMap.queryRenderedFeatures(rectF, featureLayerIdentifiers.toArray(new String[0]));
if(!featureList.isEmpty()){
Feature feature = firstFeatureOnLayers(rectF);
if(feature != null){
final Map<String, Object> arguments = new HashMap<>(1);
arguments.put("featureId", featureList.get(0).id());
arguments.put("featureId", feature.id());
methodChannel.invokeMethod("feature#onTap", arguments);
} else {
final Map<String, Object> arguments = new HashMap<>(5);
Expand Down
25 changes: 21 additions & 4 deletions ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,25 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
return trackCameraPosition ? mapView.camera : nil

}
/*
* Scan layers from top to bottom and return the first matching feature
*/
private func firstFeatureOnLayers(at: CGPoint) -> MGLFeature? {
guard let style = mapView.style else { return nil}

// get layers in order (featureLayerIdentifiers is unordered)
felix-ht marked this conversation as resolved.
Show resolved Hide resolved
let clickableLayers = style.layers.filter{ layer in
return featureLayerIdentifiers.contains(layer.identifier)
}

for layer in clickableLayers.reversed() {
let features = mapView.visibleFeatures(at: at, styleLayerIdentifiers: [layer.identifier])
felix-ht marked this conversation as resolved.
Show resolved Hide resolved
if let feature = features.first {
return feature
}
}
return nil
}

/*
* UITapGestureRecognizer
Expand All @@ -818,15 +837,13 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
@objc @IBAction func handleMapTap(sender: UITapGestureRecognizer) {
// Get the CGPoint where the user tapped.
let point = sender.location(in: mapView)
let coordinate = mapView.convert(point, toCoordinateFrom: mapView)

let features = mapView.visibleFeatures(at: point, styleLayerIdentifiers: featureLayerIdentifiers)

if let feature = features.last, let id = feature.identifier {
if let feature = firstFeatureOnLayers(at: point), let id = feature.identifier {
channel?.invokeMethod("feature#onTap", arguments: [
"featureId": id
])
} else {
let coordinate = mapView.convert(point, toCoordinateFrom: mapView)
channel?.invokeMethod("map#onMapClick", arguments: [
"x": point.x,
"y": point.y,
Expand Down