將資料集新增至地圖

選取平台: Android iOS JavaScript

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

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

必要條件

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

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

如要為資料集的地圖項目套用樣式,請將樣式函式套用至地圖的資料集地圖項目圖層將資料集與地圖樣式建立關聯時,系統會建立資料集地圖項目圖層。

請按照下列步驟操作,將資料集與您目前使用的地圖樣式建立關聯:

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

將樣式套用至資料集

如要設定資料集圖層的地圖項目樣式,請使用可接受 GMSDatasetFeature 並傳回 GMSFeatureStyle 的樣式封閉式函式,以便定義樣式屬性。接著,將樣式屬性設為樣式結束函式,其中包含樣式邏輯。

樣式結束函式必須是確定性的,並在套用時傳回一致的結果。如果任何地圖項目的樣式規格有所變更,就必須再次套用樣式。

設定筆觸、填充和點半徑

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

  • UIColor 類別定義的邊框筆劃顏色和不透明度。預設值為透明 (UIColor.clearColor)。

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

  • 填滿顏色和透明度,由 UIColor 類別定義。預設值為透明 (UIColor.clearColor)。

  • 點要素的點半徑,介於 0 到 128 像素之間。

使用簡易樣式規則

要設定地圖項目樣式,最簡單的方法就是定義常數樣式屬性,例如顏色、不透明度和線條寬度。您可以直接將地圖項目樣式選項套用至資料集地圖項目圖層,也可以搭配自訂樣式使用。

Swift

let mapView = GMSMapView(frame: .zero, mapID: GMSMapID(identifier: "YOUR_MAP_ID"), camera: GMSCameraPosition(latitude: 40.7, longitude: -74.0, zoom: 12))

let layer = mapView.datasetFeatureLayer(of: "YOUR_DATASET_ID")

// Define a style with green fill and stroke.
// Apply the style to all features in the dataset.
layer.style = { feature in
    let style = MutableFeatureStyle()
    style.fillColor = .green.withAlphaComponent(0.1)
    style.strokeColor = .green
    style.strokeWidth = 2.0
    return style
}

Objective-C

GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero mapID:[GMSMapID mapIDWithIdentifier:@"MAP_ID"] camera:[GMSCameraPosition cameraWithLatitude: 40.7 longitude: -74.0 zoom:12]];

GMSDatasetFeatureLayer *layer = [mapView datasetFeatureLayerOfDatasetID:@"YOUR_DATASET_ID"];

// Define a style with green fill and stroke.
// Apply the style to all features in the dataset.
layer.style = ^(GMSDatasetFeature *feature) {
    GMSMutableFeatureStyle *style = [GMSMutableFeatureStyle style];
    style.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.1];
    style.strokeColor = [UIColor greenColor];
    style.strokeWidth = 2.0;
    return style;
};

使用宣告式樣式規則

您可以根據地圖項目的屬性,以宣告方式設定樣式規則,並套用至整個資料集。您可以從地圖項目樣式函式傳回 nil,例如希望部分地圖項目保持隱藏時。

舉例來說,您可以使用 GMSDatasetFeature.datasetAttributes 傳回地圖項目的資料集屬性值。接著,您可以根據地圖項目的屬性自訂其樣式。

本範例會判斷資料集每個地圖項目的「highlightColor」屬性值,以控制樣式:

Swift

layer.style = { feature in
    var attributeColor: String = feature.datasetAttributes["highlightColor"]
    // Conditionalize styling based on the value of the "highlightColor" attribute.
    ...
}

Objective-C

// Apply the style to a single dataset feature.
layer.style = ^(GMSDatasetFeature *feature) {
    NSString *attributeColor = feature.datasetAttributes[@"highlightColor"];
    // Conditionalize styling based on the value of the "highlightColor" attribute.
    ...
};

移除圖層中的樣式

如要移除圖層中的樣式,請將 style 設為 null

Swift

layer.style = nil

Objective-C

layer.style = nil;

此外,也可以從地圖項目樣式函式傳回 nil (例如,希望部分地圖項目保持隱藏時)。