地图上的明亮加载瓦片和暗色风格不太搭配。有没有办法改变加载瓦片颜色?
地图上的明亮加载瓦片和暗色风格不太搭配。有没有办法改变加载瓦片颜色?
GoogleMap.setMapStyle
更改地图的主题,尝试以下方法:try {
// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
boolean success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, R.raw.style_json));
if (!success) {
Log.e(TAG, "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Can't find style. Error: ", e);
}
style_json
。[
{
"elementType": "geometry",
"stylers": [
{
"color": "#212121"
}
]
},
{
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#757575"
}
]
},
{
"elementType": "labels.text.stroke",
"stylers": [
{
"color": "#212121"
}
]
},
{
"featureType": "administrative.country",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#9e9e9e"
}
]
},
{
"featureType": "administrative.locality",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#bdbdbd"
}
]
},
{
"featureType": "poi",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#757575"
}
]
},
{
"featureType": "poi.park",
"elementType": "geometry",
"stylers": [
{
"color": "#181818"
}
]
},
{
"featureType": "poi.park",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#616161"
}
]
},
{
"featureType": "poi.park",
"elementType": "labels.text.stroke",
"stylers": [
{
"color": "#1b1b1b"
}
]
},
{
"featureType": "road",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#2c2c2c"
}
]
},
{
"featureType": "road",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#8a8a8a"
}
]
},
{
"featureType": "road.arterial",
"elementType": "geometry",
"stylers": [
{
"color": "#373737"
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry",
"stylers": [
{
"color": "#3c3c3c"
}
]
},
{
"featureType": "road.highway.controlled_access",
"elementType": "geometry",
"stylers": [
{
"color": "#4e4e4e"
}
]
},
{
"featureType": "road.local",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#616161"
}
]
},
{
"featureType": "transit",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#757575"
}
]
},
{
"featureType": "water",
"elementType": "geometry",
"stylers": [
{
"color": "#000000"
}
]
},
{
"featureType": "water",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#3d3d3d"
}
]
}
]
我曾经面临同样的问题,通过片段XML资源成功解决:
<fragment
android:id="@+id/fragment_google_map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:backgroundColor="@color/primaryBlue"/>
概念:
瓦片是一个矩形结构,它保存着图像数据。 Tile
(link) 是由 TileProvider
(link) 提供的。 TileProvider
是一个类,为 TileOverlay
(link) 提供瓦片图像。
注意:叠加层是透明的,具有 x、y 和 z 轴。就像你面前的透明玻璃一样。
TileProvider提供瓦片,就像您将书放在透明玻璃下方一样,位于TileOverlay
的z轴上有一组图层,例如GroundOverlays
,Circles
,Polylines
和Polygons
。这意味着GroundOverlays
是最后一层,并且它是最不透明的。您提供自定义图像并更改GroundOverlay
中的颜色。您还保留所有其他顶部图层的透明度、可见性等属性。通过这种方式,您可以更改瓦片颜色。
现在,您的主要任务是创建自定义GroundOverlay。您提供的GroundOverlay
图像作为BitmapDescriptor
提供。您可以使用BitmapDescriptorFactory
创建自定义的BitmapDescriptor
图像,如下所示。BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.current_position_tennis_ball)
current_position_tennis_ball drawable resource
,现在被用作你的Tile
颜色 */jpg
图像,而tileProvider
引用了该图像(实际上是一个定制的图像)private static final String MOON_MAP_URL_FORMAT =
"http://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw/%d/%d/%d.jpg";
TileProvider tileProvider /* ultimately it is an image */ = new UrlTileProvider(256, 256) {
@Override
public synchronized URL getTileUrl(int x, int y, int zoom) {
// The moon tile coordinate system is reversed. This is not normal.
int reversedY = (1 << zoom) - y - 1;
String s = String.format(Locale.US, MOON_MAP_URL_FORMAT, zoom, x, reversedY);
URL url = null;
try {
url = new URL(s);
} catch (MalformedURLException e) {
throw new AssertionError(e);
}
return url;
}
};
试试这个
map = new GMap2(document.getElementById("map"), { backgroundColor: "#000000" });
setBackgroundColor(color); //Specifies the background color of the app.
mapLabelStyle.json
的文件,并将该JSON样式复制到该文件中。[
{
"elementType": "labels",
"stylers": [
{
"lightness": 5
},
{
"visibility": "simplified"
}
]
},
{
"elementType": "labels.text",
"stylers": [
{
"color": "#400040" //You can change color of your choice
}
]
}
]
然后使用以下代码设置样式。
GoogleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this,R.raw.mapLabelStyle));
mTileOverlay = map.addTileOverlay(new TileOverlayOptions() .tileProvider(new UrlTileProvider() {...}) .transparency(0.5f));
然后 mTileOverlay.setTransparency(0.5f - mTileOverlay.getTransparency());
。 这将在“0.5f”和“0.0f”之间切换瓷砖覆盖层的透明度。 - H.P.