我正在关注iOS的示例: https://github.com/mapbox/ios-sdk-examples/blob/master/Examples/Swift/FeatureSelectionExample.swift 基本上它是这样的: 1)用户点击地图。 2)获取所选状态的名称。 3)突出显示所选状态。 这是iOS中的Swift代码:
@objc @IBAction func handleMapTap(sender: UITapGestureRecognizer) {
// Get the CGPoint where the user tapped.
let spot = sender.location(in: mapView)
// Access the features at that point within the state layer.
let features = mapView.visibleFeatures(at: spot, styleLayerIdentifiers: Set([layerIdentifier]))
// Get the name of the selected state.
if let feature = features.first, let state = feature.attribute(forKey: "name") as? String {
changeOpacity(name: state)
} else {
changeOpacity(name: "")
}
}
func changeOpacity(name: String) {
let layer = mapView.style?.layer(withIdentifier: layerIdentifier) as! MGLFillStyleLayer
// Check if a state was selected, then change the opacity of the states that were not selected.
if name.count > 0 {
layer.fillOpacity = NSExpression(format: "TERNARY(name = %@, 1, 0)", name)
} else {
// Reset the opacity for all states if the user did not tap on a state.
layer.fillOpacity = NSExpression(forConstantValue: 1)
}
}
我需要在Android中做到这一点。 我发现的最接近的示例是这样的: https://github.com/mapbox/mapbox-android-demo/blob/master/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/query/SelectBuildingActivity.java
我可以使用以下代码获取属性并从那里获取状态名称:
List<Feature> features = mapboxMap.queryRenderedFeatures(finalPoint, "building");
但是如何在Android中做到这一点?
layer.fillOpacity = NSExpression(format: "TERNARY(name = %@, 1, 0)", name)