让数据地图项响应 click
事件,并使用它们根据用户互动情况来更改地图项的外观。
编写点击事件处理脚本
当地图项图层上发生点击事件时,Maps SDK for Android 会将 FeatureClickEvent
对象传递给事件处理脚本。
使用 FeatureClickEvent.getFeatures()
方法可获取受点击影响的地图项列表。
处理地图项图层事件
请按照以下步骤处理数据集图层上的事件。在此示例中,您将为表示所选地图项的多边形应用蓝色填充和边框。
当您调用 FeatureLayer.setFeatureStyle()
时,样式工厂函数会为数据集中的所有地图项设置地图项样式。如需在事件处理脚本中更新数据集的样式,您必须调用 FeatureLayer.setFeatureStyle()
以在所有数据集地图项上设置更新后的样式。
按照使用入门中的步骤创建新的地图 ID 和地图样式(如果您尚未执行此操作)。请务必启用数据集地图项图层。
确保您的类实现了
FeatureLayer.OnFeatureClickListener
。通过调用
FeatureLayer.addOnFeatureClickListener()
为地图项点击事件注册事件处理脚本。Kotlin
private var datasetLayer: FeatureLayer? = null // The globalid of the clicked dataset feature. var lastGlobalId: String? = null
override fun onMapReady(googleMap: GoogleMap) { // Get the DATASET feature layer. datasetLayer = googleMap.getFeatureLayer(FeatureLayerOptions.Builder() .featureType(FeatureType.DATASET) // Specify the dataset ID. .datasetId(YOUR_DATASET_ID) .build())
// Register the click event handler for the Datasets layer. datasetLayer?.addOnFeatureClickListener(this)
// Apply default style to all features on load to enable clicking. styleDatasetsLayer() }
// Define the click event handler to set lastGlobalId to globalid of selected feature. override fun onFeatureClick(event: FeatureClickEvent) { // Get the dataset feature affected by the click. val clickFeatures: MutableList<Feature> = event.features lastGlobalId = null if (clickFeatures.get(0) is DatasetFeature) { lastGlobalId = ((clickFeatures.get(0) as DatasetFeature).getDatasetAttributes().get("globalid")) // Remember to reset the Style Factory. styleDatasetsLayer() } }Java
private FeatureLayer datasetLayer; // The globalid of the clicked dataset feature. String lastgobalid = null;
@Override public void onMapReady(GoogleMap map) {
// Get the DATASET feature layer. datasetLayer = map.getFeatureLayer(new FeatureLayerOptions.Builder() .featureType(FeatureType.DATASET) // Specify the dataset ID. .datasetId(YOUR_DATASET_ID) .build());
// Register the click event handler for the Datasets layer. datasetLayer.addOnFeatureClickListener(this);
// Apply default style to all features on load to enable clicking. styleDatasetsLayer(); }
@Override // Define the click event handler. public void onFeatureClick(FeatureClickEvent event) { // Get the dataset feature affected by the click. List<Feature> clickFeatures = event.getFeatures(); lastgobalid = null; if (clickFeatures.get(0) instanceof DatasetFeature) { lastgobalid = ((DatasetFeature) clickFeatures.get(0)).getDatasetAttributes().get("globalid"); // Remember to reset the Style Factory. styleDatasetsLayer(); } }将蓝色填充颜色应用于所选地图项,将绿色填充颜色应用于所有其他地图项。只有可见地图项可供点击。
Kotlin
// Set fill and border for all features. private fun styleDatasetsLayer() { // Create the style factory function. val styleFactory = FeatureLayer.StyleFactory { feature: Feature ->
// Check if the feature is an instance of DatasetFeature. if (feature is DatasetFeature) { val globalIDs: MutableMap<String, String> = feature.getDatasetAttributes() // Determine globalid attribute. val globalID = globalIDs!!["globalid"] // Set default colors to to green. var fillColor = 0x800000ff var strokeColor = 0xff0000ff if (globalID == lastGlobalId) { // Color selected area blue. fillColor = 0x8000ff00 strokeColor = 0xff00ff00 } return@StyleFactory FeatureStyle.Builder() .fillColor(fillColor) .strokeColor(strokeColor) .build() } return@StyleFactory null }
// Apply the style factory function to the dataset feature layer. datasetLayer?.setFeatureStyle(styleFactory) }Java
// Set default green fill and border for all features. private void styleDatasetsLayer() { // Create the style factory function. FeatureLayer.StyleFactory styleFactory = (Feature feature) -> {
// Check if the feature is an instance of DatasetFeature. if (feature instanceof DatasetFeature) {
// Check if "globalid" attribute of feature is the "globalid" of clicked feature. Map<String, String> globalIDs = ((DatasetFeature) feature).getDatasetAttributes(); String globalID = globalIDs.get("globalid"); // Set default colors to green. int fillColor = 0x4000ff00; int strokeColor = 0xff00ff00; if (Objects.equals(globalID, lastgobalid)) { // Color selected area blue. fillColor = 0x400000ff; strokeColor = 0xff0000ff; } return new FeatureStyle.Builder() .fillColor(fillColor) .strokeColor(strokeColor) .strokeWidth(2) .build(); } return null; };
// Apply the style factory function to the feature layer. datasetLayer.setFeatureStyle(styleFactory); }