refactor(maps): migrate gmap & tdtmap adapters to BaseMap#2875
Conversation
将两个使用 any 类型参数的非 mapbox 适配器从已废弃的 BaseMapService 迁移到新基类 BaseMap (lib/base-map.ts),行为保持不变: - gmap, tdtmap: extends BaseMapService -> extends BaseMap - $mapContainer -> mapContainer (BaseMap 字段名) - cameraChangedCallback(viewport) -> cameraChangedCallback?.(viewport) (与已迁移的 amap-next 一致,BaseMap 中该字段为可选) - tdtmap: viewport 声明由 IViewport | null = null 改为 IViewport = new Viewport() (BaseMap 抽象属性声明为非空 IViewport;原 BaseMapService 声明为 IViewport | unknown 才掩盖了不兼容。与 amap-next 范式一致) - 显式补全原先继承自 BaseMapService 的抽象方法 (getMapStyle/getMapStyleConfig/setMaxZoom/setMinZoom 等),方法体与原继承实现逐字一致 bmap/tmap 暂未迁移: 其地图类型 (BMapGL.Map/TMap.Map) 的 @types 不完整, 原 BaseMapService 通过 IMapService<Map & T> 交集借用 mapbase Map 的方法签名, 迁移到 BaseMap 后会暴露 on/off/getMinZoom/getContainer 等 TS2333, 需配合 @types 补全或类型放宽决策后再单独处理。 构建验证: @antv/l7-maps esm/cjs (52 文件) + umd 全部通过; tsc 无 TS2654/TS2416; eslint 通过。
|
There was a problem hiding this comment.
Code Review
This pull request refactors GMapService and TdtMapService to extend BaseMap instead of BaseMapService, renaming $mapContainer to mapContainer and explicitly implementing several map-related methods. The review feedback highlights that the newly added getMapStyle and setMapStyle methods in both services contain Mapbox-specific logic that is incompatible with Google Maps and TianDiTu, resulting in dead code and potential runtime errors. Additionally, it is recommended to initialize the viewport property directly in GMapService to align with TdtMapService and improve type safety.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| public getMapStyle(): string { | ||
| try { | ||
| // @ts-ignore | ||
| const styleUrl = this.map.getStyle().sprite ?? ''; | ||
| if (/^mapbox:\/\/sprites\/zcxduo\/\w+\/\w+$/.test(styleUrl)) { | ||
| return styleUrl?.replace(/\/\w+$/, '').replace(/sprites/, 'styles'); | ||
| } | ||
| return styleUrl; | ||
| } catch { | ||
| return ''; | ||
| } | ||
| } | ||
|
|
||
| public getMapStyleConfig(): MapStyleConfig { | ||
| return MapTheme; | ||
| } | ||
|
|
||
| public setMapStyle(style: MapStyleName): void { | ||
| // @ts-ignore | ||
| this.map?.setStyle(this.getMapStyleValue(style)); | ||
| } |
There was a problem hiding this comment.
The getMapStyle and setMapStyle methods were copied verbatim from the Mapbox-centric BaseMapService. However, TianDiTu (T.Map) does not natively support getStyle() or setStyle().
- Calling
this.map?.setStylewill result in a runtimeTypeErrorbecausesetStyleis undefined onT.Map. - The Mapbox-specific regex and
@ts-ignoreingetMapStyleare dead code and will always fail, falling back to returning''.
We should safely check if setStyle exists before calling it, and simplify getMapStyle to return the configured style from this.config.style or an empty string.
| public getMapStyle(): string { | |
| try { | |
| // @ts-ignore | |
| const styleUrl = this.map.getStyle().sprite ?? ''; | |
| if (/^mapbox:\/\/sprites\/zcxduo\/\w+\/\w+$/.test(styleUrl)) { | |
| return styleUrl?.replace(/\/\w+$/, '').replace(/sprites/, 'styles'); | |
| } | |
| return styleUrl; | |
| } catch { | |
| return ''; | |
| } | |
| } | |
| public getMapStyleConfig(): MapStyleConfig { | |
| return MapTheme; | |
| } | |
| public setMapStyle(style: MapStyleName): void { | |
| // @ts-ignore | |
| this.map?.setStyle(this.getMapStyleValue(style)); | |
| } | |
| public getMapStyle(): string { | |
| return this.config.style ?? ''; | |
| } | |
| public getMapStyleConfig(): MapStyleConfig { | |
| return MapTheme; | |
| } | |
| public setMapStyle(style: MapStyleName): void { | |
| if (this.map && typeof this.map.setStyle === 'function') { | |
| this.map.setStyle(this.getMapStyleValue(style)); | |
| } | |
| } |
| export default class GMapService extends BaseMap<any> { | ||
| // @ts-ignore | ||
| protected viewport: IViewport = null; |
There was a problem hiding this comment.
To align with the TdtMapService implementation and improve type safety, we can initialize viewport directly at declaration and remove the @ts-ignore and null assignment.
| export default class GMapService extends BaseMap<any> { | |
| // @ts-ignore | |
| protected viewport: IViewport = null; | |
| export default class GMapService extends BaseMap<any> { | |
| protected viewport: IViewport = new Viewport(); |
| public getMapStyle(): string { | ||
| try { | ||
| // @ts-ignore | ||
| const styleUrl = this.map.getStyle().sprite ?? ''; | ||
| if (/^mapbox:\/\/sprites\/zcxduo\/\w+\/\w+$/.test(styleUrl)) { | ||
| return styleUrl?.replace(/\/\w+$/, '').replace(/sprites/, 'styles'); | ||
| } | ||
| return styleUrl; | ||
| } catch { | ||
| return ''; | ||
| } | ||
| } |
There was a problem hiding this comment.
The getMapStyle method was copied verbatim from the Mapbox-centric BaseMapService. However, Google Maps (google.maps.Map) does not have a getStyle() method. This Mapbox-specific code is dead code and will always throw an error, falling back to returning ''.
We should simplify this to return the configured style from this.config.style or an empty string.
public getMapStyle(): string {
return this.config.style ?? '';
}
refactor(maps): gmap & tdtmap 迁移到 BaseMap
延续上一轮创建的
lib/base-map.ts(BaseMap,取代已@deprecated的utils/BaseMapService)。此前仅amap-next用了新基类,本轮把两个使用any类型参数的非 mapbox 适配器迁过去,行为保持不变。改动(gmap, tdtmap)
extends BaseMapService<any>→extends BaseMap<any>$mapContainer→mapContainer(BaseMap字段名)cameraChangedCallback(viewport)→cameraChangedCallback?.(viewport)(与已迁移的 amap-next 一致,BaseMap中该字段为可选)viewport声明由IViewport | null = null改为IViewport = new Viewport()。BaseMap抽象属性声明为非空IViewport,原BaseMapService声明为IViewport | unknown(= unknown)才掩盖了不兼容 —— 此处对齐 amap-next 范式BaseMapService的抽象方法(getMapStyle/getMapStyleConfig/setMaxZoom/setMinZoom等),方法体与原继承实现逐字一致bmap / tmap 暂未迁移
其地图类型
BMapGL.Map/TMap.Map的@types不完整,原BaseMapService通过implements IMapService<Map & T>交集借用了 mapbaseMap的方法签名,迁移到BaseMap后会暴露on/off/getMinZoom/getContainer等TS2333,需配合@types补全或类型放宽决策后再单独处理(已在 commit message 标注)。验证
@antv/l7-mapsesm/cjs(52 文件)+ umd 全部构建通过tsc无TS2654/TS2416eslint通过后续(未在本 PR 范围)
map/mapbox/maplibre/earth):严重继承BaseMapService约 30 个 mapbox 通用实现,需先抽MapboxBaseMap extends BaseMap中间基类@types决策utils/BaseMapService.ts及其导出改动
2 files changed, 90 insertions(+), 23 deletions(-)