與地圖互動 (Android SDK)

本文說明如何使用地圖事件管理員。

注意

Azure 地圖服務 Android SDK 淘汰

適用於 Android 的 Azure 地圖服務 原生 SDK 現在已被取代,將於 3/31/25 淘汰。 若要避免服務中斷,請透過 3/31/25 移轉至 Azure 地圖服務 Web SDK。 如需詳細資訊,請參閱 Azure 地圖服務 Android SDK 移轉指南

與地圖互動

地圖會透過其 events 屬性管理所有事件。 下表列出支援的地圖事件。

Event 事件處理常式格式 描述
OnCameraIdle ()

在地圖進入「閒置」狀態之前轉譯最後一個畫面之後引發:

  • 沒有任何觀景窗正在轉換。
  • 所有目前要求的圖格都已載入。
  • 所有淡出或轉換動畫都已完成。

OnCameraMove () 在因使用者互動或方法的結果,而從某個檢視動態轉換到另一個檢視期間重複引發。
OnCameraMoveCanceled () 在取消相機的移動要求時引發。
OnCameraMoveStarted (int reason) 在地圖因使用者互動或方法的結果,而開始從某個檢視轉換到另一個檢視之前引發。 事件接聽程式的 reason 引數會傳回整數值,提供相機移動起始方式的詳細資料。 下列清單概述可能的原因:
  • 1:手勢
  • 2:開發人員動畫
  • 3:API 動畫
OnClick (double lat, double lon): boolean 在地圖上的相同點按下並放開該地圖時引發。 這個事件處理常式會傳回布林值,指出是否應該取用事件,或進一步傳遞至其他事件接聽程式。
OnFeatureClick (List<Feature>): boolean 在功能上的相同點按下並放開該地圖時引發。 這個事件處理常式會傳回布林值,指出是否應該取用事件,或進一步傳遞至其他事件接聽程式。
OnLayerAdded (Layer layer) 圖層新增至地圖時引發。
OnLayerRemoved (Layer layer) 從地圖移除圖層時引發。
OnLoaded () 在已下載所有必要資源並已產生地圖的第一個視覺化完整轉譯之後,立即引發。
OnLongClick (double lat, double lon): boolean 當地圖按下、按住一段時間,然後在地圖上的相同點放開時引發。 這個事件處理常式會傳回布林值,指出是否應該取用事件,或進一步傳遞至其他事件接聽程式。
OnLongFeatureClick (List<Feature>): boolean 當地圖按下、按住一段時間,然後在功能上的相同點放開時引發。 這個事件處理常式會傳回布林值,指出是否應該取用事件,或進一步傳遞至其他事件接聽程式。
 OnReady                (AzureMap map)       一開始載入地圖、方向變更,以及載入必要地圖資源下限時,且地圖已準備好以程式設計方式互動時引發。 
OnSourceAdded (Source source) DataSourceVectorTileSource 新增至地圖時引發。
OnSourceRemoved (Source source) 從地圖移除 DataSourceVectorTileSource 時引發。
OnStyleChange () 載入或變更地圖的樣式時引發。

下列程式碼顯示如何將 OnClickOnFeatureClickOnCameraMove 事件新增至地圖。

map.events.add((OnClick) (lat, lon) -> {
    //Map clicked.

    //Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
    return true;
});

map.events.add((OnFeatureClick) (features) -> {
    //Feature clicked.

    //Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
    return true;
});

map.events.add((OnCameraMove) () -> {
    //Map camera moved.
});
map.events.add(OnClick { lat: Double, lon: Double -> 
    //Map clicked.

    //Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
    return false
})

map.events.add(OnFeatureClick { features: List<Feature?>? -> 
    //Feature clicked.

    //Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
    return false
})

map.events.add(OnCameraMove {
    //Map camera moved.
})

如需詳細資訊,請參閱《瀏覽地圖》文件,以了解如何與地圖和觸發事件互動。

將功能事件範圍設定為圖層

OnFeatureClickOnLongFeatureClick 事件新增至地圖時,圖層執行個體或圖層識別碼可以當做第二個參數傳入。 當圖層傳入時,如果事件發生在該層上,就會引發事件。 符號、泡泡、線條和多邊形圖層支援範圍設定為圖層的事件。

//Create a data source.
DataSource source = new DataSource();
map.sources.add(source);

//Add data to the data source.
source.add(Point.fromLngLat(0, 0));

//Create a layer and add it to the map.
BubbleLayer layer = new BubbleLayer(source);
map.layers.add(layer);

//Add a feature click event to the map and pass the layer ID to limit the event to the specified layer.
map.events.add((OnFeatureClick) (features) -> {
    //One or more features clicked.

    //Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
    return true;
}, layer);

//Add a long feature click event to the map and pass the layer ID to limit the event to the specified layer.
map.events.add((OnLongFeatureClick) (features) -> {
    //One or more features long clicked.

    //Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
    return true;
}, layer);
//Create a data source.
val source = DataSource()
map.sources.add(source)

//Add data to the data source.
source.add(Point.fromLngLat(0, 0))

//Create a layer and add it to the map.
val layer = BubbleLayer(source)
map.layers.add(layer)

//Add a feature click event to the map and pass the layer ID to limit the event to the specified layer.
map.events.add(
    OnFeatureClick { features: List<Feature?>? -> 
        //One or more features clicked.

        //Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
        return false
    },
    layer
)

//Add a long feature click event to the map and pass the layer ID to limit the event to the specified layer.
map.events.add(
    OnLongFeatureClick { features: List<Feature?>? -> 
         //One or more features long clicked.

        //Return true indicating if event should be consumed and not passed further to other listeners registered afterwards, false otherwise.
        return false
    },
    layer
)

下一步

請參閱下列文章中的完整程式碼範例: