Search code examples
iosobjective-cgoogle-mapsgoogle-maps-sdk-ios

Google map ios - how to keep the marker info window always displayed?


I'm trying to keep the info window from disappearing. To show it I've used

[mapView_ setSelectedMarker:marker];

But when the user taps the map somewhere the info window disappears. I've tried re-setting it at didTapAtCoordinate but this is called before deselecting any markers. How to do it?


Solution

  • Key Value Observing is the design pattern for this case. This answer works for me.

    - (void)viewDidLoad {
      [super viewDidLoad];
      self.shouldShowInfoView = NO;
      [self.mapView addObserver:self
                forKeyPath:@"selectedMarker"
                   options:(NSKeyValueObservingOptionNew |
                            NSKeyValueObservingOptionOld)
                   context:NULL];
    }
    
    - (void)needToShowInfoView {
      self.shouldShowInfoView = YES;
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath
                          ofObject:(id)object
                            change:(NSDictionary *)change
                           context:(void *)context {
    
      if ([keyPath isEqual:@"selectedMarker"]) {
        if (self.shouldShowInfoView) {
          if ([[change objectForKey:NSKeyValueChangeNewKey] isKindOfClass:[NSNull class]]) {
            self.mapView.selectedMarker = [change objectForKey:NSKeyValueChangeOldKey];
          }
        }
      }
    }