Google地图:用户更改样式模式暗/亮模式后,即刻更改样式

问题描述

我是 Swift 语言和 iOS 开发的新手。我一直在开发一个处理 Google Maps API 的应用。在此应用中,我想确保一旦用户将设备的“暗”模式更改为“亮”模式,反之亦然地图样式自动更改。我检查了很多资源,找不到解决方案。因此,我决定执行与颜色按钮或其他任何子视图选择相同的步骤。

比方说,我想使按钮的颜色适应 Dark / Light 模式,并确保颜色自动改变,我将定义如下内容

extension UIColor {
    static var buttonTintColor: UIColor {
        if #available(iOS 13.0,*) {
            return UIColor { (traits) -> UIColor in
                // Return one of two colors depending on light or dark mode
                return traits.userInterfaceStyle == .dark ?
                    UIColor(red: 0,green: 0,blue: 0,alpha: 1) : // dark
                    UIColor(red: 1,green: 1,blue: 1,alpha: 1) // light
            }
        } else {
            // Same old color used for iOS 12 and earlier
            return UIColor(red: 0.3,green: 0.4,blue: 0.5,alpha: 1)
        }
    }
}

我只需要定义一个UIColor类型变量即可根据当前样式模式进行更改。

因此,我想将相同的想法实现为Google Maps样式,并定义如下内容

extension GMSMapStyle {
    static var customStyleTranstion: GMSMapStyle? {
        var mapStyle: GMSMapStyle
        if #available(iOS 13.0,*) {
            return GMSMapStyle { (traits) -> GMSMapStyle in
                // Return one of two colors depending on light or dark mode
                if traits.userInterfaceStyle == .dark {
                    do {
                      // Set the map style by passing the URL of the local file.
                      if let styleURL = Bundle.main.url(forResource: "style",withExtension: "json") {
                        mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
                        return mapStyle
                      } else {
                        NSLog("Unable to find style.json")
                        return nil
                      }
                    } catch {
                      NSLog("One or more of the map styles Failed to load. \(error)")
                        return nil
                    }
                } else {
                    do {
                      // Set the map style by passing the URL of the local file.
                      if let styleURL = Bundle.main.url(forResource: "style2",withExtension: "json") {
                        mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
                        return mapStyle
                      } else {
                        NSLog("Unable to find style.json")
                        return nil
                      }
                    } catch {
                      NSLog("One or more of the map styles Failed to load. \(error)")
                        return nil
                    }
                }
            }
        } else {
            // Same old color used for iOS 12 and earlier
            do {
              // Set the map style by passing the URL of the local file.
              if let styleURL = Bundle.main.url(forResource: "style",withExtension: "json") {
                mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
                return mapStyle
              } else {
                NSLog("Unable to find style.json")
                return nil
              }
            } catch {
              NSLog("One or more of the map styles Failed to load. \(error)")
                return nil
            }
            
        }
    }
}

但是,我无法使其正常工作,并且出现错误消息:“在调用初始化程序时没有完全匹配”。

(注意:请不要忘记导入“ GoogleMaps ”)

PS。 我想使该应用程序具有适应性,以便用户无需重新启动该应用程序即可更改地图的样式。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...