Skip to content

SFMapView ​

#import <SFMapSDK/SFMapView.h>

SFMapView 是核心地图视图类(UIView 子类),负责在屏幕上显示地图内容,提供用户交互(平移、缩放、旋转),并以标注和覆盖物的形式呈现信息。

内部封装了 MapLibre 的 MLNMapView,对外提供 SFMapSDK 的统一 API。

delegate 模式:所有事件回调通过 SFMapViewDelegate 协议实现。设置 mapView.delegate = self 后实现对应的 @optional 方法即可接收回调。


枚举 ​

SFMapType ​

常量值说明
SFMapTypeStandard0标准地图(默认,可用)
SFMapTypeSatellite1卫星地图(暂未提供,回落为标准地图)
SFMapTypeHybrid2混合地图(暂未提供,回落为标准地图)
SFMapTypeSimple3简洁地图(暂未提供,回落为标准地图)
SFMapTypeNight4夜间模式地图(可用)
SFMapTypeCustom5自定义地图(暂未提供,回落为标准地图)

当前样式服务端仅提供标准(allvector_day)与夜间(allvector_night)两套样式,除 SFMapTypeNight 外其余类型均回落为标准地图;自定义样式请改用 styleURL 属性指定。

SFMUserTrackingMode ​

常量值说明
SFMUserTrackingModeNone0不追踪用户位置更新
SFMUserTrackingModeFollow1追踪用户位置更新

SFMOverlayLevel ​

常量值说明
SFMOverlayLevelRoads0道路级别(道路标签下方)
SFMOverlayLevelBuildings1建筑物级别(建筑物和道路标签上方)
SFMOverlayLevelWater2水体级别(所有标签上方)
SFMOverlayLevelAboveLabels3最顶层

SFMOrnamentPosition ​

常量值说明
SFMOrnamentPositionTopLeft0左上角
SFMOrnamentPositionTopRight1右上角
SFMOrnamentPositionBottomLeft2左下角
SFMOrnamentPositionBottomRight3右下角

SFMTMCColorType ​

常量值说明
SFMTMCColorTypeNone0无颜色
SFMTMCColorTypeSmooth1畅通(绿色)
SFMTMCColorTypeSlow2缓行(黄色)
SFMTMCColorTypeCongested3拥堵(红色)
SFMTMCColorTypeVeryCongested4严重拥堵(深红色)
SFMTMCColorTypeUnknown5未知状态

属性 ​

基本属性 ​

属性说明
@property(nonatomic, weak, nullable) id<SFMMapViewDelegate> delegate;地图视图的委托对象
@property(nonatomic, assign) SFMapType mapType;地图显示类型
@property(nonatomic, strong, nullable) NSURL *styleURL;地图样式 URL
@property(nonatomic, readonly, nullable) MLNStyle *style;当前地图样式(样式加载完成后有效)

相机属性 ​

属性说明
@property(nonatomic) CLLocationCoordinate2D centerCoordinate;地图中心点地理坐标
@property(nonatomic) double zoomLevel;地图缩放级别
@property(nonatomic) double minimumZoomLevel;最小缩放级别
@property(nonatomic) double maximumZoomLevel;最大缩放级别
@property(nonatomic) CLLocationDirection direction;地图方向(0 表示正北,单位度)
@property(nonatomic) CGFloat pitch;地图相机倾斜角(0 垂直俯视,范围 0~60)
@property(nonatomic) SFMCoordinateBounds visibleCoordinateBounds;地图可见的地理边界
@property(nonatomic) SFMCoordinateRegion region;地图当前显示的地理区域
@property(nonatomic, readonly) SFMCameraPosition *cameraPosition;当前相机位置(中心+缩放+朝向+倾斜)

用户交互属性 ​

属性说明
@property(nonatomic, assign) BOOL zoomEnabled;是否允许缩放手势
@property(nonatomic, assign) BOOL scrollEnabled;是否允许滚动手势
@property(nonatomic, assign) BOOL rotateEnabled;是否允许旋转手势
@property(nonatomic, assign) BOOL pitchEnabled;是否允许倾斜手势

UI 控件属性 ​

