5

我已经将Google maps sdk for ios添加到我的iphone应用程序中,并且如果点击信息窗口弹出标题,我有一些自定义标记,我如何在此信息窗口中添加一个按钮,以便按下后会转到新页面?现在我试着用这个帖子来解决这个问题Adding Click Event on InfoWindow/Marker in Google Maps SDK for native iOS/objective C它不给我错误,但它不会工作。如何在ios上添加Google地图标记信息窗口的按钮?

这是我希望我的结果是:http://www.flickr.com/photos/[email protected]/6728157477/

回答

8

为您链接显示的代码使用MapKit的问题的答案,因此它不会与谷歌地图SDK iOS版工作。

请参阅如何返回与谷歌地图SDK自定义视图为iOS这个问题:

Custom annotation view in Google Maps SDK

但是请注意,根据这个问题,它看起来像所显示的内容可能是一个视觉副本视图不是实际的观点,这可能会限制你的观点做互动:

Add buttons to view returned by markerInfoWindow delegate method

简单地检测信息窗口的水龙头,去一个不同尽管如此,页面应该可以使用didTapWindowOfMarker

注意,有一个在谷歌的问题跟踪功能要求增加对这种直接支持:

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

+0

这是我用什么: - (空)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(id )marker [0126]自我执行SegueWithIdentifier:@“ViewController”sender:[marker snippet]]; } 它不是一个很好的解决方案来替换按钮,但它的工作原理!谢谢 – user1810991 2013-03-21 16:31:51

+0

User1810991,我试着用你的** performSegueWithIdentifier:**解决方案,但我得到一个错误。标识符(第一个参数)到底需要对应什么?我通过包含视图控制器进行匹配,但没有工作 – 2013-12-20 21:29:27

+0

嗨查理,它是segue的名称 - 单击segue(在故事板上的连接线),然后设置它的标识符。 – 2013-12-20 23:49:50

4

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

最后,我使用几个委托方法或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; 
} 
+0

可以请你介绍一下PointMapItem。无法从您的示例代码中找出它。 – Aneesh 2016-10-13 05:53:56

0

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

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