Appearance
地图基础
本篇覆盖丰图地图 iOS SDK 的核心基础能力:地图视图创建、相机控制、UI 手势/控件设置、地图类型与样式、事件监听。所有示例均从官方 Demo 提取精简而来,可直接参考集成。
官方 Demo 的
BaseMapViewController基类在viewDidLoad中完成[SFMapsInitializer setApiKey:]、创建SFMapView并设置 delegate、设置默认相机位置(深圳 zoom=12),随后调用onMapReady:通知子类。子类只需重写titleText和onMapReady:即可开始业务配置。基类还提供坐标常量(coordinateForCity:)、操作按钮(addButton:action:)、日志输出(log:)等辅助方法。
地图视图与生命周期
功能说明
SFMapView 是地图的容器视图,负责管理底层 MapLibre 引擎的创建与渲染。使用前必须先调用 [SFMapsInitializer setApiKey:] 设置开发者 Key,否则地图无法加载。SFMapView 的创建方式为 initWithFrame:,加入视图层级后即可使用。iOS 平台由系统自动管理视图生命周期,无需像 Android 那样手动转发 onResume/onPause 等方法。
关键代码
objc
// 1. 创建 SFMapView 前先设置 ApiKey(建议放在 AppDelegate 或基类)
[SFMapsInitializer setApiKey:@"你的ApiKey"];
// 2. 创建 SFMapView 并加入布局
SFMapView *mapView = [[SFMapView alloc] initWithFrame:self.view.bounds];
mapView.delegate = self;
mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:mapView];
// 3. 设置初始中心点和缩放
CLLocationCoordinate2D shenzhen = CLLocationCoordinate2DMake(22.5429, 113.9414);
[mapView setCenterCoordinate:shenzhen zoomLevel:12.0 animated:NO];
// 4. 地图类型切换
mapView.mapType = SFMapTypeStandard; // 普通地图(默认)
mapView.mapType = SFMapTypeNight; // 黑夜地图
// 5. 缩放操作
mapView.zoomLevel += 1.0; // 放大一级
mapView.zoomLevel -= 1.0; // 缩小一级
// 6. 移动中心点(带动画)
CLLocationCoordinate2D shanghai = CLLocationCoordinate2DMake(31.2304, 121.4737);
[mapView setCenterCoordinate:shanghai animated:YES];常用 API
objc
// 地图类型
mapView.mapType = SFMapTypeStandard; // SFMapTypeNight 为黑夜模式
// 缩放范围
mapView.minimumZoomLevel = 5.0;
mapView.maximumZoomLevel = 15.0;
// 当前相机信息
CLLocationCoordinate2D center = mapView.centerCoordinate;
double zoom = mapView.zoomLevel;
CLLocationDirection direction = mapView.direction; // 朝向(度)
CGFloat pitch = mapView.pitch; // 倾斜角(度)
// 可视区域
SFMCoordinateBounds bounds = mapView.visibleCoordinateBounds;
// bounds.sw / bounds.ne
// 坐标与屏幕像素互转
CGPoint screenPoint = [mapView convertCoordinate:coordinate toPointToView:nil];
CLLocationCoordinate2D coord = [mapView convertPoint:screenPoint toCoordinateFromView:nil];要点
setApiKey:必须在SFMapView创建之前调用,建议放在AppDelegate的application:didFinishLaunchingWithOptions:中。setCenterCoordinate:zoomLevel:animated:同时设置中心点和缩放级别,animated:NO为瞬时跳转,animated:YES为平滑动画。- iOS 无需手动转发生命周期方法,
SFMapView随 UIView 自动管理。 mapType切换为SFMapTypeNight可启用深色主题底图。
地图类型与样式
当前仅标准地图与夜间模式可用(样式服务端只提供 allvector_day / allvector_night 两套样式):
objc
typedef NS_ENUM(NSUInteger, SFMapType) {
SFMapTypeStandard = 0, // 标准地图(默认,可用)
SFMapTypeSatellite, // 卫星地图(暂未提供,回落为标准地图)
SFMapTypeHybrid, // 混合地图(暂未提供,回落为标准地图)
SFMapTypeSimple, // 简洁地图(暂未提供,回落为标准地图)
SFMapTypeNight, // 夜间模式(可用)
SFMapTypeCustom, // 自定义地图(暂未提供,回落为标准地图)
};
self.mapView.mapType = SFMapTypeNight;除
SFMapTypeNight外,其余类型在 SDK 内部均回落为标准样式(allvector_day)。
需要自定义样式时,直接指定样式 URL(不受 mapType 限制):
objc
self.mapView.styleURL = [NSURL URLWithString:@"https://gis-map-apis.sf-express.com/MapVTService/vmapx/mapstyle/v2/3857/allvector_night"];
// 或使用 SDK 内置常量
self.mapView.styleURL = [NSURL URLWithString:[SFMapsInitializer NIGHT_STYLE_URL]];相机控制与投影
功能说明
地图视角由相机参数决定:中心点(centerCoordinate)、缩放(zoomLevel)、朝向(direction)、倾斜(pitch)。SDK 通过 setCenterCoordinate:zoomLevel:animated:、setDirection:animated:、setPitch:animated: 等方法改变视角,支持瞬时和动画两种模式。投影接口提供屏幕坐标与地理坐标互转,以及可视区域查询。
关键代码
objc
// ── 移动(瞬时)──
CLLocationCoordinate2D beijing = CLLocationCoordinate2DMake(39.9042, 116.4074);
[self.mapView setCenterCoordinate:beijing zoomLevel:10.0 animated:NO];
// ── 动画移动 ──
[self.mapView setCenterCoordinate:beijing zoomLevel:10.0 animated:YES];
// ── 缩放 ──
[self.mapView setZoomLevel:8.0 animated:YES]; // 缩放到指定级别
[self.mapView setZoomLevel:self.mapView.zoomLevel + 1 animated:YES]; // 放大一级
[self.mapView setZoomLevel:self.mapView.zoomLevel - 1 animated:YES]; // 缩小一级
// ── 旋转(bearing)──
[self.mapView setDirection:45 animated:YES]; // 顺时针旋转 45°
[self.mapView setDirection:0 animated:YES]; // 重置为正北
// ── 倾斜(pitch)──
[self.mapView setPitch:30 animated:YES]; // 倾斜 30°
[self.mapView setPitch:0 animated:YES]; // 重置为俯视
// ── 适配多个点到可视范围 ──
CLLocationCoordinate2D sw = CLLocationCoordinate2DMake(minLat, minLng);
CLLocationCoordinate2D ne = CLLocationCoordinate2DMake(maxLat, maxLng);
SFMCoordinateBounds bounds = SFMCoordinateBoundsMake(sw, ne);
[self.mapView setVisibleCoordinateBounds:bounds
edgePadding:UIEdgeInsetsMake(100, 100, 100, 100)
animated:YES];
// edgePadding 为四周像素边距,确保所有点不被边缘裁切
// ── 缩放范围设置 ──
self.mapView.minimumZoomLevel = 5.0;
self.mapView.maximumZoomLevel = 15.0;
// ── 投影:坐标互转 ──
CLLocationCoordinate2D shenzhen = CLLocationCoordinate2DMake(22.5429, 113.9414);
CGPoint screen = [self.mapView convertCoordinate:shenzhen toPointToView:nil];
// toPointToView:nil 表示相对 mapView 自身
CGPoint center = CGPointMake(self.mapView.bounds.size.width / 2.0,
self.mapView.bounds.size.height / 2.0);
CLLocationCoordinate2D coord = [self.mapView convertPoint:center toCoordinateFromView:nil];
// ── 投影:可视区域 ──
SFMCoordinateBounds visible = self.mapView.visibleCoordinateBounds;
// visible.sw(西南角)/ visible.ne(东北角)要点
setCenterCoordinate:zoomLevel:animated:是最常用的相机方法,animated:NO适合初始化,animated:YES适合用户交互后的过渡。direction单位为度,0 表示正北方向,顺时针递增;pitch为相机俯仰角,0 为正俯视。setVisibleCoordinateBounds:edgePadding:animated:的edgePadding是四周等距像素边距,用于确保所有点不被边缘裁切。- 坐标互转
convertCoordinate:toPointToView:传nil表示相对 mapView 自身坐标系。 - iOS SDK 暂未暴露动画完成回调(onFinish/onCancel),如需在动画结束后执行逻辑,可用
dispatch_after延迟近似处理。
UI 手势与控件
功能说明
SFMapView 直接提供手势和控件的开关属性,无需像 Android 那样获取单独的 UiSettings 对象。可分别开关缩放、平移、旋转、倾斜手势,以及指北针、比例尺等内置控件,还支持控件位置和边距调整、内容内边距设置。
关键代码
objc
SFMapView *mapView = self.mapView;
// ── 手势开关 ──
mapView.zoomEnabled = YES; // 双指缩放 / 双击缩放
mapView.scrollEnabled = YES; // 拖拽平移
mapView.rotateEnabled = YES; // 双指旋转
mapView.pitchEnabled = YES; // 双指倾斜
// 禁用全部手势(逐个设置,iOS 无统一 API)
mapView.zoomEnabled = NO;
mapView.scrollEnabled = NO;
mapView.rotateEnabled = NO;
mapView.pitchEnabled = NO;
// ── 内置控件 ──
mapView.showsCompass = YES; // 指北针(地图旋转时显示)
mapView.showsScale = YES; // 比例尺
// ── 控件位置 ──
// 指北针在 4 个角之间切换
mapView.compassViewPosition = SFMOrnamentPositionTopRight; // 右上
mapView.compassViewPosition = SFMOrnamentPositionTopLeft; // 左上
mapView.compassViewPosition = SFMOrnamentPositionBottomLeft; // 左下
mapView.compassViewPosition = SFMOrnamentPositionBottomRight; // 右下
// 指北针边距
mapView.compassViewMargins = CGPointMake(24, 24);
// 比例尺位置与边距
mapView.scaleBarPosition = SFMOrnamentPositionBottomLeft;
mapView.scaleBarMargins = CGPointMake(24, 24);
// ── 内容内边距(地图可视区域避让导航栏/工具栏)──
// 带动画设置(顶部避让 100pt)
[mapView setContentInset:UIEdgeInsetsMake(100, 0, 0, 0) animated:YES];
// 直接设置(底部避让 80pt)
UIEdgeInsets inset = mapView.mapContentInset;
inset.bottom = 80;
mapView.mapContentInset = inset;要点
- 手势默认均为开启状态,按需关闭即可。
- iOS 无
setAllGesturesEnabled统一 API,需逐个设置四个手势属性。 - 指北针仅在地图发生旋转时才显示,正北朝向时自动隐藏。
SFMOrnamentPosition枚举有四个值:TopRight、TopLeft、BottomLeft、BottomRight。setContentInset:animated:会使地图相机在可视区域(减去内边距后)居中,适合导航栏/工具栏避让场景。- Android 的缩放按钮(
setZoomControlsEnabled)在 iOS 无对应控件,省略。
事件监听
功能说明
SDK 通过 SFMapViewDelegate 协议提供丰富的事件回调,覆盖地图点击/长按、相机变化、注解(Marker)点击、信息窗口点击、线/面覆盖物点击、POI 点击、底层触摸、注解拖拽等。所有回调均通过设置 mapView.delegate 后由协议方法触发。
关键代码
objc
// ── 地图点击与长按 ──
- (void)mapView:(SFMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
// coordinate 为点击处的地理坐标
}
- (void)mapView:(SFMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate {
// 长按回调
}
// ── 相机变化 ──
- (void)mapView:(SFMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
// 变化开始时回调
}
- (void)mapView:(SFMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
// 变化结束后回调一次
double zoom = mapView.zoomLevel;
}
// ── 注解(Marker)点击 ──
- (BOOL)mapView:(SFMapView *)mapView didTapAnnotation:(id<SFMAnnotation>)annotation {
// 返回 NO: 放行默认行为(弹出 Callout)
// 返回 YES: 拦截,不弹出 Callout
return NO;
}
// ── 信息窗口(Callout)点击 ──
- (void)mapView:(SFMapView *)mapView didTapCalloutForAnnotation:(id<SFMAnnotation>)annotation {
// annotation.title / annotation.subtitle
}
// ── Polyline 点击 ──
- (void)mapView:(SFMapView *)mapView didTapPolyline:(SFMPolyline *)polyline {
// polyline.pointCount / [polyline getCoordinates:range:]
}
// ── Polygon 点击 ──
- (void)mapView:(SFMapView *)mapView didTapPolygon:(SFMPolygon *)polygon {
// polygon.pointCount
}
// ── POI 点击(点击地图上的内置兴趣点)──
- (BOOL)mapView:(SFMapView *)mapView didTapTouchPoi:(SFMTouchPoi *)poi {
// poi.name / poi.coordinate
return YES; // 返回 YES 拦截默认行为
}
// ── 底层触摸事件 ──
- (void)mapView:(SFMapView *)mapView didReceiveTouch:(UITouch *)touch
coordinate:(CLLocationCoordinate2D)coordinate {
// 所有手势的原始 UITouch 都会经过此回调,高频触发
}
// ── 注解拖拽 ──
- (void)mapView:(SFMapView *)mapView
annotationView:(SFMAnnotationView *)view
didChangeDragState:(SFMAnnotationViewDragState)state
fromOldState:(SFMAnnotationViewDragState)oldState {
switch (state) {
case SFMAnnotationViewDragStateStarting: /* 拖拽开始 */ break;
case SFMAnnotationViewDragStateDragging: /* 拖拽中 */ break;
case SFMAnnotationViewDragStateEnding: /* 拖拽结束 */ break;
case SFMAnnotationViewDragStateCanceling: /* 拖拽取消 */ break;
default: break;
}
}添加可点击覆盖物示例
objc
// 添加可点击的 Polyline
CLLocationCoordinate2D coords[3] = { /* ... */ };
SFMPolyline *polyline = [SFMPolyline polylineWithCoordinates:coords count:3];
[self.mapView addOverlay:polyline];
// 添加可点击的 Polygon
CLLocationCoordinate2D coords[4] = { /* ... */ };
SFMPolygon *polygon = [SFMPolygon polygonWithCoordinates:coords count:4];
[self.mapView addOverlay:polygon];
// 添加可拖拽 Marker
SFMPointAnnotation *a = [[SFMPointAnnotation alloc] init];
a.coordinate = shenzhen;
a.title = @"拖拽我";
[self.mapView addAnnotation:a];
// 在 viewForAnnotation: 中设置 view.draggable = YES要点
didTapAnnotation:返回NO时执行默认行为(弹出 Callout),返回YES则拦截。regionDidChangeAnimated:在相机变化结束后回调一次,适合获取最终状态;regionWillChangeAnimated:在变化开始时回调。注意regionDidChangeAnimated:在手势过程中可能高频触发,内部不要执行耗时操作。didTapTouchPoi:返回YES可拦截 POI 默认行为。SFMTouchPoi包含name和coordinate属性。didReceiveTouch:coordinate:为高频回调,所有手势都会经过,避免在此做重逻辑。- 拖拽状态有 5 个枚举值:
None、Starting、Dragging、Canceling、Ending。