属性说明
@property(nonatomic, assign) BOOL showsScale;是否显示比例尺
@property(nonatomic, assign) BOOL showsCompass;是否显示指南针
@property(nonatomic, assign) SFMOrnamentPosition compassViewPosition;指南针锚定角(默认右上角)
@property(nonatomic, assign) CGPoint compassViewMargins;指南针偏移(默认 (8, 8))
@property(nonatomic, assign) SFMOrnamentPosition scaleBarPosition;比例尺锚定角(默认左上角)
@property(nonatomic, assign) CGPoint scaleBarMargins;比例尺偏移(默认 (8, 8))
@property(nonatomic, assign) UIEdgeInsets mapContentInset;地图内容内边距
@property(nonatomic, assign) BOOL displayHeadingCalibration;是否显示方向校准提示(默认 YES)

定位属性 ​

属性说明
@property(nonatomic, assign) BOOL showsUserLocation;是否显示用户位置
@property(nonatomic, readonly, nullable) SFMUserLocation *userLocation;用户位置对象
@property(nonatomic) SFMUserTrackingMode userTrackingMode;定位追踪模式(follow 模式下手动操作会取消 follow)
@property(nonatomic, readonly, getter=isUserLocationVisible) BOOL userLocationVisible;用户位置是否在可见区域内
@property(nonatomic, nullable) SFMLocationManager *locationManager;自定义定位管理器(需在 showsUserLocation=YES 之前设置)

标注与覆盖物属性 ​

属性说明
@property(nonatomic, readonly, nullable) NSArray<id<SFMAnnotation>> *annotations;地图上所有标注(只读)
@property(nonatomic, readonly, nonnull) NSArray<id<SFMOverlay>> *overlays;地图上所有覆盖物(只读)
@property(nonatomic, copy, nullable) NSArray<id<SFMAnnotation>> *selectedAnnotations;当前选中的标注
@property(nonatomic, strong, nullable) SFMClusterManager *clusterManager;关联的聚合管理器(nil 表示未启用聚合)

初始化方法 ​

方法说明
- (instancetype)initWithFrame:(CGRect)frame;初始化地图视图
- (instancetype)initWithFrame:(CGRect)frame styleURL:(nullable NSURL *)styleURL;使用指定样式 URL 初始化
- (instancetype)initWithFrame:(CGRect)frame styleURL:(nullable NSURL *)styleURL localIdeographFontFamily:(nullable NSString *)localIdeographFontFamily;使用指定样式 URL 和本地 CJK 字体回退策略初始化

localIdeographFontFamily:nil 使用系统字体回退;@""(空串)禁用本地回退,CJK 走远端 style 字体;非空串用该字体作本地 CJK 回退。默认便捷初始化器使用 @""。


相机操作 ​

方法说明
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate animated:(BOOL)animated;以动画方式设置中心坐标
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(double)zoomLevel animated:(BOOL)animated;以动画方式设置中心坐标和缩放级别
- (void)setZoomLevel:(double)zoomLevel animated:(BOOL)animated;以动画方式设置缩放级别
- (void)setDirection:(CLLocationDirection)direction animated:(BOOL)animated;以动画方式设置地图方向
- (void)setPitch:(CGFloat)pitch animated:(BOOL)animated;以动画方式设置倾斜角
- (void)setVisibleCoordinateBounds:(SFMCoordinateBounds)bounds animated:(BOOL)animated;以动画方式设置可见地理边界
- (void)setVisibleCoordinateBounds:(SFMCoordinateBounds)bounds edgePadding:(UIEdgeInsets)edgePadding animated:(BOOL)animated;以动画方式设置可见地理边界,指定边距
- (void)setRegion:(SFMCoordinateRegion)region animated:(BOOL)animated;以动画方式设置可见地理区域
- (void)setCamera:(SFMCameraPosition *)camera animated:(BOOL)animated;以动画方式原子设置相机位置(中心+缩放+朝向+倾斜)
- (void)setContentInset:(UIEdgeInsets)inset animated:(BOOL)animated;以动画方式设置内容内边距
- (void)setUserTrackingMode:(SFMUserTrackingMode)mode animated:(BOOL)animated;以动画方式设置追踪模式
- (void)updateUserLocationRepresentation:(SFMUserLocationRepresentation *)representation;设定定位蓝点样式

标注管理 ​

