Skip to content

定位 ​

本篇涵盖用户位置蓝点、定位管理器及蓝点样式配置。


SFMUserLocation ​

#import <SFMapSDK/SFMUserLocation.h>

SFMUserLocation 是标识用户当前位置的特殊标注。不直接创建实例,从 SFMapView.userLocation 获取。实现 SFMAnnotation 协议。

属性说明
@property(nonatomic, readonly, nullable) CLLocation *location;设备当前位置(未确定时 nil)
@property(nonatomic, readonly, getter=isUpdating) BOOL updating;是否正在更新
@property(nonatomic, readonly, nullable) CLHeading *heading;用户位置朝向
@property(nonatomic, copy) NSString *title;标题
@property(nonatomic, copy, nullable) NSString *subtitle;副标题

SFMLocationManager ​

#import <SFMapSDK/SFMLocationManager.h>

SFMLocationManager 是默认定位管理器,包装系统 CLLocationManager,实现 MapLibre 的 MLNLocationManager 协议。业务可实例化后通过 SFMapView.locationManager 注入做自定义定位源。

属性说明
@property(nonatomic, weak, nullable) id<MLNLocationManagerDelegate> delegate;代理(由 MLNMapView 注入时设置)

SFMUserLocationRepresentation ​

#import <SFMapSDK/SFMUserLocationRepresentation.h>

用户位置显示样式控制(对齐高德 MAUserLocationRepresentation)。通过 SFMapView 的 updateUserLocationRepresentation: 设置。

属性说明
@property(nonatomic, assign) BOOL showsAccuracyRing;精度圈是否显示,默认 YES
@property(nonatomic, strong) UIColor *fillColor;精度圈填充色,默认 (136/255, 166/255, 227/255, 0.3)
@property(nonatomic, strong) UIColor *strokeColor;精度圈边线色,默认同 fillColor
@property(nonatomic, assign) CGFloat lineWidth;精度圈边线宽,默认 0
@property(nonatomic, strong) UIColor *locationDotBgColor;定位点背景色,默认白色
@property(nonatomic, strong) UIColor *locationDotFillColor;定位点蓝色圆点填充色,默认蓝色
@property(nonatomic, assign) BOOL enablePulseAnnimation;内部蓝色圆点是否使用律动效果,默认 YES
@property(nonatomic, strong, nullable) UIImage *image;定位图标(与蓝色圆点互斥,nil 用蓝点)

函数 ​

objc
FOUNDATION_EXPORT UIColor *SFMUserLocationAccuracyCircleDefaultColor(void);

返回精度圈默认色。


SFMUserLocationAnnotationView ​

#import <SFMapSDK/SFMUserLocationAnnotationView.h>

用户位置蓝点视图(对齐高德经典蓝点)。继承自 MLNUserLocationAnnotationView,重写 update 用 CAShapeLayer 自绘精度圈 + 律动 + 蓝点 + 方向箭头 + 自定义图标。

属性说明
@property(nonatomic, strong, nullable) SFMUserLocationRepresentation *representation;样式,set 后立即刷新外观

SFMapView 定位相关 API ​

以下属性和方法定义在 SFMapView 上:

属性/方法说明
@property(nonatomic, assign) BOOL showsUserLocation;是否显示用户位置
@property(nonatomic, readonly, nullable) SFMUserLocation *userLocation;用户位置对象
@property(nonatomic) SFMUserTrackingMode userTrackingMode;定位追踪模式
- (void)setUserTrackingMode:(SFMUserTrackingMode)mode animated:(BOOL)animated;动画设置追踪模式
@property(nonatomic, readonly, getter=isUserLocationVisible) BOOL userLocationVisible;用户位置是否在可见区域内
- (void)updateUserLocationRepresentation:(SFMUserLocationRepresentation *)representation;设定定位蓝点样式
@property(nonatomic, nullable) SFMLocationManager *locationManager;自定义定位管理器(需在 showsUserLocation=YES 之前设置)
@property(nonatomic, assign) BOOL displayHeadingCalibration;是否显示方向校准提示,默认 YES

追踪模式枚举 ​

常量值说明
SFMUserTrackingModeNone0不追踪
SFMUserTrackingModeFollow1追踪位置

follow 模式下,手动设中心/可见区域/滑动手势会自动取消 follow。

定位相关 delegate 方法 ​

方法说明
- (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