自定義 Google 地圖樣式

地圖樣式

使用以下程式碼,Google 地圖會附帶一組不同的樣式:

// Sets the map type to be "hybrid"
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);

不同的地圖樣式是:

正常

map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

典型的路線圖。展示了道路,一些人造特徵以及河流等重要的自然景觀。道路和功能標籤也可見。

StackOverflow 文件

混合動力

map.setMapType(GoogleMap.MAP_TYPE_HYBRID);

新增了道路地圖的衛星照片資料。道路和功能標籤也可見。

https://i.stack.imgur.com/7s9r1.jpg

衛星

map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

衛星照片資料。道路和功能標籤不可見。

https://i.stack.imgur.com/qmMzD.jpg

地形

map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

地形資料。地圖包括顏色,輪廓線和標籤以及透視陰影。一些道路和標籤也可見。

https://i.stack.imgur.com/cRKSl.jpg

沒有

map.setMapType(GoogleMap.MAP_TYPE_NONE);

沒有瓷磚。地圖將呈現為沒有載入圖塊的空網格。

StackOverflow 文件

其他風格選擇

室內地圖

在高縮放級別,地圖將顯示室內空間的平面圖。這些被稱為室內地圖,僅顯示普通衛星地圖型別。

啟用或禁用室內地圖,這是如何做到的:

GoogleMap.setIndoorEnabled(true).
GoogleMap.setIndoorEnabled(false).

我們可以為地圖新增自定義樣式

在 onMapReady 方法中新增以下程式碼段

mMap = googleMap;
    try {
        // Customise the styling of the base map using a JSON object defined
        // in a raw resource file.
        boolean success = mMap.setMapStyle(
                MapStyleOptions.loadRawResourceStyle(
                        MapsActivity.this, R.raw.style_json));

        if (!success) {
            Log.e(TAG, "Style parsing failed.");
        }
    } catch (Resources.NotFoundException e) {
        Log.e(TAG, "Can't find style.", e);
    }

res 資料夾下建立一個資料夾名稱 raw 並新增樣式 json 檔案。樣式 style.json 檔案

    [
  {
    "featureType": "all",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#242f3e"
      }
    ]
  },
  {
    "featureType": "all",
    "elementType": "labels.text.stroke",
    "stylers": [
      {
        "lightness": -80
      }
    ]
  },
  {
    "featureType": "administrative",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#746855"
      }
    ]
  },
  {
    "featureType": "administrative.locality",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#d59563"
      }
    ]
  },
  {
    "featureType": "poi",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#d59563"
      }
    ]
  },
  {
    "featureType": "poi.park",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#263c3f"
      }
    ]
  },
  {
    "featureType": "poi.park",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#6b9a76"
      }
    ]
  },
  {
    "featureType": "road",
    "elementType": "geometry.fill",
    "stylers": [
      {
        "color": "#2b3544"
      }
    ]
  },
  {
    "featureType": "road",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#9ca5b3"
      }
    ]
  },
  {
    "featureType": "road.arterial",
    "elementType": "geometry.fill",
    "stylers": [
      {
        "color": "#38414e"
      }
    ]
  },
  {
    "featureType": "road.arterial",
    "elementType": "geometry.stroke",
    "stylers": [
      {
        "color": "#212a37"
      }
    ]
  },
  {
    "featureType": "road.highway",
    "elementType": "geometry.fill",
    "stylers": [
      {
        "color": "#746855"
      }
    ]
  },
  {
    "featureType": "road.highway",
    "elementType": "geometry.stroke",
    "stylers": [
      {
        "color": "#1f2835"
      }
    ]
  },
  {
    "featureType": "road.highway",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#f3d19c"
      }
    ]
  },
  {
    "featureType": "road.local",
    "elementType": "geometry.fill",
    "stylers": [
      {
        "color": "#38414e"
      }
    ]
  },
  {
    "featureType": "road.local",
    "elementType": "geometry.stroke",
    "stylers": [
      {
        "color": "#212a37"
      }
    ]
  },
  {
    "featureType": "transit",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#2f3948"
      }
    ]
  },
  {
    "featureType": "transit.station",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#d59563"
      }
    ]
  },
  {
    "featureType": "water",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#17263c"
      }
    ]
  },
  {
    "featureType": "water",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#515c6d"
      }
    ]
  },
  {
    "featureType": "water",
    "elementType": "labels.text.stroke",
    "stylers": [
      {
        "lightness": -20
      }
    ]
  }
]

要生成樣式 json 檔案,請單擊此連結 StackOverflow 文件