方法说明
- (void)addAnnotation:(id<SFMAnnotation>)annotation;向地图添加标注
- (void)addAnnotations:(NSArray<id<SFMAnnotation>> *)annotations;向地图添加多个标注
- (void)removeAnnotation:(id<SFMAnnotation>)annotation;从地图移除标注
- (void)removeAnnotations:(NSArray<id<SFMAnnotation>> *)annotations;从地图移除多个标注
- (void)selectAnnotation:(id<SFMAnnotation>)annotation animated:(BOOL)animated;选中指定标注
- (void)deselectAnnotation:(id<SFMAnnotation>)annotation animated:(BOOL)animated;取消选中指定标注
- (void)showAnnotations:(NSArray<id<SFMAnnotation>> *)annotations animated:(BOOL)animated;使指定标注可见
- (nullable SFMAnnotationView *)viewForAnnotation:(id<SFMAnnotation>)annotation;获取指定标注对应的视图
- (nullable __kindof SFMAnnotationView *)dequeueReusableAnnotationViewWithIdentifier:(NSString *)identifier;从回收池获取可复用标注视图

覆盖物管理 ​

方法说明
- (void)addOverlay:(id<SFMOverlay>)overlay;向地图添加覆盖物
- (void)addOverlays:(NSArray<id<SFMOverlay>> *)overlays;向地图添加多个覆盖物
- (void)removeOverlay:(id<SFMOverlay>)overlay;从地图移除覆盖物
- (void)removeOverlays:(NSArray<id<SFMOverlay>> *)overlays;从地图移除多个覆盖物
- (void)addOverlay:(id<SFMOverlay>)overlay level:(SFMOverlayLevel)level;在指定层级添加覆盖物
- (void)addOverlays:(NSArray<id<SFMOverlay>> *)overlays level:(SFMOverlayLevel)level;在指定层级添加多个覆盖物
- (nullable SFMOverlayView *)viewForOverlay:(id<SFMOverlay>)overlay;获取指定覆盖物对应的视图

自定义绘制 ​

方法说明
- (void)addCustomDrawOverlay:(SFMCustomDrawOverlay *)overlay;添加自定义绘制覆盖物
- (void)removeCustomDrawOverlay:(SFMCustomDrawOverlay *)overlay;移除自定义绘制覆盖物

Marker 管理 ​

方法说明
- (void)addMarker:(SFMMarker *)marker;添加 Marker
- (void)removeMarker:(SFMMarker *)marker;移除 Marker
- (void)removeAllMarkers;移除所有 Marker

坐标转换与工具 ​

