Search code examples
swiftgoogle-maps-sdk-ios

Hide Google Map info window when map is moved with Swift


So with my app, I'm displaying a custom info window on a GMS Marker. This info window takes up the top half of the screen and I don't want it moving with the Marker. So I want to hide the info window as soon as the map has moved. How can I do this?


Solution

  • You could make your UIViewController conform to GMSMapViewDelegate and then implement one of the methods listed here: https://developers.google.com/maps/documentation/ios-sdk/reference/protocol_g_m_s_map_view_delegate-p
    I guess the first one (mapView:willMove:) would work for you. I would use a print statement (or breakpoint) to check, whether the method is triggered as expected. To hide the current info window, you should be able to set mapView.selectedMarker = nil.
    To sum up, your code might look like this:

    import UIKit
    import GoogleMaps
    
    class ViewController: UIViewController {
        
        //declare your map, etc.
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // setup your map
        }
    }
    
    extension ViewController: GMSMapViewDelegate {
        func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
            if gesture { //The map was moved by the user
                //mapView.selectedMarker = nil
            }
        }
    }