Skip to content

弹窗 Popup 与提示 Tooltip

标准版提供 PopupTooltip 两个类,用于在地图上显示信息气泡和悬浮提示。


弹窗组件,可在地图指定位置显示 HTML 内容。

构造函数

ts
class Popup {
  readonly id: string;

  constructor(options?: PopupOptions);
}
ts
interface PopupOptions {
  position?: Coordinate;
  content?: string | HTMLElement;
  offset?: [number, number, number?];
  closeButton?: boolean;
  maxWidth?: number;
  className?: string;
}

属性

属性类型默认值说明
idstring自动生成弹窗唯一标识

方法

方法参数返回值说明
setPosition(c)c: Coordinatethis设置弹窗位置
setContent(c)c: string | HTMLElementthis设置弹窗内容
setOffset(o)o: [number, number, number?]this设置偏移量
setFeature(f)f: Featurethis关联要素
open()this打开弹窗
close()this关闭弹窗
isOpen()boolean是否打开
getElement()HTMLElement获取 DOM 元素
update()this刷新弹窗

示例

基本用法

ts
import { Popup } from '@gmap/standard';

const popup = new Popup({
  position: [116.397, 39.908],
  content: '<h3>天安门广场</h3><p>北京市中心</p>',
  offset: [0, -20],
  closeButton: true,
  maxWidth: 300,
  className: 'my-popup',
});

popup.open();

绑定到要素

ts
import { Popup, Point } from '@gmap/standard';

const popup = new Popup({
  content: '<strong>站点 A</strong>',
});

// 假设 feature 是从查询得到的要素
popup.setFeature(feature);
popup.setPosition(feature.geometry.getCentroid().get());
popup.open();

动态更新内容

ts
const popup = new Popup({ position: [116.4, 39.9] });
popup.open();

// 动态更新
popup.setContent('<p>加载中...</p>');

// 异步获取数据后更新
fetch('/api/info/123')
  .then(res => res.json())
  .then(data => {
    popup.setContent(`
      <h3>${data.name}</h3>
      <p>类型: ${data.type}</p>
      <p>面积: ${data.area} km²</p>
    `);
    popup.update();
  });

链式调用

ts
const popup = new Popup()
  .setPosition([116.397, 39.908])
  .setContent('Hello!')
  .setOffset([0, -10])
  .open();

关闭弹窗

ts
// 关闭
popup.close();
console.log(popup.isOpen()); // false

Tooltip

提示组件,在鼠标悬停时显示简短信息。

构造函数

ts
class Tooltip {
  readonly id: string;

  constructor();
}

属性

属性类型说明
idstring提示唯一标识(自动生成)

方法

方法参数返回值说明
show(coord, content?)coord: Coordinate, content?: stringthis在指定位置显示提示
showForFeature(f, content?)f: Feature, content?: stringthis为要素显示提示
hide()this隐藏提示
setContent(c)c: stringthis设置提示内容

示例

基本用法

ts
import { Tooltip } from '@gmap/standard';

const tooltip = new Tooltip();

// 在指定位置显示
tooltip.show([116.397, 39.908], '这里是天安门');

// 更新内容
tooltip.setContent('更新后的提示');

// 隐藏
tooltip.hide();

配合图层使用

ts
import { Tooltip } from '@gmap/standard';

const tooltip = new Tooltip();

// 当鼠标悬停在要素上时显示提示
layer.on('mouseenter', (e) => {
  const name = e.feature.properties.name;
  const value = e.feature.properties.value;
  tooltip.showForFeature(e.feature, `${name}: ${value}`);
});

layer.on('mouseleave', () => {
  tooltip.hide();
});

链式调用

ts
const tooltip = new Tooltip()
  .show([116.4, 39.9], '提示内容')
  .setContent('更新内容');

特性PopupTooltip
交互方式点击触发悬停触发
内容类型HTML 字符串或 HTMLElement纯文本字符串
是否可关闭支持关闭按钮自动隐藏
定位方式坐标 + 偏移跟随鼠标
关联要素setFeature()showForFeature()
典型用途要素详情面板要素名称提示

综合示例

ts
import { Popup, Tooltip, GeoJsonSource, FeatureLayer, SimpleRenderer, FillSymbol } from '@gmap/standard';

const source = new GeoJsonSource({ id: 'districts', data: districtsGeoJSON });
const layer = new FeatureLayer({
  id: 'districts',
  source,
  renderer: new SimpleRenderer({ symbol: new FillSymbol({ color: '#3388ff' }) }),
});

const popup = new Popup({ offset: [0, -15], closeButton: true, maxWidth: 350 });
const tooltip = new Tooltip();

// 悬停显示名称
layer.on('mouseenter', (e) => {
  tooltip.showForFeature(e.feature, e.feature.properties.name);
});

layer.on('mouseleave', () => {
  tooltip.hide();
});

// 点击显示详情
layer.on('click', (e) => {
  const p = e.feature.properties;
  popup
    .setFeature(e.feature)
    .setPosition(e.coordinate)
    .setContent(`
      <div class="popup-content">
        <h3>${p.name}</h3>
        <table>
          <tr><td>人口</td><td>${p.population.toLocaleString()}</td></tr>
          <tr><td>面积</td><td>${p.area} km²</td></tr>
          <tr><td>密度</td><td>${(p.population / p.area).toFixed(0)} 人/km²</td></tr>
        </table>
      </div>
    `)
    .open();
});

map.on('click', () => popup.close());

四川省交通运输综合地理服务平台 地图开发框架