方法说明
- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(nullable UIView *)view;屏幕坐标转地理坐标
- (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(nullable UIView *)view;地理坐标转屏幕坐标
- (CGRect)convertCoordinateBounds:(SFMCoordinateBounds)bounds toRectToView:(nullable UIView *)view;地理边界转屏幕矩形
- (SFMCoordinateBounds)convertRect:(CGRect)rect toCoordinateBoundsFromView:(nullable UIView *)view;屏幕矩形转地理边界
- (nullable NSArray<id<SFMAnnotation>> *)visibleAnnotationsInRect:(CGRect)rect;获取指定矩形内的可见标注

截图 ​

方法说明
- (void)takeSnapshotWithCompletionHandler:(void (^)(UIImage *_Nullable image, NSError *_Nullable error))completionHandler;对当前地图截图
- (void)triggerRepaint;触发地图重绘一帧(自绘覆盖物动画场景使用)

SFMapViewDelegate 协议 ​

@protocol SFMapViewDelegate <NSObject>,所有方法均为 @optional。

区域 / 相机变化 ​

方法说明
- (void)mapView:(SFMapView *)mapView regionWillChangeAnimated:(BOOL)animated;地图可见区域即将改变
- (void)mapView:(SFMapView *)mapView regionDidChangeAnimated:(BOOL)animated;地图可见区域已经改变
- (void)mapView:(SFMapView *)mapView cameraWillChangeAnimated:(BOOL)animated;地图即将开始改变中心点或缩放级别
- (void)mapView:(SFMapView *)mapView cameraDidChangeAnimated:(BOOL)animated;地图已经完成中心点或缩放级别改变

加载 ​

方法说明
- (void)mapViewWillStartLoadingMap:(SFMapView *)mapView;地图样式即将开始加载
- (void)mapViewDidFinishLoadingMap:(SFMapView *)mapView;地图样式已经完成加载
- (void)mapView:(SFMapView *)mapView didFailLoadingMapWithError:(NSError *)error;地图样式加载失败
- (void)mapViewWillStartRenderingMap:(SFMapView *)mapView;地图样式即将渲染
- (void)mapViewDidFinishRenderingMap:(SFMapView *)mapView fullyRendered:(BOOL)fullyRendered;地图样式已经完成渲染
- (void)mapViewWillStartRenderingFrame:(SFMapView *)mapView;地图帧即将渲染
- (void)mapViewDidFinishRenderingFrame:(SFMapView *)mapView fullyRendered:(BOOL)fullyRendered;地图帧已经完成渲染

标注 ​

方法说明
- (nullable SFMAnnotationView *)mapView:(SFMapView *)mapView viewForAnnotation:(id<SFMAnnotation>)annotation;为指定标注返回对应的标注视图
- (void)mapView:(SFMapView *)mapView didSelectAnnotationView:(SFMAnnotationView *)view;标注视图被选中
- (void)mapView:(SFMapView *)mapView didDeselectAnnotationView:(SFMAnnotationView *)view;标注视图被取消选中
- (void)mapView:(SFMapView *)mapView didSelectAnnotation:(id<SFMAnnotation>)annotation;标注被选中
- (void)mapView:(SFMapView *)mapView didDeselectAnnotation:(id<SFMAnnotation>)annotation;标注被取消选中
- (void)mapView:(SFMapView *)mapView annotationView:(SFMAnnotationView *)view didChangeDragState:(SFMAnnotationViewDragState)state fromOldState:(SFMAnnotationViewDragState)oldState;标注视图拖拽状态变化

覆盖物 ​

方法说明
- (nullable SFMOverlayView *)mapView:(SFMapView *)mapView viewForOverlay:(id<SFMOverlay>)overlay;为指定覆盖物返回对应的视图
- (CGFloat)mapView:(SFMapView *)mapView lineWidthForPolyline:(SFMPolyline *)polyline;为指定折线返回线宽
- (UIColor *)mapView:(SFMapView *)mapView strokeColorForShapeAnnotation:(SFMShape *)shape;为指定形状返回描边颜色
- (UIColor *)mapView:(SFMapView *)mapView fillColorForPolygonAnnotation:(SFMPolygon *)polygon;为指定多边形返回填充颜色
- (void)mapView:(SFMapView *)mapView didTapMultiPointOverlay:(SFMMultiPointOverlay *)overlay item:(SFMMultiPointItem *)item;点击海量点覆盖物中的单个点

用户交互 ​

方法说明
- (void)mapView:(SFMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate;用户点击地图
- (void)mapView:(SFMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate;用户长按地图
- (BOOL)mapView:(SFMapView *)mapView didTapAnnotation:(id<SFMAnnotation>)annotation;用户点击标注(返回 YES 消费事件)
- (void)mapView:(SFMapView *)mapView didTapCalloutForAnnotation:(id<SFMAnnotation>)annotation;用户点击标注的 Callout
- (BOOL)mapView:(SFMapView *)mapView didTapTouchPoi:(SFMTouchPoi *)poi;用户点击 POI(返回 YES 消费事件)
- (void)mapView:(SFMapView *)mapView didTapPolyline:(SFMPolyline *)polyline;用户点击折线覆盖物
- (void)mapView:(SFMapView *)mapView didTapPolygon:(SFMPolygon *)polygon;用户点击多边形覆盖物
- (void)mapView:(SFMapView *)mapView didReceiveTouch:(UITouch *)touch coordinate:(CLLocationCoordinate2D)coordinate;用户触摸地图(每次触摸均回调)

用户位置 ​

方法说明
- (void)mapView:(SFMapView *)mapView didUpdateUserLocation:(SFMUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation;用户位置更新
- (void)mapView:(SFMapView *)mapView didChangeUserTrackingMode:(SFMUserTrackingMode)mode animated:(BOOL)animated;追踪模式变化
- (void)mapView:(SFMapView *)mapView didFailToLocateUserWithError:(NSError *)error;定位失败

V3.2-202604301400