自定义 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 文档