Skip to content

信息窗口 ​

Callout 内置信息窗口 ​

功能说明 ​

内置 Callout 是 SDK 自带的默认气泡,显示 Marker 的 title/subtitle,并支持左右 accessory 视图(如按钮、图标)。通过 view.canShowCallout = YES 启用,点击 Marker 后自动弹出。可通过 selectAnnotation: / deselectAnnotation: 编程式控制弹出和隐藏。

关键代码 ​

objc
// ── 创建带 Callout 的 Marker ──
SFMPointAnnotation *a = [[SFMPointAnnotation alloc] init];
a.coordinate = CLLocationCoordinate2DMake(22.5429, 113.9414);
a.title = @"信息窗口标题";
a.subtitle = @"点击 Marker 即可显示信息窗口";
[self.mapView addAnnotation:a];

// ── delegate: 启用 Callout + 设置 accessory ──
- (SFMAnnotationView *)mapView:(SFMapView *)mapView
               viewForAnnotation:(id<SFMAnnotation>)annotation {
    static NSString *reuseId = @"calloutMarker";
    SFMAnnotationView *view = (SFMAnnotationView *)
        [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (!view) {
        view = [[SFMAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
    }
    view.annotation = annotation;
    view.image = markerIcon;
    view.canShowCallout = YES;  // 启用默认气泡

    // 左侧 accessory(色块/图片/nil)
    view.leftCalloutAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 10)];

    // 右侧 accessory(按钮,可点击回调)
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setImage:[UIImage systemImageNamed:@"info.circle.fill"] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 20, 20);
    [btn addTarget:self action:@selector(rightAccessoryTapped) forControlEvents:UIControlEventTouchUpInside];
    view.rightCalloutAccessoryView = btn;

    return view;
}

// ── 编程式弹出/隐藏 Callout ──
[self.mapView selectAnnotation:a animated:YES];     // 弹出 Callout
[self.mapView deselectAnnotation:a animated:YES];    // 隐藏 Callout

// ── Callout 点击回调 ──
- (void)mapView:(SFMapView *)mapView didTapCalloutForAnnotation:(id<SFMAnnotation>)annotation {
    // 点击气泡本身(非 accessory)的回调
}

// ── 刷新已弹出的 Callout(修改标题/accessory 后)──
[self.mapView deselectAnnotation:a animated:NO];
[self.mapView selectAnnotation:a animated:YES];

// ── 禁用 Callout ──
view.canShowCallout = NO;
view.leftCalloutAccessoryView = nil;
view.rightCalloutAccessoryView = nil;
if (view.selected) {
    [self.mapView deselectAnnotation:view.annotation animated:YES];
}

要点 / 注意事项 ​

  • canShowCallout = YES 后,点击 Marker 自动弹出 title/subtitle 气泡,无需手动 select。
  • leftCalloutAccessoryView / rightCalloutAccessoryView 可挂载任意 UIView,右侧常放按钮以响应用户操作。
  • 修改 annotation 的 title/subtitle 或 accessory 后,需 deselect→select 重新触发呈现以刷新气泡内容。
  • 右侧 accessory 按钮的点击事件通过 addTarget:action: 处理,不走 didTapCalloutForAnnotation: 回调。
  • didTapCalloutForAnnotation: 回调的是点击气泡主体(非 accessory)的事件。

自定义气泡(CustomCalloutView) ​

功能说明 ​

当内置 Callout 无法满足视觉需求时,可使用 SFMCustomCalloutView 承载完全自绘的 customView。气泡的外观完全由 customView 决定,SDK 只负责将其定位在 Marker 上方并随地图平移。支持 userData 绑定业务数据。

关键代码 ​

objc
// ── 自定义卡片视图(完全自绘)──
@interface DemoCustomCardView : UIView
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UIButton *actionButton;
@property (nonatomic, copy, nullable) void (^onAction)(void);
@end

@implementation DemoCustomCardView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 10;
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowOpacity = 0.25;
        self.layer.shadowOffset = CGSizeMake(0, 2);
        self.layer.shadowRadius = 6;
        // 添加 titleLabel / subtitleLabel / actionButton 子视图...
        [_actionButton addTarget:self action:@selector(actionTapped) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
- (void)actionTapped { if (self.onAction) self.onAction(); }
@end

// ── delegate: 挂载自定义气泡 ──
- (SFMAnnotationView *)mapView:(SFMapView *)mapView
               viewForAnnotation:(id<SFMAnnotation>)annotation {
    static NSString *reuseId = @"customCalloutMarker";
    SFMAnnotationView *view = (SFMAnnotationView *)
        [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (!view) {
        view = [[SFMAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
    }
    view.annotation = annotation;
    view.image = markerIcon;

    // 用 SFMCustomCalloutView 承载自绘卡片
    DemoCustomCardView *card = [[DemoCustomCardView alloc] initWithFrame:CGRectMake(0, 0, 220, 56)];
    card.titleLabel.text = annotation.title;
    card.subtitleLabel.text = annotation.subtitle;
    __weak typeof(self) weakSelf = self;
    card.onAction = ^{ [weakSelf log:@"卡片按钮被点击"]; };
    view.customCalloutView = [[SFMCustomCalloutView alloc] initWithCustomView:card];

    return view;
}

// ── 编程式弹出 ──
[self.mapView selectAnnotation:a animated:YES];

// ── 绑定/读取业务数据 ──
view.customCalloutView.userData = @{@"name": @"SFMap", @"time": @(2026)};
id data = view.customCalloutView.userData;

// ── 切换卡片样式(重建 customView)──
DemoCustomCardView *newCard = [[DemoCustomCardView alloc] initWithFrame:CGRectMake(0, 0, 220, 56)];
newCard.titleLabel.text = @"样式二";
view.customCalloutView = [[SFMCustomCalloutView alloc] initWithCustomView:newCard];
[self.mapView selectAnnotation:view.annotation animated:YES];  // 重新弹出

要点 / 注意事项 ​

  • SFMCustomCalloutView 通过 initWithCustomView: 承载任意 UIView,气泡外观完全由 customView 决定,SDK 不绘制任何气泡形状。
  • customCalloutView 优先于 canShowCallout 的内置气泡,设置后内置 Callout 不再显示。
  • 切换样式时需重新创建 customView 并赋值给 customCalloutView,然后重新 selectAnnotation:animated: 弹出。
  • userData 可绑定任意业务对象(字典/模型等),在点击回调或样式更新时取回。
  • 地图平移时气泡自动跟随 Marker 位置移动,无需额外处理。
  • customView 内的按钮等交互通过 customView 自身的 addTarget:action: 或 block 回调处理,与 SDK 无关。

V3.2-202604301400