iosswiftxcodegoogle-mapsgmsmapview

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


Need some help regarding the themes of the Google map sdk . I have used a JSON file name as style2.json for calling the night mode of google map . The theme is changing properly from standard to the night mode . The only issue is i want to reconvert the Night mode map to the standard Map theme with all my placemarkers. Plese find the attached code

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)")
        }

I have used this code for Button named Night Mode i have another button Day mode .What should i do to bring map to standard theme on clicking the day mode ..


Solution

  • You can find out what the standard/default mapStyle is by printing its value when night mode is not on.

    I did exactly that, and found that it's nil, so you just need to set it to nil:

    self.myMapView.mapStyle = nil
    

    You should probably save the night style into a property or something, so that you don't have to re-read the file every time you switch to night mode.