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
Changes from 1 commit
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
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