Skip to content

refactor(maps): migrate gmap & tdtmap adapters to BaseMap#2875

Open
lzxue wants to merge 1 commit into
masterfrom
feat/basemap-migration-gmap-tdtmap
Open

refactor(maps): migrate gmap & tdtmap adapters to BaseMap#2875
lzxue wants to merge 1 commit into
masterfrom
feat/basemap-migration-gmap-tdtmap

Conversation

@lzxue

@lzxue lzxue commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

refactor(maps): gmap & tdtmap 迁移到 BaseMap

延续上一轮创建的 lib/base-map.tsBaseMap,取代已 @deprecatedutils/BaseMapService)。此前仅 amap-next 用了新基类,本轮把两个使用 any 类型参数的非 mapbox 适配器迁过去,行为保持不变。

改动(gmap, tdtmap)

  • extends BaseMapService<any>extends BaseMap<any>
  • $mapContainermapContainerBaseMap 字段名)
  • cameraChangedCallback(viewport)cameraChangedCallback?.(viewport)(与已迁移的 amap-next 一致,BaseMap 中该字段为可选)
  • tdtmapviewport 声明由 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> 交集借用了 mapbase Map 的方法签名,迁移到 BaseMap 后会暴露 on/off/getMinZoom/getContainerTS2333,需配合 @types 补全或类型放宽决策后再单独处理(已在 commit message 标注)。

验证

  • @antv/l7-maps esm/cjs(52 文件)+ umd 全部构建通过
  • tscTS2654/TS2416
  • eslint 通过

后续(未在本 PR 范围)

  • mapbox 类适配器(map/mapbox/maplibre/earth):严重继承 BaseMapService 约 30 个 mapbox 通用实现,需先抽 MapboxBaseMap extends BaseMap 中间基类
  • bmap/tmap:待 @types 决策
  • 全部迁完后删除 utils/BaseMapService.ts 及其导出

改动

2 files changed, 90 insertions(+), 23 deletions(-)

将两个使用 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 通过。
@changeset-bot

changeset-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 87c1e44

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +522 to +542
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));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The getMapStyle and setMapStyle methods were copied verbatim from the Mapbox-centric BaseMapService. However, TianDiTu (T.Map) does not natively support getStyle() or setStyle().

  1. Calling this.map?.setStyle will result in a runtime TypeError because setStyle is undefined on T.Map.
  2. The Mapbox-specific regex and @ts-ignore in getMapStyle are 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.

Suggested change
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));
}
}

Comment on lines +30 to 32
export default class GMapService extends BaseMap<any> {
// @ts-ignore
protected viewport: IViewport = null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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();

Comment on lines +367 to +378
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 '';
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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 ?? '';
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant