我怎样才能添加一个button谷歌地图标记信息窗口上的iOS?

我已经添加Google地图sdk for ios到我的iPhone应用程序,我有一些自定义标记,如果点击信息窗口popup与标题,我怎么能添加一个button到这个信息窗口,所以如果按下将转到新的页面? 现在我已经尝试使用这个post来解决这个问题在Google地图SDK中的InfoWindow / Marker上添加Click事件本地iOS /目标C它不给我错误,但它不会工作。

这是我想要我的结果是: http : //www.flickr.com/photos/74719051@N05/6728157477/

链接问题的答案显示了使用MapKit的代码,因此它不适用于iOS的Google Maps SDK。

查看此问题以了解如何使用Google Maps SDK for iOS返回自定义视图:

Google地图SDK中的自定义注释视图

但是请注意,根据这个问题,它看起来像显示的内容可能是视图的可视副本而不是实际的视图,这可能会限制您可以对视图进行的交互:

添加button以查看由markerInfoWindow委托方法返回的结果

尽pipe简单地检测信息窗口上的一个水龙头,并转到不同的页面应该可以使用didTapWindowOfMarker

请注意,Google的问题跟踪器中有一项function请求为此添加了直接支持:

https://code.google.com/p/gmaps-api-issues/issues/detail?id=4961

这段代码完全是你想要的。 它的灵感来自于萨克森德鲁士推荐链接中的#9,但做法不同而且更完整。 它检测添加到我的自定义信息窗口button。 我用2个假button创build了一个自定义的infoWindow(它们实际上可以被图像replace,因为它们不会触发任何动作),我在infoWindow上添加了一个透明的UIView,它有2个真实但透明的button。 这些button将触发这些操作。

最后,当infoWindow本身移动时,我使用一些委托方法或KVC来移动覆盖的UIView。

 - (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker { [self.actionOverlayCalloutView removeFromSuperview]; UIView *calloutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, infoWindowWidth, infoWindowHeight)]; float offset = anchorSize * M_SQRT2; CGAffineTransform rotateBy45Degrees = CGAffineTransformMakeRotation(M_PI_4); UIView *arrow = [[UIView alloc] initWithFrame:CGRectMake((infoWindowWidth - anchorSize)/2.0, infoWindowHeight - offset, anchorSize, anchorSize)]; arrow.transform = rotateBy45Degrees; arrow.backgroundColor = [UIColor lightGrayColor]; [calloutView addSubview:arrow]; UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, infoWindowWidth, infoWindowHeight - offset/2)]; [contentView setBackgroundColor:[UIColor whiteColor]]; contentView.layer.cornerRadius = 5; contentView.layer.masksToBounds = YES; contentView.layer.borderColor = [UIColor lightGrayColor].CGColor; contentView.layer.borderWidth = 1.0f; self.actionOverlayCalloutView = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:contentView]]; //hack to copy a view... self.actionOverlayCalloutView.backgroundColor = [UIColor lightGrayColorWithAlpha:0.5]; self.actionOverlayCalloutView.layer.cornerRadius = 5; NSMutableArray *falseButtons = [NSMutableArray array]; NSMutableArray *actionButtons = [NSMutableArray array]; PointMapItem *pointAnnotation = marker.userData; if ([pointAnnotation canPerformSend]) { UIButton *button = [[UIButton alloc] init]; [button setImage:[UIImage imageNamed:@"imageButton1.png"] forState:UIControlStateNormal]; [falseButtons addObject:button]; UIButton *activableButton = [[UIButton alloc] init]; [activableButton addTarget:self action:@selector(onButton1Clicked) forControlEvents:UIControlEventTouchUpInside]; [actionButtons addObject:activableButton]; } if ([pointAnnotation canPerformShowDetails]) { UIButton *button = [[UIButton alloc] init]; [button setImage:[UIImage imageNamed:@"imageButton1.png"] forState:UIControlStateNormal]; [falseButtons addObject:button]; UIButton *activableButton = [[UIButton alloc] init]; [activableButton addTarget:self action:@selector(onButton2Clicked) forControlEvents:UIControlEventTouchUpInside]; [actionButtons addObject:activableButton]; } int buttonWidth = contentView.frame.size.width / [falseButtons count]; int currentOffset = 0; for (int i=0; i<falseButtons.count; i++) { UIButton *falseButton = [falseButtons objectAtIndex:i]; UIButton *activableButton = [actionButtons objectAtIndex:i]; [falseButton setFrame:CGRectMake(currentOffset, 0, buttonWidth, contentView.frame.size.height)]; currentOffset += buttonWidth; activableButton.frame = falseButton.frame; [activableButton setTitle:@"" forState:UIControlStateNormal]; [self.actionOverlayCalloutView addSubview:activableButton]; [contentView addSubview:falseButton]; } [calloutView addSubview:contentView]; CLLocationCoordinate2D anchor = [self.mapView.selectedMarker position]; CGPoint point = [self.mapView.projection pointForCoordinate:anchor]; point.y -= self.mapView.selectedMarker.icon.size.height + offset/2 + (infoWindowHeight - offset/2)/2; self.actionOverlayCalloutView.center = point; [self.mapView addSubview:self.actionOverlayCalloutView]; return calloutView; } - (void)mapView:(GMSMapView *)pMapView didChangeCameraPosition:(GMSCameraPosition *)position { if (pMapView.selectedMarker != nil && self.actionOverlayCalloutView.superview) { CLLocationCoordinate2D anchor = [self.mapView.selectedMarker position]; CGPoint point = [self.mapView.projection pointForCoordinate:anchor]; float offset = anchorSize * M_SQRT2; point.y -= self.mapView.selectedMarker.icon.size.height + offset/2 + (infoWindowHeight - offset/2)/2; self.actionOverlayCalloutView.center = point; } else { [self.actionOverlayCalloutView removeFromSuperview]; } } - (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate { [self.actionOverlayCalloutView removeFromSuperview]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"mapView.selectedMarker"]) { if (!self.mapView.selectedMarker) { [self.actionOverlayCalloutView removeFromSuperview]; } } } - (void)onButton2Clicked { //your code self.mapView.selectedMarker = nil; } - (void)onButton1Clicked { // your code; self.mapView.selectedMarker = nil; } 

用于CustomInfoWindowbutton处理的Swift 3.0解决scheme

https://stackoverflow.com/a/40681031/3933302

更多详情请访问这个博客

自定义和交互式Google地图(IOS SDK)infowindow

1)创build一个你想在infoWindow中显示的子视图。 2)设置子视框等于infoWindow视图的框架。

  subView = [[[NSBundle mainBundle] loadNibNamed:@"viewName" owner:self options:nil] objectAtIndex:0]; [subView setFrame:infoview.frame]; [self.mapview addSubview:subView];