7

Iphone アプリに iOS 用 Google マップ SDK を追加しました。クリックした情報ウィンドウがタイトルとともにポップアップする場合、いくつかのカスタム マーカーがあります。この情報ウィンドウにボタンを追加して、押すと新しいページに移動するにはどうすればよいですか? 今、私はこの投稿を使用してこの問題を解決しようとしましたネイティブ iOS/objective C 用の Google Maps SDK で InfoWindow/Marker にクリック イベントを追加してもエラーは発生しませんが、機能しません。

これが私の結果になりたいものです:http://www.flickr.com/photos/74719051@N05/6728157477/

4

4 に答える 4

8

リンクした質問に対する回答は、MapKit を使用するためのコードを示しているため、Google Maps SDK for iOS では機能しません。

Google Maps SDK for iOS でカスタム ビューを返す方法については、次の質問を参照してください。

Google Maps SDK のカスタム アノテーション ビュー

ただし、この質問によると、表示されているのは実際のビューではなく、ビューの視覚的なコピーである可能性があることに注意してください。これにより、ビューで実行できる操作が制限される可能性があります。

markerInfoWindow デリゲート メソッドによって返されるビューにボタンを追加します。

ただし、情報ウィンドウのタップを検出して別のページに移動するだけでも可能didTapWindowOfMarkerです。

これに対する直接的なサポートを追加するために、Google の問題トラッカーに機能リクエストがあることに注意してください。

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

于 2013-03-21T00:32:25.920 に答える
5

このコードはまさにあなたが望むことをします。これは、Saxon Druce が推奨するリンクの #9 にインスパイアされていますが、別の方法でより完全に行われています。カスタム infoWindow に追加されたボタンのタップを検出します。2 つの偽のボタンを使用してカスタムの infoWindow を作成し (いずれにしてもアクションをトリガーしないため、実際には画像に置き換えることができます)、infoWindow の上に 2 つの本物の透明なボタンを使用して透明な UIView を追加します。これらのボタンは、アクションをトリガーします。

最後に、infoWindow 自体が移動するときにオーバーレイされた UIView を移動するために、いくつかのデリゲート メソッドまたは KVC を使用します。

- (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;
}
于 2013-11-30T13:22:53.207 に答える
1

CustomInfoWindow でのボタン処理のためのSwift 3.0 ソリューション

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

詳細については、このブログをご覧ください

カスタムおよびインタラクティブな googlemaps (IOS SDK) 情報ウィンドウ

于 2016-11-18T15:47:50.020 に答える
0

1) infoWindow に表示するサブビューを 1 つ作成します。2) サブビューのフレームを infoWindow ビューのフレームに等しく設定します。

  subView =  [[[NSBundle mainBundle] loadNibNamed:@"viewName" owner:self options:nil] objectAtIndex:0];
  [subView setFrame:infoview.frame];
  [self.mapview addSubview:subView];
于 2015-11-24T06:22:31.150 に答える