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]; 
相關問題