更新 iOS Google Maps mapStyle 环境 colorScheme 更改(深色模式)
Update iOS Google Maps mapStyle on environment colorScheme change (dark mode)
在浅色模式和深色模式之间更改设备设置时,我的 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)")
}
}
这里有一个适合你的方法:
struct ContentView: View {
@Environment(\.colorScheme) var colorScheme
var body: some View {
MapView(colorScheme: colorScheme)
}
}
struct MapView: UIViewRepresentable {
var colorScheme: 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(colorScheme: colorScheme)
}
func applyColorSchemeToMap(colorScheme: ColorScheme) {
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)")
}
}
}
我发现了问题。 MapView 必须符合 UIViewControllerRepresentable,而不是 UIViewRepresentable。
可在此处找到有关如何实现此目的(使用 ViewControllerBridge)的指南:
https://developers.google.com/codelabs/maps-platform/maps-platform-ios-swiftui#0
在浅色模式和深色模式之间更改设备设置时,我的 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)")
}
}
这里有一个适合你的方法:
struct ContentView: View {
@Environment(\.colorScheme) var colorScheme
var body: some View {
MapView(colorScheme: colorScheme)
}
}
struct MapView: UIViewRepresentable {
var colorScheme: 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(colorScheme: colorScheme)
}
func applyColorSchemeToMap(colorScheme: ColorScheme) {
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)")
}
}
}
我发现了问题。 MapView 必须符合 UIViewControllerRepresentable,而不是 UIViewRepresentable。
可在此处找到有关如何实现此目的(使用 ViewControllerBridge)的指南:
https://developers.google.com/codelabs/maps-platform/maps-platform-ios-swiftui#0