Skip to content

Point 类

Point 类用于在地图上添加和管理点要素,支持普通点、聚合点、DOM 点和 Vue 组件点。

构造函数

typescript
constructor(map: Map)
  • map: OpenLayers 地图实例。

接口定义

PointOptions

继承自 BaseOptions, StyleOptions, TextOptions

属性名类型说明
textKeystring数据中用于显示文本的字段键名
imgstring图标图片的 URL
scalenumber图标缩放比例
iconColorstring图标颜色(用于改变图标色调)
layerNamestring图层名称(必填,继承自 BaseOptions)
zIndexnumber图层层级
visibleboolean是否可见
styleStyle | Style[] | ((feature: FeatureLike) => Style | Style[])自定义样式函数

ClusterOptions

继承自 PointOptions

属性名类型说明
distancenumber聚合距离(像素),默认为 20
minDistancenumber最小聚合距离

PointData

属性名类型说明
lgtdnumber经度
lttdnumber纬度
[key: string]any其他业务数据字段

VueTemplatePointOptions

属性名类型说明
TemplateanyVue 组件模板
lgtdnumber经度
lttdnumber纬度
propsany传递给组件的 props
styleType'default' | 'custom'样式类型
positioningstring定位方式,如 'bottom-center'
stopEventboolean是否阻止事件冒泡
visibleboolean是否可见
classNamestring自定义类名
zIndexnumber层级

方法

addPoint

添加普通点图层。

typescript
addPoint(pointData: PointData[], options: PointOptions): VectorLayer<VectorSource> | null
  • pointData: 点位数据数组。
  • options: 配置选项。
  • 返回值: 创建的向量图层,如果数据无效返回 null

addClusterPoint

添加聚合点图层。

typescript
addClusterPoint(pointData: PointData[], options: ClusterOptions): VectorLayer<VectorSource> | null
  • pointData: 点位数据数组。
  • options: 聚合配置选项。
  • 返回值: 创建的聚合图层。

addDomPoint

添加 DOM 元素点(Overlay)。

typescript
addDomPoint(twinkleList: TwinkleItem[], callback?: Function): {
  anchors: Overlay[],
  remove: () => void,
  setVisible: (visible: boolean) => void
}
  • twinkleList: 包含经纬度和类名的数据列表。
  • callback: 点击回调函数。
  • 返回值: 控制对象,包含 removesetVisible 方法。

addVueTemplatePoint

添加 Vue 组件作为点位。

typescript
addVueTemplatePoint(pointDataList: PointData[], template: any, options?: VueTemplatePointOptions): {
  setVisible: (visible: boolean) => void,
  setOneVisibleByProp: (propName: string, propValue: any, visible: boolean) => void,
  remove: () => void,
  getPoints: () => VueTemplatePointInstance[]
}
  • pointDataList: 点位数据列表。
  • template: Vue 组件。
  • options: 配置选项。
  • 返回值: 控制对象,包含显示/隐藏和移除方法。

使用示例

添加普通点

typescript
import { Point } from 'my-openlayers';

const point = new Point(map);
const data = [
  { lgtd: 116.40, lttd: 39.90, name: '北京' },
  { lgtd: 121.47, lttd: 31.23, name: '上海' }
];

point.addPoint(data, {
  layerName: 'cities',
  img: 'path/to/icon.png',
  textKey: 'name',
  scale: 0.8
});

添加聚合点

typescript
point.addClusterPoint(data, {
  layerName: 'clusters',
  distance: 40,
  img: 'path/to/cluster-icon.png'
});

添加 Vue 组件点

typescript
import MyComponent from './MyComponent.vue';

const ctrl = point.addVueTemplatePoint(data, MyComponent, {
  positioning: 'bottom-center',
  props: { status: 'active' }
});

// 隐藏所有点
ctrl.setVisible(false);

Released under the MIT License.