在地圖和樣式資料集中加入資料集

選取平台: Android iOS JavaScript

本頁說明如何在地圖中新增資料集,以及如何套用樣式。

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

必要條件

請先備妥地圖 ID、地圖樣式和資料集 ID,才能繼續操作。

將資料集 ID 與地圖樣式建立關聯

如要設定資料集地圖項目的樣式,請將樣式函式套用至 資料集地圖項目圖層。資料集地圖項目圖層會在 將資料集與地圖樣式建立關聯

如何將資料集與地圖樣式建立關聯:

  1. 在 Google Cloud 控制台中,前往「Datasets」(資料集) 頁面
  2. 按一下資料集名稱。「Dataset details」(資料集詳細資料) 頁面隨即顯示。
  3. 按一下「預覽」分頁標籤。
  4. 在「Associated map style」(相關聯的地圖樣式) 部分中,按一下 新增地圖樣式
    「ADD MAP STYLE」(新增地圖樣式) 按鈕的螢幕截圖。
  5. 找出要建立關聯的地圖樣式,勾選對應的核取方塊,然後按一下「SAVE」(儲存)。

將樣式套用至資料集

如何將樣式套用至資料集:

  1. 建立會實作 FeatureLayer.StyleFactory 存取 API這個函式會定義資料集的樣式邏輯。

  2. 致電 FeatureLayer.setFeatureStyle() 對資料集中的每個地圖項目套用樣式工廠函式。

建立樣式工廠函式

樣式工廠函式會套用至資料集中的每個地圖項目 可在您設定函式圖層的函式時啟動。這個函式必須 傳回 FeatureStyle 物件,指定如何設定多邊形的樣式。

如果樣式工廠傳回 null,系統就不會算繪指定地圖項目。如要 請參閱「移除圖層的樣式」一文。

Maps SDK for Android 會傳送 Feature 設為樣式工廠函式的例項。Feature 例項代表 功能的中繼資料,方便您存取風格工廠的中繼資料 函式。

樣式工廠函式應該會傳回一致的結果 已套用。舉例來說,如果您想隨機為一組地圖項目上色,則 就不適合在地圖項目樣式函式中套用 。

因為這個函式會針對圖層中每個地圖項目執行,所以最佳化非常重要。為了避免影響算繪時間呼叫 如果地圖項目圖層不再使用,則為 FeatureLayer.setFeatureStyle(null)

您也可以呼叫 FeatureLayer.getDatasetId() 來取得資料集 ID。

設定筆劃、填滿和點半徑

為樣式工廠函式中的地圖項目設定樣式時,您可以設定以下屬性:

  • 邊框的筆劃顏色和透明度,如 Color 類別預設值為透明 (Color.TRANSPARENT)。

  • 以螢幕像素為單位的邊框筆劃寬度。預設值為 2。

  • 填滿顏色和透明度Color 類別預設值為透明 (Color.TRANSPARENT)。

  • 點地圖項目的點半徑,介於 0 到 128 像素之間。

使用簡易樣式規則

如要設定地圖項目樣式,最簡單的方法就是定義 FeatureLayer.StyleFactory 這些物件一律會建構相同的 FeatureStyle 物件 而不是每個特徵的分數直接將地圖項目樣式選項套用至資料集地圖項目圖層,或使用 並搭配 FeatureStyleFunction 使用

使用宣告式樣式規則

您可以根據地圖項目屬性,以宣告的方式設定樣式規則。 並套用至整個資料集你可以從以下位置退回 null: 地圖項目樣式函式。舉例來說,假設您要保留部分地圖項目 隱形

舉例來說,使用 DatasetFeature.getDatasetAttributes() 方法 特徵的資料集屬性 Map<String,String>。接著 根據地圖項目屬性自訂地圖項目樣式。

這個範例會決定「highlightColor」的值每個 Pod 的 以控制樣式:

Kotlin

// Get the dataset feature, so we can work with all of its attributes.
val datasetFeature: DatasetFeature = feature as DatasetFeature
// Create a switch statement based on the value of the // "highlightColor" attribute of the dataset. val attributeColor: MutableMap<String, String> = datasetFeature.getDatasetAttributes() when (attributeColor["highlightColor"]) { "Black" -> { ... } "Red" -> { ... } else -> { ... } }

Java

// Get the dataset feature, so we can work with all of its attributes.
DatasetFeature datasetFeature = (DatasetFeature) feature;
// Create a switch statement based on the value of the // "highlightColor" attribute of the dataset. Map<String, String> attributeColor = datasetFeature.getDatasetAttributes(); switch(attributeColor.get("highlightColor")) { case "Black": ... break; case "Red": ... break; default: // Color not defined. ... break; }

將樣式套用至資料集地圖項目圖層

這個範例會將樣式工廠函式套用至資料集內的多邊形 地圖項目圖層樣式工廠函式會套用自訂的填滿和筆劃樣式 轉換成多邊形:

  1. 如果您還沒有這樣做,請按照 開始使用 建立新的地圖 ID 和地圖樣式。請務必啟用資料集 地圖項目圖層

  2. 在地圖初始化時,取得資料集地圖項目圖層的參照。

    Kotlin

    private var datasetLayer: FeatureLayer? = 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())
    // Apply style factory function to DATASET layer. styleDatasetsLayer() }

    Java

    private FeatureLayer datasetLayer;
    @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());
    // Apply style factory function to DATASET layer. styleDatasetsLayer(); }

  3. 建立樣式工廠函式並套用至資料集 地圖項目圖層

    下例將相同的填滿和筆觸套用至所有地圖項目

    Kotlin

    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) {
    return@StyleFactory FeatureStyle.Builder() // Define a style with green fill at 50% opacity and // solid green border. .fillColor(0x8000ff00.toInt()) .strokeColor(0xff00ff00.toInt()) .strokeWidth(2F) .build() } return@StyleFactory null }
    // Apply the style factory function to the feature layer. datasetLayer?.setFeatureStyle(styleFactory) }

    Java

    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) {
    return new FeatureStyle.Builder() // Define a style with green fill at 50% opacity and solid green border. .fillColor(0x8000ff00) .strokeColor(0xff00ff00) .strokeWidth(2F) .build(); } return null; };
    // Apply the style factory function to the feature layer. datasetLayer.setFeatureStyle(styleFactory); }

移除圖層中的樣式

如要移除圖層中的樣式,請呼叫 FeatureLayer.setFeatureStyle(null)

也可以從風格工廠傳回 null,例如: 您希望讓某些地圖項目保持隱藏。