google-maps-sdk-ios - 添加按钮以查看由 markerInfoWindow 委托(delegate)方法返回的 View

标签 google-maps-sdk-ios

我目前正在通过将委托(delegate)设置为 self 并使用以下代码,使用 google maps ios SDK 创建并返回自定义 View 。

#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");
}

当我点击自定义 View 时,didTapWindowOfMarker 被触发,但按钮的目标方法都没有被触发。

为什么会这样?

最佳答案

可能,正如 Google Maps Android API 文档中正式提到的,以下关于 infowindows 的限制也适用于 Google Maps iOS SDK:

信息窗口不是实时 View ,而是将 View 呈现为 map 上的图像。因此,您在 View 上设置的任何监听器都将被忽略,并且您无法区分 View 各个部分的点击事件。建议您不要在自定义信息窗口中放置交互式组件(例如按钮、复选框或文本输入)。

所以基本上点击信息窗口的任何部分只会触发“ didTapWindowOfMarker

关于google-maps-sdk-ios - 添加按钮以查看由 markerInfoWindow 委托(delegate)方法返回的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15213049/

相关文章:

objective-c - -转换点 :toCoordinateFromView: method for Google Maps SDK for iOS

ios - 将搜索栏/按钮/任何东西放在谷歌地图 View 的顶部

ios - 带有设备方向/陀螺仪传感器的 iOS 版谷歌街景

ios - 在 iOS 中更改 MyLocationButton 在 Google map 中的位置

ios - 基于 Radius Google Maps iOS SDK 更改相机缩放

ios - 是否可以清除 Google Maps for iOS SDK 上的 map 图 block 缓存?

ios - 适用于 iOS 的 Google map API - 不显示标记

ios - 如何禁用动画以更改 iOS 中的 GMSMarker 位置?

swift - 在父类(super class)符合 Swift 协议(protocol)的子类中实现委托(delegate)方法

ios - 是否有用于平移或缩放 map 的 Google map 事件监听器?