【发布时间】:2021-12-03 13:40:05
【问题描述】:
在浅色模式和深色模式之间更改设备设置时,我的 Google 地图 mapStyle 没有更新。其他视图成功切换到所选模式,只是地图没有变化。
当我的应用重新启动时,mapStyle 确实会正确更改。
struct MapView: UIViewRepresentable {
@Environment(\.colorScheme) var colorScheme
private let mapView = GMSMapView(frame: .zero)
private let defaultZoomLevel: Float = 10
func makeUIView(context: Context) -> GMSMapView {
mapView.delegate = context.coordinator
applyColorSchemeToMap()
return mapView
}
func updateUIView(_ mapView: GMSMapView, context: Context) {
applyColorSchemeToMap()
}
func applyColorSchemeToMap() {
do {
if let styleURL = Bundle.main.url(forResource: colorScheme == .dark ? "night_map_style" : "map_style", withExtension: "json") {
mapView.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)")
}
}
【问题讨论】:
标签: swift swiftui google-maps-sdk-ios