如何在 swift 中单击按钮时将 Google 地图的主题从夜间模式更改为标准模式

How to change the theme of Google map from Night mode to Standard mode on button click in swift

需要有关 Google 地图 sdk 主题的帮助。 我使用 JSON 文件名作为 style2.json 来调用 google 地图的夜间模式。 主题正在从标准模式正确更改为夜间模式。唯一的问题是我想用我所有的地标将夜间模式地图重新转换为标准地图主题。 请找到附加代码

do {
            // Set the map style by passing the URL of the local file.
            if let styleURL = Bundle.main.url(forResource: "style2", withExtension: "json") {
                self.myMapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
            } else {
                NSLog("Unable to find style.json")
            }
        } catch {
            NSLog("One or more of the map styles failed to load. \(error)")
        }

我已经将此代码用于名为 夜间模式 的按钮,我还有另一个按钮 白天模式 。我应该怎么做才能将地图带到单击日模式的标准主题..

当夜间模式未打开时,您可以通过打印它的值来找出 standard/default mapStyle 是什么。

我就是这么做的,发现是nil,所以你只要设置成nil:

self.myMapView.mapStyle = nil

您可能应该将夜间模式保存到 属性 或其他文件中,这样您就不必在每次切换到夜间模式时都重新读取文件。