import {
deepMerge,
getLevelMiddleHeight,
wgs84ToGcj02Format,
convertPosition,
} from "../util";
import Mars3dEntity from "../base/mars3d_entity";
import GaodeEntity from "../base/gaode_entity";
import SijiEntity from "../base/siji_entity";
import CesiumEntity from "../base/cesium_entity";
export default (hnMap: any) => {
const defaultOption = {
id: "",
position: [],
num: 0,
color: "#ffffff",
opacity: 1,
size: 6,
fontColor: "#000000",
scale: 1,
scaleByDistance: true,
distanceDisplayCondition: false,
distanceDisplayCondition_far: 1,
distanceDisplayCondition_near: 18,
data: null,
};
class mars3d_class extends Mars3dEntity {
type: any = "numPoint";
id: any = null;
option: any = JSON.parse(JSON.stringify(defaultOption));
config: any = null;
graphic: any = null;
constructor(option: any) {
super(hnMap);
this.id = option.id;
deepMerge(this.option, option);
this.config = this.formatConfig(this.option);
this.graphic = new mars3d.graphic.BillboardEntity(this.config);
}
formatConfig(option: any) {
const distanceDisplayCondition_far = getLevelMiddleHeight(
option.distanceDisplayCondition_far
);
const distanceDisplayCondition_near = getLevelMiddleHeight(
option.distanceDisplayCondition_near
);
return {
id: option.id,
position: option.position,
style: {
image: mars3d.Util.getCircleImage(option.num, {
color: option.color,
radius: option.size / 2,
borderWidth: 0,
fontColor: option.fontColor,
}),
scale: option.scale,
clampToGround: !option.position[2],
scaleByDistance: option.scaleByDistance,
distanceDisplayCondition: option.distanceDisplayCondition,
distanceDisplayCondition_far: distanceDisplayCondition_far,
distanceDisplayCondition_near: distanceDisplayCondition_near,
},
attr: option.data,
};
}
set(option: any) {
deepMerge(this.option, option);
this.config = this.formatConfig(this.option);
this.graphic.setOptions(this.config);
}
}
class gaode_class extends GaodeEntity {
id: any = null;
option: any = JSON.parse(JSON.stringify(defaultOption));
config: any = null;
graphic: any = null;
constructor(option: any) {
super(hnMap);
this.id = option.id;
deepMerge(this.option, option);
this.config = this.formatConfig(this.option);
this.graphic = new AMap.Marker(this.config);
}
formatConfig(option: any) {
const amapPosition = wgs84ToGcj02Format(option.position);
var markerContent = `
${option.num}
`;
return {
id: option.id,
position: new AMap.LngLat(amapPosition[0], amapPosition[1]),
content: markerContent,
anchor: "top-center",
offset: [0, -20],
extData: {
id: option.id,
data: option.data,
},
};
}
set(option: any) {
deepMerge(this.option, option);
this.config = this.formatConfig(this.option);
this.graphic.setOptions(this.config);
}
}
class siji_class extends SijiEntity {
type: any = "numPoint";
id: any = null;
option: any = JSON.parse(JSON.stringify(defaultOption));
config: any = null;
configLabel: any = null;
constructor(option: any) {
super(hnMap);
this.id = option.id;
deepMerge(this.option, option);
this.config = this.formatConfig(this.option);
this.configLabel = this.formatConfigNum(this.option);
}
formatConfig(option: any) {
let config: any = {};
config = {
id: option.id,
type: "circle",
source: {
type: "geojson",
data: {
type: "FeatureCollection",
features: [
{
type: "Feature",
geometry: {
type: "Point",
coordinates: convertPosition(option.position),
},
properties: {
name: option.text,
},
},
],
},
},
paint: {
"circle-opacity": Number(option.opacity),
"circle-radius": Number(option.size),
"circle-color": option.color,
}, // 填充样式
};
return config;
}
formatConfigNum(option: any) {
let configLabel: any = {};
configLabel = {
id: "num_" + option.id,
type: "symbol",
source: {
type: "geojson",
data: {
type: "FeatureCollection",
features: [
{
type: "Feature",
geometry: {
type: "Point",
coordinates: convertPosition(option.position),
},
properties: {
name: option.num,
},
},
],
},
},
layout: {
"icon-anchor": "center",
"text-field": "{name}", // 文本
"text-size": Number(option.size),
"text-anchor": "center", // 顶部对齐
"text-ignore-placement": true, //
}, // 文本样式
paint: {
"text-color": option.fontColor,
}, // 填充样式
};
return configLabel;
}
set(option: any) {
deepMerge(this.option, option);
this.config = this.formatConfig(this.option);
this.configLabel = this.formatConfigNum(this.option);
let mySource = hnMap.map.map.getSource(this.config.id);
mySource.setData({
type: "FeatureCollection",
features: [
{
type: "Feature",
properties: { name: this.option.text },
geometry: {
type: "Point",
coordinates: this.option.position.map((num: any) => Number(num)),
},
},
],
});
for (let key in this.config) {
if (this.config.hasOwnProperty(key)) {
if (key == "paint") {
for (let key2 in this.config[key]) {
if (this.config[key].hasOwnProperty(key2)) {
// 遍历 paint 属性
hnMap.map.map.setPaintProperty(
this.config.id,
key2,
key2 == "circle-opacity"
? Number(this.config[key][key2])
: this.config[key][key2]
);
}
}
}
}
}
// 数字更改
let mySource_num = hnMap.map.map.getSource(this.configLabel.id);
mySource_num.setData({
type: "FeatureCollection",
features: [
{
type: "Feature",
properties: { name: this.option.num },
geometry: {
type: "Point",
coordinates: this.option.position.map((num: any) => Number(num)),
},
},
],
});
for (let key in this.configLabel) {
if (this.configLabel.hasOwnProperty(key)) {
if (key == "paint") {
for (let key2 in this.configLabel[key]) {
if (this.configLabel[key].hasOwnProperty(key2)) {
// 遍历 paint 属性
hnMap.map.map.setPaintProperty(
this.configLabel.id,
key2,
this.configLabel[key][key2]
);
}
}
}
if (key == "layout") {
for (let key2 in this.configLabel[key]) {
if (this.configLabel[key].hasOwnProperty(key2)) {
// 遍历 layout 属性
hnMap.map.map.setLayoutProperty(
this.configLabel.id,
key2,
key2 == "text-size"
? Number(this.configLabel[key][key2])
: this.configLabel[key][key2]
);
}
}
}
}
}
}
}
class cesium_class extends CesiumEntity {
type: any = "numPoint";
id: any = null;
option: any = JSON.parse(JSON.stringify(defaultOption));
config: any = null;
graphic: any = null;
constructor(option: any) {
super(hnMap);
this.id = option.id;
deepMerge(this.option, option);
this.config = this.formatConfig(this.option);
// 创建 Cesium 实体
this.graphic = new Cesium.Entity(this.config);
// 添加自定义属性
this.graphic.userData = {
type: "numPoint",
originalData: this.option.data || {},
};
}
formatConfig(option: any) {
// 转换位置坐标为 Cesium 格式
const position = Cesium.Cartesian3.fromDegrees(
option.position[0],
option.position[1],
option.position[2] || 0
);
// 准备距离参数但不立即创建 DistanceDisplayCondition 对象
const near = getLevelMiddleHeight(option.distanceDisplayCondition_near);
const far = getLevelMiddleHeight(option.distanceDisplayCondition_far);
const config = {
id: option.id,
position: position,
point: {
color: Cesium.Color.fromCssColorString(option.color).withAlpha(
Number(option.opacity) || 1
),
pixelSize: Number(option.size),
show: true,
clampToGround: !option.position[2],
// 只有当启用 distanceDisplayCondition 时才添加该属性
...(option.distanceDisplayCondition && {
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
near,
far
),
}),
scaleByDistance: option.scaleByDistance
? new Cesium.NearFarScalar(1.0e2, 1.0, 1.0e7, 0.1)
: undefined,
},
label: {
text: option.num + '',
fillColor: Cesium.Color.fromCssColorString(
option.fontColor
).withAlpha(1),
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
// 缩放
scale: Number(option.scale),
font: `${option.size / 1.5 }px PingFang SC`,
horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
verticalOrigin: Cesium.VerticalOrigin.CENTER,
disableDepthTestDistance: Number.POSITIVE_INFINITY, // 禁用深度测试
heightReference: Cesium.HeightReference.NONE, // 高度参考
// 只有当启用 distanceDisplayCondition 时才添加该属性
...(option.distanceDisplayCondition && {
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
near,
far
),
}),
scaleByDistance: option.scaleByDistance
? new Cesium.NearFarScalar(1.0e2, 1.0, 1.0e7, 0.1)
: undefined,
show: true,
},
properties: option.data || {}, // 添加自定义数据
};
return config;
}
// 设置方法
set(option: any) {
deepMerge(this.option, option);
this.config = this.formatConfig(this.option);
console.log(option, "====修改=====");
// 更新实体
if (this.graphic) {
// 更新位置
if (option.position !== undefined) {
this.graphic.position = Cesium.Cartesian3.fromDegrees(
option.position[0],
option.position[1],
option.position[2] || 0
);
}
// 更新点属性
if (this.graphic.point) {
const point = this.graphic.point;
if (option.size !== undefined) {
point.pixelSize = option.size;
}
if (option.color !== undefined || option.opacity !== undefined) {
point.color = Cesium.Color.fromCssColorString(
this.option.color
).withAlpha(Number(this.option.opacity));
}
if (option.scaleByDistance) {
point.scaleByDistance = new Cesium.NearFarScalar(
1.0e2,
1.0,
1.0e7,
0.1
);
}
}
// 更新标签属性
if (this.graphic.label) {
const label = this.graphic.label;
if (option.num !== undefined) {
label.text = option.num;
}
label.horizontalOrigin = Cesium.HorizontalOrigin.CENTER;
label.verticalOrigin = Cesium.VerticalOrigin.CENTER;
label.disableDepthTestDistance = Number.POSITIVE_INFINITY;
label.heightReference = Cesium.HeightReference.NONE;
if (option.fontColor !== undefined) {
label.fillColor = Cesium.Color.fromCssColorString(option.fontColor);
}
if (option.scale !== undefined) {
label.scale = Number(option.scale);
}
if (option.scaleByDistance) {
label.scaleByDistance = new Cesium.NearFarScalar(
1.0e2,
1.0,
1.0e7,
0.1
);
} else {
label.scaleByDistance = undefined;
}
}
// 更新距离显示条件
if (option.distanceDisplayCondition) {
const near = getLevelMiddleHeight(
option.distanceDisplayCondition_near
);
const far = getLevelMiddleHeight(option.distanceDisplayCondition_far);
if (this.graphic.point) {
this.graphic.point.distanceDisplayCondition =
new Cesium.DistanceDisplayCondition(near, far);
}
if (this.graphic.label) {
this.graphic.label.distanceDisplayCondition =
new Cesium.DistanceDisplayCondition(near, far);
}
} else {
this.graphic.point.distanceDisplayCondition = undefined;
}
}
}
// 销毁方法
destroy() {
if (this.graphic && hnMap.map.map && hnMap.map.map.entities) {
hnMap.map.map.entities.remove(this.graphic);
}
}
// 打开弹窗
openPopup(htmlContent: string) {
if (this.graphic && hnMap.map) {
this.graphic.description = htmlContent;
hnMap.map.selectedEntity = this.graphic;
}
}
}
const fn: any = {
mars3d: mars3d_class,
gaode: gaode_class,
siji: siji_class,
cesium: cesium_class,
};
return fn[hnMap.mapType];
};