【问题标题】:How can i add a button to google maps marker info window on ios?如何在 ios 上的谷歌地图标记信息窗口中添加按钮?
【发布时间】:2013-03-21 00:13:44
【问题描述】:

我已将 Google maps sdk for ios 添加到我的 Iphone 应用程序中,如果单击的信息窗口弹出标题,我有一些自定义标记,我如何在此信息窗口中添加一个按钮,以便如果按下将转到新页面?现在我尝试使用这篇文章来解决这个问题Adding Click Event on InfoWindow/Marker in Google Maps SDK for native iOS/objective C 它没有给我错误但它不起作用。

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

【问题讨论】:

    标签: ios objective-c button google-maps-markers google-maps-sdk-ios


    【解决方案1】:

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

    有关如何使用适用于 iOS 的 Google Maps SDK 返回自定义视图,请参阅此问题:

    Custom annotation view in Google Maps SDK

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

    Add buttons to view returned by markerInfoWindow delegate method

    不过,使用didTapWindowOfMarker 应该可以简单地检测信息窗口上的点击并转到不同的页面。

    请注意,Google 的问题跟踪器中有一个功能请求以添加对此的直接支持:

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

    【讨论】:

    • 这是我使用的:- (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(id)marker { [self performSegueWithIdentifier:@"ViewController" sender:[marker sn-p ]];替换按钮不是一个好的解决方案,但它有效!谢谢
    • User1810991,我尝试使用您的 performSegueWithIdentifier: 解决方案,但出现错误。标识符(第一个参数)究竟需要对应什么?我通过包含视图控制器使其匹配,但不起作用
    • 嗨 Charlie,这是 segue 的名称 - 单击 segue(故事板上的连接线),然后设置其标识符。
    【解决方案2】:

    此代码完全符合您的要求。它的灵感来自 Saxon Druce 推荐的link 中的#9,但做得不同且更完整。它检测到点击添加到我的自定义信息窗口的按钮。我创建了一个带有 2 个假按钮的自定义 infoWindow(它们实际上可以被图像替换,因为它们无论如何都不会触发任何操作)并且我在 infoWindow 上添加了一个带有 2 个真实但透明的按钮的透明 UIView。这些按钮将触发操作。

    最后,我使用了一些委托方法或 KVC,以便在 infoWindow 本身移动时移动覆盖的 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;
    }
    

    【讨论】:

    • 你能描述一下PointMapItem吗?无法从您的示例代码中弄清楚。
    【解决方案3】:

    Swift 3.0 解决方案用于 CustomInfoWindow 上的按钮处理

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

    更多详情请访问此博客

    Custom and interactive googlemaps(IOS SDK) infowindow

    【讨论】:

      【解决方案4】:

      1) 创建一个要在 infoWindow 中显示的子视图。 2)设置subview的frame等于infoWindow view的frame。

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

      【讨论】:

        猜你喜欢
        • 2017-07-31
        • 2017-07-26
        • 2011-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多