简体   繁体   English

如何在ios上添加一个按钮到谷歌地图标记信息窗口?

[英]How can i add a button to google maps marker info window on ios?

I have added Google maps sdk for ios to my Iphone app, and i have some custom markers if clicked info window pops up with title, how can i add a button to this info window so if pressed will go to new page? 我已将ios的Google地图sdk添加到我的Iphone应用程序中,如果点击的信息窗口弹出标题,我有一些自定义标记,如何在此信息窗口中添加按钮,如果按下将转到新页面? Now i have tryed to use this post to solve this issue Adding Click Event on InfoWindow/Marker in Google Maps SDK for native iOS/objective C it doesnt give me error but it won't work. 现在我已经尝试使用这篇文章来解决这个问题在Google Maps SDK中为InfoWindow / Marker添加点击事件以获取原生iOS /目标C它不会给我错误但它不起作用。

this is what i want my result to be: http://www.flickr.com/photos/74719051@N05/6728157477/ 这就是我想要的结果: http//www.flickr.com/photos/74719051@N05/6728157477/

The answer for the question you linked shows code for using MapKit, so it wouldn't work with the Google Maps SDK for iOS. 您链接的问题的答案显示了使用MapKit的代码,因此它不适用于iOS版Google Maps SDK。

See this question for how to return a custom view with the Google Maps SDK for iOS: 有关如何使用Google Maps SDK for iOS返回自定义视图,请参阅此问题:

Custom annotation view in Google Maps SDK Google Maps SDK中的自定义注释视图

However note that according to this question, it looks like what is displayed might be a visual copy of the view not the actual view, which might limit the interaction you can do with the view: 但请注意,根据此问题,显示的内容可能是视图的可视副本而非实际视图,这可能会限制您可以对视图进行的交互:

Add buttons to view returned by markerInfoWindow delegate method 添加按钮到markerInfoWindow委托方法返回的视图

Simply detecting a tap on the info window and going to a different page should be possible using didTapWindowOfMarker though. 使用didTapWindowOfMarker可以简单地检测信息窗口上的点击并转到不同的页面。

Note that there's a feature request in Google's issue tracker to add direct support for this: 请注意,Google问题跟踪器中有一项功能请求,可为此添加直接支持:

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

This code does exactly what you want. 此代码完全符合您的要求。 It's inspired by #9 in the link recommended by Saxon Druce but done differently and more complete. 它的灵感来自Saxon Druce推荐的链接 #9,但做得不同,更完整。 It detects tap on buttons added to my custom infoWindow. 它会检测添加到我的自定义infoWindow的按钮。 I create a custom infoWindow with 2 fake buttons (they could actually be replaced by images because they won't trigger any action anyway) and I add a transparent UIView with 2 real but transparent buttons over the infoWindow. 我创建了一个带有2个假按钮的自定义infoWindow(它们实际上可以被图像替换,因为它们不会触发任何动作)并且我在infoWindow上添加了一个带有2个真实但透明按钮的透明UIView。 These buttons will trigger the actions. 这些按钮将触发操作。

Finally, I use a few delegate methods or KVC in order to move the overlaid UIView when the infoWindow itself moves. 最后,我使用一些委托方法或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;
}

Swift 3.0 solution for button handling on CustomInfoWindow 用于CustomInfoWindow上按钮处理的Swift 3.0解决方案

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

More more detail visit this blog 更多细节请访问此博客

Custom and interactive googlemaps(IOS SDK) infowindow infowindow自定义和交互式googlemaps(IOS SDK)

1)Create one subview which you want to show in infoWindow. 1)创建一个要在infoWindow中显示的子视图。 2)Set frame of subview equals to frame of infoWindow view. 2)设置子视图的帧等于infoWindow视图的帧。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不点击标记的情况下在 iOS 谷歌地图中显示信息窗口? - How to show a Info window in iOS Google maps without tapping on Marker? Google Maps在标记信息窗口iOS中显示语音气泡 - Google Maps Showing speechbubble in marker info window iOS Google Maps SDK iOS - 带UIButton的自定义标记信息窗口 - Google Maps SDK iOS - Custom marker info window with UIButton 以编程方式谷歌地图iOS的标记的关闭信息窗口 - Close info window of a marker programmatically google maps iOS 如何在iOS谷歌地图中显示所有信息窗口而无需点击标记? - How to show all Info window in iOS Google maps without tapping on Marker? 如何使用AFnetworking图像缓存在iOS Google Maps中刷新标记信息窗口? - How to refresh marker info window in iOS Google maps with AFnetworking Image cache? 按下按钮时如何添加Google Maps标记? - How do I add a google maps marker when Pressing a button? 如何在 Google Maps ios 的中心修复标记和 GMSCircle? - How can I fix marker and GMSCircle on the center in Google Maps ios? 谷歌地图ios - 如何始终显示标记信息窗口? - Google map ios - how to keep the marker info window always displayed? 在Google地图上加载标记的自定义信息窗口时,iOS应用程序冻结 - iOS app freezes when loading custom info window for marker on Google Maps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM