如何在SupportMapFragment中使用暗黑模式?

8
我在我的代码中使用 SupportMapFragment (class="com.google.android.gms.maps.SupportMapFragment") 来实现地图功能。
当我切换应用程序为黑暗或夜间模式时,除了地图片段以外的所有内容都是黑色的。
有没有办法在 SupportMapFragment 中使用黑暗模式呢?
谢谢。
我正在使用浅色模式地图。 enter image description here 我想要如下的夜间模式地图。 enter image description here

分享你的代码,以及你已经完成了什么。 - Gaurav
你找到了什么吗? - Shahzeb
2个回答

18

您可以使用这个样式API(夜间模式或任何其他想要的样式)来设置您的地图样式: https://developers.google.com/maps/documentation/android-sdk/styling

在拥有GoogleMap对象的类中,您可以使用以下样式之一: https://github.com/googlemaps/android-samples/blob/master/ApiDemos/java/app/src/main/res/raw/mapstyle_night.json

他们甚至有一个漂亮的预览编辑器可以创建这个json: https://mapstyle.withgoogle.com/

用法

MapStyleOptions style = MapStyleOptions.loadRawResourceStyle(this, R.raw.mapstyle_night);
GoogleMap mMap; // assigned in onMapReady()
mMap.setMapStyle(style);

1
不幸的是,这仍然会使未加载的瓷砖变白 - 这会在片段加载时造成极其不愉快的白色闪烁。 - Mavrik
@Mavrik 你有没有找到解决白瓷砖问题的办法? - undefined

-1

你需要在地图上添加一个带有暗透明背景的View

 <RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/myActivity_mapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"/>

    <View
        android:id="@+id/myActivity_mapFragmentDark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#55000000"/>

</RelativeLayout>

这样你就可以显示/隐藏这个视图来调暗地图:

mapFragmentDark.setVisibility(View.VISIBLE);

或者

mapFragmentDark.setVisibility(View.GONE);

谢谢提供其他地图变暗的选项。但我想使用黑暗地图。我已经附上了我想要的地图截图。 - Tejas Patel
据我所知,android-map目前不支持此功能。唯一的方法是使用下面的代码将其变暗。 - Oleg Khalidov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接