Skip to content

Commit

Permalink
fixed layer based feature selection (flutter-mapbox-gl#765)
Browse files Browse the repository at this point in the history
* fixed feature selection issue for ios

* fixed feature selection issue for android
  • Loading branch information
felix-ht authored Nov 17, 2021
1 parent 035d927 commit b26debb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
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)
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])
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

0 comments on commit b26debb

Please sign in to comment.