雲端式地圖樣式設定的 JSON 結構定義可讓您使用 JSON 自訂地圖,就像透過樣式編輯器介面一樣。本文說明 JSON 結構定義,以及如何進行 JSON 樣式宣告。
如要瞭解如何在樣式編輯器中使用 JSON 編輯地圖樣式,或匯入及匯出地圖樣式,請參閱「搭配雲端式地圖樣式使用 JSON」。
查看 JSON 樣式宣告範例
下列 JSON 樣式宣告會設定背景顏色,然後定義搜尋點、公園和水域的樣式,並隱藏餐飲場所的標籤。
{
"variant": "light",
"styles": [
{
"id": "natural.land",
"geometry": {
"fillColor": "#f7e3f7"
}
},
{
"id": "natural.water",
"geometry": {
"fillColor": "#d4b2ff"
},
"label": {
"textFillColor": "#3d2163",
"textStrokeColor": "#f0e1ff"
}
},
{
"id": "pointOfInterest",
"label": {
"pinFillColor": "#e0349a",
"textFillColor": "#a11e6e",
"textStrokeColor": "#ffd9f0"
}
},
{
"id": "pointOfInterest.emergency.hospital",
"geometry": {
"fillColor": "#ffe3e3"
}
},
{
"id": "pointOfInterest.foodAndDrink",
"label": {
"visible": false
}
},
{
"id": "pointOfInterest.recreation.park",
"geometry": {
"fillColor": "#f9b9d2"
}
}
]
}
JSON 物件
JSON 樣式宣告由頂層物件和樣式規則陣列組成。
- 頂層設定 (選用) - 全域樣式設定,例如
backgroundColor和variant。 styles- 樣式規則物件的陣列,可包含下列項目:id:修改此樣式時要選取的地圖項目 (例如pointOfInterest.recreation.park)。geometry(選用) - 地圖項目的幾何元素和要套用的樣式規則 (例如fillColor)。label(選用) - 地圖項目的文字或圖釘標籤,以及要套用的樣式規則 (例如textStrokeColor)。
頂層設定
下表中的屬性適用於整個地圖樣式。
| 屬性 | 類型 | 說明 | 範例 |
|---|---|---|---|
|
字串 | 使用 #RRGGBB 十六進位字串自訂地圖應用程式的背景顏色。這項設定不支援變更不透明度。 | "#002211" |
|
「light」|「dark」 | 指定淺色或深色模式。如未指定,預設為「light」。 | 「light」 |
|
布林值 | 如要啟用單色模式,請使用 true 取得地圖的灰階版本。 |
|
樣式規則物件
本節說明定義 styles 陣列中樣式規則物件的屬性,可自訂地圖功能。每個樣式規則物件都必須包含下列項目:
id屬性。geometry或label元素,並定義相關聯的樣式屬性。
id (地圖功能)
id 屬性會指定要設定樣式的地圖項目。屬性名稱是樣式編輯器中地圖特徵名稱的駝峰式大小寫版本。
地圖項目會形成類別樹狀結構。如果指定父項地圖項目類型 (例如 pointOfInterest),則您指定的父項樣式會套用至該父項的所有子項,例如 pointOfInterest.retail 和 pointOfInterest.lodging。詳情請參閱「地圖項目階層」一文。
可用的 id 屬性清單
可用的 id 屬性如下:
pointOfInterestpointOfInterest.emergencypointOfInterest.emergency.firepointOfInterest.emergency.hospitalpointOfInterest.emergency.pharmacypointOfInterest.emergency.policepointOfInterest.entertainmentpointOfInterest.entertainment.artspointOfInterest.entertainment.casinopointOfInterest.entertainment.cinemapointOfInterest.entertainment.historicpointOfInterest.entertainment.museumpointOfInterest.entertainment.themeParkpointOfInterest.entertainment.touristAttractionpointOfInterest.foodAndDrinkpointOfInterest.foodAndDrink.barpointOfInterest.foodAndDrink.cafepointOfInterest.foodAndDrink.restaurantpointOfInterest.foodAndDrink.winerypointOfInterest.landmarkpointOfInterest.lodgingpointOfInterest.recreationpointOfInterest.recreation.beachpointOfInterest.recreation.boatingpointOfInterest.recreation.fishingpointOfInterest.recreation.golfCoursepointOfInterest.recreation.hotSpringpointOfInterest.recreation.natureReservepointOfInterest.recreation.parkpointOfInterest.recreation.peakpointOfInterest.recreation.sportsComplexpointOfInterest.recreation.sportsFieldpointOfInterest.recreation.trailheadpointOfInterest.recreation.zoopointOfInterest.retailpointOfInterest.retail.grocerypointOfInterest.retail.shoppingpointOfInterest.servicepointOfInterest.service.atmpointOfInterest.service.bankpointOfInterest.service.carRentalpointOfInterest.service.evChargingpointOfInterest.service.gasStationpointOfInterest.service.parkingLotpointOfInterest.service.postOfficepointOfInterest.service.restStoppointOfInterest.service.restroompointOfInterest.transitpointOfInterest.transit.airportpointOfInterest.otherpointOfInterest.other.bridgepointOfInterest.other.cemeterypointOfInterest.other.governmentpointOfInterest.other.librarypointOfInterest.other.militarypointOfInterest.other.placeOfWorshippointOfInterest.other.schoolpointOfInterest.other.townSquarepoliticalpolitical.countryOrRegionpolitical.borderpolitical.reservationpolitical.stateOrProvincepolitical.citypolitical.sublocalitypolitical.neighborhoodpolitical.landParcelinfrastructureinfrastructure.buildinginfrastructure.building.commercialinfrastructure.businessCorridorinfrastructure.roadNetworkinfrastructure.roadNetwork.noTrafficinfrastructure.roadNetwork.noTraffic.pedestrianMallinfrastructure.roadNetwork.noTraffic.trailinfrastructure.roadNetwork.noTraffic.trail.pavedinfrastructure.roadNetwork.noTraffic.trail.unpavedinfrastructure.roadNetwork.parkingAisleinfrastructure.roadNetwork.rampinfrastructure.roadNetwork.roadinfrastructure.roadNetwork.road.arterialinfrastructure.roadNetwork.road.highwayinfrastructure.roadNetwork.road.localinfrastructure.roadNetwork.road.noOutletinfrastructure.roadNetwork.roadShieldinfrastructure.roadNetwork.roadSigninfrastructure.roadNetwork.roadDetailinfrastructure.roadNetwork.roadDetail.surfaceinfrastructure.roadNetwork.roadDetail.crosswalkinfrastructure.roadNetwork.roadDetail.sidewalkinfrastructure.roadNetwork.roadDetail.intersectioninfrastructure.railwayTrackinfrastructure.railwayTrack.commercialinfrastructure.railwayTrack.commuterinfrastructure.transitStationinfrastructure.transitStation.bicycleShareinfrastructure.transitStation.busStationinfrastructure.transitStation.ferryTerminalinfrastructure.transitStation.funicularStationinfrastructure.transitStation.gondolaStationinfrastructure.transitStation.monorailinfrastructure.transitStation.railStationinfrastructure.transitStation.railStation.subwayStationinfrastructure.transitStation.railStation.tramStationinfrastructure.urbanAreanaturalnatural.continentnatural.archipelagonatural.islandnatural.landnatural.land.landCovernatural.land.landCover.cropsnatural.land.landCover.dryCropsnatural.land.landCover.forestnatural.land.landCover.icenatural.land.landCover.sandnatural.land.landCover.shrubnatural.land.landCover.tundranatural.waternatural.water.oceannatural.water.lakenatural.water.rivernatural.water.othernatural.base
元素
元素是指地圖項目的子群組。舉例來說,道路是由地圖上的圖形線條 (geometry),以及標註道路名稱的文字 (label) 這兩個元素所組成。
以下為可用的元素 (但請注意,特定地圖項目可能不支援任何、部分或所有元素):
geometry:選取指定地圖項目的所有幾何元素 (例如多邊形、折線)。label:選取指定地圖項目的所有標籤元素 (例如文字、圖釘)。
樣式工具
樣式函數可定義地圖項目各元素的樣式規則。
舉例來說,您可以為建築物足跡的每個元素設定樣式,如下所示:
建築物的 geometry 樣式設定器範例 |
建築物的 label 樣式設定器範例 |
|---|---|
| 是否要在地圖上隱藏或顯示建築物足跡多邊形。 | 是否要隱藏或顯示建築物標籤。 |
| 多邊形填滿顏色和不透明度。 | 文字填滿顏色和不透明度。 |
| 框線顏色、不透明度和寬度。 | 文字筆劃顏色和不透明度。 |
本節說明 geometry 和 label 元素可用的不同樣式選項。
geometry 樣式工具
下表列出所有可用的幾何樣式。
| Styler | 類型 | 說明 |
|---|---|---|
|
布林值 | 如要隱藏地圖項目的多邊形或折線,請設為 false。 |
|
字串 | 使用 RGB 十六進位字串自訂多邊形或折線的顏色。 |
|
float | 自訂多邊形或折線的不透明度,0 為透明,1 為不透明。 |
|
字串 | 使用 RGB 十六進位字串自訂外框顏色。 |
|
float | 自訂外框的不透明度,0 為透明,1 為不透明。 |
|
float | 自訂輪廓粗細,範圍為 0 到 8。 |
詳情請參閱「多邊形和折線」。
label 樣式工具
下表列出所有可用的標籤樣式設定工具。
| Styler | 類型 | 說明 |
|---|---|---|
|
布林值 | 如要隱藏地圖項目的標籤,請設為 false。 |
|
字串 | 使用 RGB 十六進位字串自訂文字標籤的顏色。 |
|
float | 自訂文字標籤的不透明度,0 為透明,1 為不透明。 |
|
字串 | 使用 RGB 十六進位字串自訂外框顏色。 |
|
float | 自訂外框的不透明度,0 為透明,1 為不透明。 |
|
float | 自訂輪廓粗細,範圍為 0 到 8。 |
|
字串 | 使用 RGB 十六進位字串自訂圖釘顏色。 |
詳情請參閱「圖示和文字標籤」。
鍵縮放設定
您可以為所有 Keyzoom 等級的特徵設定單一樣式,也可以為不同 Keyzoom 等級指定不同樣式。如果只提供一種樣式,系統會從 z0 開始,將該樣式用於所有 keyzoom 等級。如果您為不同 keyzoom 等級提供樣式,系統會從該縮放等級套用樣式,直到您定義下一個等級為止。
如要為樣式設定 keyzoom 級別,請在樣式器屬性中定義 z0 到 z22 的 keyzoom 級別,然後自訂樣式器。
在下列範例中,水色在 keyzoom 等級 0 到 5 為黑色,在 keyzoom 等級 6 到 11 為深灰色,從 keyzoom 等級 12 開始為淺灰色。
{
"id": "natural.water",
"geometry": {
"fillColor": {
"z0": "#000000",
"z6": "#666666",
"z12": "#cccccc"
}
}
}
詳情請參閱「設定縮放等級的樣式」一文。
限制
您可以使用 JSON 設定 Google Cloud 控制台中的幾乎所有項目樣式,但「地圖設定」選單中的下列功能除外:
如要使用這些功能,請在「地圖設定」選單中選取偏好設定。