我试图阻止信息窗口消失。为了表明我已经使用了
[mapView_ setSelectedMarker:marker];
但是当用户在某处点击地图时,信息窗口就会消失。我尝试在didTapAtCoordinate
重新设置它,但在取消选择任何标记之前调用它。怎么做?
答案 0 :(得分:0)
Key Value Observing是此案例的设计模式。 This answer适合我。
- (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];
}
}
}
}
答案 1 :(得分:0)
对于想要在 Swift 4 中测试Hanton的解决方案的人,简化为只要在页面加载时始终显示信息窗口:
override func viewDidLoad() {
mapView.addObserver(self, forKeyPath: "selectedMarker", options: [.new, .old], context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "selectedMarker" {
if change?[.newKey] as? NSObject is NSNull {
mapView.selectedMarker = change?[.oldKey] as? GMSMarker
}
}
}
这确实有效,但信息窗口的短暂闪烁消失,并在用户点击时重新出现。