Appearance
Annotation 与信息窗口
本篇涵盖标注协议、标注视图、点标注、引脚标注视图、弹跳标注视图及自定义气泡。
SFMAnnotation 协议
#import <SFMapSDK/SFMAnnotation.h>
SFMAnnotation 协议继承自 MapLibre 的 MLNAnnotation 协议,用于向地图视图提供标注相关信息。id<SFMAnnotation> 天然符合 id<MLNAnnotation>,无需强转。
| 属性/方法 | 说明 |
|---|---|
@property(nonatomic, readonly) CLLocationCoordinate2D coordinate; | 标注中心点(必填,只读) |
@property(nonatomic, readonly, copy, nullable) NSString *title; | 标注标题(可选) |
@property(nonatomic, readonly, copy, nullable) NSString *subtitle; | 标注副标题(可选) |
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate; | 设置坐标(可选,可拖拽标注实现) |
SFMPointAnnotation
#import <SFMapSDK/SFMPointAnnotation.h>
SFMPointAnnotation 是基本的点标注实现,同时实现 SFMAnnotation 和 MLNAnnotation。
| 属性 | 说明 |
|---|---|
@property(nonatomic, assign) CLLocationCoordinate2D coordinate; | 点标注地理坐标 |
@property(nonatomic, copy, nullable) NSString *title; | 标题 |
@property(nonatomic, copy, nullable) NSString *subtitle; | 副标题 |
SFMAnnotationView
#import <SFMapSDK/SFMAnnotationView.h>
SFMAnnotationView 是地图标注视图,继承自 MapLibre 的 MLNAnnotationView。复用其注解视图定位、复用、拖拽、选中能力,并扩展了图片、气泡、zIndex 等 SFM 专有属性。
枚举:SFMAnnotationViewDragState
| 常量 | 值 | 说明 |
|---|---|---|
SFMAnnotationViewDragStateNone | 0 | 无状态 |
SFMAnnotationViewDragStateStarting | 1 | 开始拖拽 |
SFMAnnotationViewDragStateDragging | 2 | 拖拽中 |
SFMAnnotationViewDragStateCanceling | 3 | 取消拖拽 |
SFMAnnotationViewDragStateEnding | 4 | 结束拖拽 |
属性
| 属性 | 说明 |
|---|---|
@property(nonatomic, strong, nullable) UIImage *image; | 显示图片,设置后立即更新内部 UIImageView |
@property(nonatomic, assign) BOOL canShowCallout; | 是否允许弹出默认气泡(默认 NO) |
@property(nonatomic, strong, nullable) UIView *leftCalloutAccessoryView; | 默认气泡左侧视图 |
@property(nonatomic, strong, nullable) UIView *rightCalloutAccessoryView; | 默认气泡右侧视图 |
@property(nonatomic, strong, nullable) SFMCustomCalloutView *customCalloutView; | 自定义气泡视图(挂载后优先于默认气泡) |
@property(nonatomic, assign) NSInteger zIndex; | 层级索引(值越大越上层,默认 0) |
@property(nonatomic, assign) BOOL centersAfterSelect; | 选中后是否居中显示 |
@property(nonatomic, assign) CGVector centerOffset; | 中心相对坐标点的像素偏移(正 x 朝右、正 y 朝下) |
@property(nonatomic, assign) CGPoint calloutOffset; | 气泡相对锚点的额外偏移(仅对 customCalloutView 生效) |
@property(nonatomic, weak, nullable) SFMapView *sfm_mapView; | 当前所属地图视图(弱引用,选中时注入) |
方法
| 方法 | 说明 |
|---|---|
- (void)commonInit; | 一次性初始化钩子(子类重写做铺层/默认值设置,NS_REQUIRES_SUPER) |
+ (CGVector)centerOffsetForAnchor:(CGPoint)anchor imageSize:(CGSize)size; | 归一化锚点 [0,1] 换算为 centerOffset |
centerOffset 机制:MapLibre 布局时把标注视图父容器中心钉在地理坐标的屏幕点,再叠加 centerOffset 偏移。centerOffset 仅在构建/复用、地图布局刷新阶段被读取一次,无自动重绘——赋值须在
viewForAnnotation:返回前执行,且每次出队复用都要重新赋值。
继承属性:
reuseIdentifier、annotation、dragState、enabled、selected、draggable、prepareForReuse、setSelected:animated:等均直接继承自MLNAnnotationView。
SFMPinAnnotationView
#import <SFMapSDK/SFMPinAnnotationView.h>
SFMPinAnnotationView 是以引脚图标显示标注的视图。继承自 SFMAnnotationView。
| 属性 | 说明 |
|---|---|
@property(nonatomic, strong) UIColor *pinColor; | 引脚颜色 |
@property(nonatomic, assign) BOOL animatesDrop; | 添加到地图时是否执行掉落动画 |
SFMBounceAnnotationView
#import <SFMapSDK/SFMBounceAnnotationView.h>
SFMBounceAnnotationView 是带弹跳动画效果的标注视图。继承自 SFMAnnotationView。
| 属性 | 说明 |
|---|---|
@property(nonatomic, copy, nullable) NSString *identifier; | 弹跳标注唯一标识符 |
SFMCustomCalloutView
#import <SFMapSDK/SFMCustomCalloutView.h>
SFMCustomCalloutView 是纯透传气泡容器,用于在标注被选中时显示业务侧完全自绘的视图。实现 MapLibre 的 MLNCalloutView 协议。只承载 init 时传入的 customView,不画气泡背景、不带指向箭头。
| 属性/方法 | 说明 |
|---|---|
@property(nonatomic, strong, readonly) UIView *customView; | init 时传入的自定义视图(只读) |
@property(nonatomic, strong, nullable) id userData; | 用户自定义数据 |
@property(nonatomic, assign) CGPoint calloutOffset; | 气泡偏移(正 x 朝右、正 y 朝下,默认 (0,0)) |
- (nullable instancetype)initWithCustomView:(UIView *)customView; | 初始化(customView 不能为 nil,NS_DESIGNATED_INITIALIZER) |
- (void)hideCallout; | 隐藏气泡(兼容旧 API) |
- (void)sfm_updateFrameToRect:(CGRect)rect constrainedToRect:(CGRect)constrainedRect; | region 变化时重新定位气泡 |
+new、-init、-initWithFrame:、-initWithCoder:均不可用(NS_UNAVAILABLE)。
气泡挂载方式:通过
SFMAnnotationView.customCalloutView属性挂载。挂载后优先于默认气泡(canShowCallout/accessory 不再生效)。
标注视图复用
标注视图采用类似 UITableView 的 cell 复用机制,通过重用标识符从回收池获取可复用视图。
objc
// delegate 中实现
- (SFMAnnotationView *)mapView:(SFMapView *)mapView viewForAnnotation:(id<SFMAnnotation>)annotation {
static NSString *reuseId = @"marker";
SFMAnnotationView *view = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (!view) {
view = [[SFMAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
}
view.image = [UIImage imageNamed:@"marker_icon"];
view.canShowCallout = YES;
return view;
}SFMViewRecycler
#import <SFMapSDK/SFMViewRecycler.h>
标注视图回收复用管理器(内部类)。
| 属性/方法 | 说明 |
|---|---|
@property(nonatomic, readonly) NSMutableDictionary<NSString *, NSMutableArray<SFMAnnotationView *> *> *reuseIdentifiersToRecycledViews; | 重用标识符到回收视图的映射 |
- (nullable SFMAnnotationView *)dequeueReusableAnnotationViewWithIdentifier:(NSString *)identifier; | 从回收池取出视图 |
- (void)recycle:(SFMAnnotationView *)annotationView; | 回收视图到回收池 |
- (void)removeAll; | 移除所有回收视图 |
- (void)removeAnnotationView:(SFMAnnotationView *)annotationView; | 移除指定回收视图 |
