處理點擊事件

選取平台: Android iOS JavaScript

請將資料地圖項目設計成能夠回應 click 事件,並使用這些地圖項目,根據使用者互動來變更地圖項目的外觀。

這張螢幕截圖顯示已設定樣式的多邊形資料。

編寫點擊事件處理常式

地圖項目圖層上發生點擊事件時,Maps SDK for Android 會將 FeatureClickEvent 物件傳遞至事件處理常式。

使用 FeatureClickEvent.getFeatures() 方法取得受點擊影響的功能清單。

處理地圖項目圖層事件

請按照下列步驟處理資料集圖層上的事件。在本例中,您將為代表所選地圖項目的多邊形套用藍色填滿和邊框。

當您呼叫 FeatureLayer.setFeatureStyle() 時,樣式工廠函式會為資料集中的所有地圖項目設定地圖項目樣式。如要在事件處理常式中更新資料集的樣式,您必須呼叫 FeatureLayer.setFeatureStyle(),為所有資料集地圖項目設定更新後的樣式。

  1. 如果尚未建立新的地圖 ID 和地圖樣式,請按照「開始使用」一文中的步驟進行。請務必啟用資料集地圖項目圖層。

  2. 請確認您的類別實作 FeatureLayer.OnFeatureClickListener

  3. 呼叫 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(); } }

  4. 將藍色填滿顏色套用至所選特徵,並將綠色套用至所有其他特徵。只有可見的地圖項目可供點選。

    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); }