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

我目前正在创build和返回自定义视图与谷歌地图iOS SDK通过设置委托给自己并使用下面的代码。

#pragma mark - GMSMapViewDelegate -(UIView*)mapView:(GMSMapView *)mapView markerInfoWindow:(id<GMSMarker>)marker { int popupWidth = 200; int contentWidth = 180; int contentPad = 10; int popupHeight = 140; int popupBottomPadding = 16; int popupContentHeight = popupHeight - popupBottomPadding; int buttonHeight = 30; UIView *outerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, popupWidth, popupHeight)]; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, popupWidth, popupContentHeight)]; [view setBackgroundColor:[UIColor whiteColor]]; UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentPad, 0, contentWidth, 22)]; [titleLabel setFont:[UIFont systemFontOfSize:17.0]]; titleLabel.text = [marker title]; UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentPad, 24, contentWidth, 20)]; [descriptionLabel setFont:[UIFont systemFontOfSize:11.0]]; descriptionLabel.text = [marker snippet]; [view addSubview:titleLabel]; [view addSubview:descriptionLabel]; UIButton *directionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; directionButton.frame = CGRectMake(contentPad, 45, contentWidth, buttonHeight); [directionButton setTitle:@"Directions" forState:UIControlStateNormal]; [directionButton addTarget:self action:@selector(directionsPressed) forControlEvents:UIControlEventTouchDown]; UIButton *viewLocationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [viewLocationButton addTarget:self action:@selector(viewLocationPressed) forControlEvents:UIControlEventTouchUpInside]; [viewLocationButton setTitle:@"View Location" forState:UIControlStateNormal]; viewLocationButton.frame = CGRectMake(contentPad, 80, contentWidth, buttonHeight); // handle bottom dealio UIImage *bottomImage = [UIImage imageNamed:@"map-pointer-bottom"]; UIImageView *bottomView = [[UIImageView alloc] initWithFrame:CGRectMake((popupWidth / 2) - (bottomImage.size.width / 2), (popupContentHeight), bottomImage.size.width, bottomImage.size.height)]; [bottomView setImage:bottomImage]; [outerView addSubview:view]; [outerView addSubview:bottomView]; [outerView addSubview:directionButton]; [outerView addSubview:viewLocationButton]; ListItem *li = (ListItem*)[marker userData]; self.currentItem = li; NSLog(@"List Item %@ - %@", li.type, li.typeid); return outerView; } -(void)directionsPressed { NSLog(@"Directions Pressed"); } -(void)viewLocationPressed { NSLog(@"Location View Pressed"); } - (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(id<GMSMarker>)marker { NSLog(@"Tap Captured"); } 

当我点击自定义视图时,didTapWindowOfMarker被触发,但是没有button的目标方法被触发。

任何想法为什么这可能是?

也许,正如谷歌地图Android API文档中提到的那样,以下有关infowindows的限制也适用于Google Maps iOS SDK:

信息窗口不是实时视图,而是视图呈现为地图上的图像。 因此,您在视图上设置的任何监听器都将被忽略,并且无法区分视图各个部分的点击事件。 build议您不要在您的自定义信息窗口中放置交互式组件(如button,checkbox或文本input)。

所以基本上点击infowindow的任何部分只会触发“ didTapWindowOfMarker

Swift 3.0解决scheme

  //empty the default infowindow func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? { return UIView() } // reset custom infowindow whenever marker is tapped func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool { customInfoView.removeFromSuperview() // customInfoView.button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside) self.view.addSubview(customInfoView) // Remember to return false // so marker event is still handled by delegate return false } // let the custom infowindow follows the camera func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) { if (locationMarker != nil){ let location = locationMarker.position customInfoView.center = mapView.projection.point(for: location) } } // take care of the close event func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) { customInfoView.removeFromSuperview() } 

我已经添加customInfoWindow像这样

并将该视图的出口(customInfoWindow)放在具有mapView的同一控制器中。

我从这个链接中得到了这个想法,感谢这个开发者自定义和互动的googlemaps(IOS SDK)infowindow

我有一个UView,我添加一个委托给视图,以便UIButton调用select器。 对于谷歌地图,我打电话时什么也不做

 - (void) mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker; 

但是,我将我的infowindow(我的自定义视图)委托设置为self,并在按下链接到select器的button时调用我的所有操作。

更新:

这里是我使用的代码,以检测添加到我的infoWindows的button点击。 我用2个假button创build一个自定义的infoWindow(它们实际上可以被图像取代,因为它们不会触发任何操作),并且我在infoWindow上添加了一个完全透明的覆盖图,其上有两个真正的button。 这些button将触发这些操作。

我使用了几个委托方法或KVC来移动infoWindow自身被移动时的覆盖。

 - (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; } 

前职位:

看看这个主题,特别是#9,应该是有帮助的https://code.google.com/p/gmaps-api-issues/issues/detail?id=4961

1)创build一个你想在infoWindow中显示的子视图。

2)设置子视框等于infoWindow视图的框架。

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