Lin_Ye_Fang_Huo/public/widgets/addmarker/widget.js

339 lines
9.1 KiB
JavaScript
Raw Normal View History

2023-07-18 09:04:15 +08:00
(function (window, mars3d) {
2023-07-08 15:37:34 +08:00
//创建widget类需要继承BaseWidget
class MyWidget extends mars3d.widget.BaseWidget {
//外部资源配置
get resources() {
2023-07-18 09:04:15 +08:00
return ["map.css"];
2023-07-08 15:37:34 +08:00
}
//弹窗配置
get view() {
return {
2023-07-18 09:04:15 +08:00
type: "window",
url: "view.html",
2023-07-08 15:37:34 +08:00
windowOptions: {
width: 300,
height: 400,
},
2023-07-18 09:04:15 +08:00
};
2023-07-08 15:37:34 +08:00
}
//初始化[仅执行1次]
create() {
2023-07-18 09:04:15 +08:00
this.storageName = "mars3d_addmarker";
this.editable = true;
2023-07-08 15:37:34 +08:00
this.graphicLayer = new mars3d.layer.GraphicLayer({
name: this.config.name,
pid: 99, //图层管理 中使用父节点id
hasEdit: false,
2023-07-18 09:04:15 +08:00
});
this.map.addLayer(this.graphicLayer);
2023-07-08 15:37:34 +08:00
this.graphicLayer.bindPopup(
(event) => {
2023-07-18 09:04:15 +08:00
let graphic = event.graphic;
if (!graphic) {
return;
}
2023-07-08 15:37:34 +08:00
var html = mars3d.Util.getTemplateHtml({
2023-07-18 09:04:15 +08:00
title: "我的标记",
2023-07-08 15:37:34 +08:00
template: [
2023-07-18 09:04:15 +08:00
{ field: "name", name: "名称" },
{ field: "remark", name: "备注", type: "textarea" },
this.editable ? { field: "id", name: "确定", type: "button" } : null,
2023-07-08 15:37:34 +08:00
],
attr: {
id: graphic.id,
...graphic.attr,
},
edit: this.editable,
width: 190,
2023-07-18 09:04:15 +08:00
});
return html;
2023-07-08 15:37:34 +08:00
},
{
onAdd: (event) => {
let eleId = event.id,
2023-07-18 09:04:15 +08:00
graphic = event.graphic;
2023-07-08 15:37:34 +08:00
//popup的DOM添加到页面的回调方法
2023-07-18 09:04:15 +08:00
$("#" + eleId + " .mars3d-popup-btn").click((e) => {
$("#" + eleId + " .mars3d-popup-edititem").each(function () {
var val = $(this).val();
var key = $(this).attr("data-type");
graphic.attr[key] = val;
});
this.saveEditFeature(graphic);
});
2023-07-08 15:37:34 +08:00
},
onRemove: function (eleId, graphic) {
//popup的DOM从页面移除的回调方法
},
anchor: [0, -35],
}
2023-07-18 09:04:15 +08:00
);
2023-07-08 15:37:34 +08:00
//事件监听
this.graphicLayer.on(mars3d.EventType.drawCreated, (e) => {
2023-07-18 09:04:15 +08:00
var graphic = e.graphic;
if (!graphic || !graphic.positionShow) {
return;
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
graphic.attr.name = "我的标记";
2023-07-08 15:37:34 +08:00
this.saveEntity(graphic, () => {
2023-07-18 09:04:15 +08:00
this.graphicLayer.openPopup(graphic);
});
});
2023-07-08 15:37:34 +08:00
this.graphicLayer.on(mars3d.EventType.editMovePoint, (e) => {
2023-07-18 09:04:15 +08:00
this.saveEntity(e.graphic); //编辑修改了点
});
this.graphicLayer.on(mars3d.EventType.removeGraphic, (e) => {
this.deleteItem(e.graphic.id); //保存数据
});
2023-07-08 15:37:34 +08:00
2023-07-18 09:04:15 +08:00
this.getList();
2023-07-08 15:37:34 +08:00
}
//每个窗口创建完成后调用
winCreateOK(opt, result) {
2023-07-18 09:04:15 +08:00
this.viewWindow = result;
2023-07-08 15:37:34 +08:00
}
//激活插件
activate() {
2023-07-18 09:04:15 +08:00
this.hasEdit(true);
2023-07-08 15:37:34 +08:00
}
//释放插件
disable() {
2023-07-18 09:04:15 +08:00
this.viewWindow = null;
this.stopDraw();
this.hasEdit(false);
2023-07-08 15:37:34 +08:00
}
stopDraw() {
2023-07-18 09:04:15 +08:00
this.graphicLayer.stopDraw();
2023-07-08 15:37:34 +08:00
}
//配置的样式
getStyle(name) {
return {
scale: 1,
2023-07-18 09:04:15 +08:00
image: this.path + "img/marker.png",
2023-07-08 15:37:34 +08:00
scaleByDistance: new Cesium.NearFarScalar(1.5e2, 1, 8.0e6, 0.2),
label: {
2023-07-18 09:04:15 +08:00
text: name || "我的标记",
2023-07-08 15:37:34 +08:00
font_size: 20,
outline: true,
outlineColor: Cesium.Color.BLACK,
outlineWidth: 3,
horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
pixelOffset: new Cesium.Cartesian2(0, -50),
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000),
},
2023-07-18 09:04:15 +08:00
};
2023-07-08 15:37:34 +08:00
}
drawPoint() {
this.graphicLayer.startDraw({
2023-07-18 09:04:15 +08:00
type: "billboard",
2023-07-08 15:37:34 +08:00
style: this.getStyle(),
2023-07-18 09:04:15 +08:00
});
2023-07-08 15:37:34 +08:00
}
hasEdit(val) {
2023-07-18 09:04:15 +08:00
this.editable = val;
this.graphicLayer.hasEdit = val;
2023-07-08 15:37:34 +08:00
}
//========================
saveEditFeature(graphic) {
2023-07-18 09:04:15 +08:00
this.graphicLayer.closePopup();
graphic.label.text = graphic.attr.name;
2023-07-08 15:37:34 +08:00
2023-07-18 09:04:15 +08:00
this.saveEntity(graphic);
2023-07-08 15:37:34 +08:00
}
deleteItemById(id) {
2023-07-18 09:04:15 +08:00
let graphic = this.graphicLayer.getGraphicById(id);
2023-07-08 15:37:34 +08:00
if (graphic) {
2023-07-18 09:04:15 +08:00
graphic.remove();
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
this.deleteItem(id);
2023-07-08 15:37:34 +08:00
}
getGraphicAttrList() {
2023-07-18 09:04:15 +08:00
var arrAttr = [];
var arrGraphic = this.graphicLayer.getGraphics();
2023-07-08 15:37:34 +08:00
for (var i = 0, len = arrGraphic.length; i < len; i++) {
2023-07-18 09:04:15 +08:00
let graphic = arrGraphic[i];
let attr = graphic.attr;
2023-07-08 15:37:34 +08:00
if (!attr.name) {
2023-07-18 09:04:15 +08:00
attr.name = "我的标记";
2023-07-08 15:37:34 +08:00
}
arrAttr.push({
id: graphic.id,
...attr,
2023-07-18 09:04:15 +08:00
});
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
return arrAttr;
2023-07-08 15:37:34 +08:00
}
flyTo(id) {
2023-07-18 09:04:15 +08:00
let graphic = this.graphicLayer.getGraphicById(id);
2023-07-08 15:37:34 +08:00
if (graphic) {
//参数解释点数据radius控制视距距离
2023-07-18 09:04:15 +08:00
this.map.flyToGraphic(graphic, { radius: 2500 });
2023-07-08 15:37:34 +08:00
}
}
getJsonData() {
2023-07-18 09:04:15 +08:00
var arr = [];
var graphics = this.graphicLayer.getGraphics();
2023-07-08 15:37:34 +08:00
for (var i = 0, len = graphics.length; i < len; i++) {
2023-07-18 09:04:15 +08:00
var graphic = graphics[i];
var attr = graphic.attr;
var point = graphic.point.format();
2023-07-08 15:37:34 +08:00
arr.push({
id: graphic.id,
name: attr.name,
remark: attr.remark,
lat: point.lat,
lng: point.lng,
alt: point.alt,
2023-07-18 09:04:15 +08:00
});
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
return JSON.stringify(arr);
2023-07-08 15:37:34 +08:00
}
loadJson(arr, isclear) {
if (arr == null || arr.length == 0) {
2023-07-18 09:04:15 +08:00
return;
2023-07-08 15:37:34 +08:00
}
if (isclear) {
2023-07-18 09:04:15 +08:00
this.graphicLayer.clear();
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
var arrGraphic = [];
2023-07-08 15:37:34 +08:00
for (var i = 0; i < arr.length; i++) {
2023-07-18 09:04:15 +08:00
var item = arr[i];
2023-07-08 15:37:34 +08:00
if (!item.lat || !item.lng) {
2023-07-18 09:04:15 +08:00
continue;
2023-07-08 15:37:34 +08:00
}
if (!isclear) {
//叠加时清除已有同id数据
2023-07-18 09:04:15 +08:00
let graphic = this.graphicLayer.getGraphicById(item.id);
this.graphicLayer.removeGraphic(graphic);
2023-07-08 15:37:34 +08:00
}
var graphic = new mars3d.graphic.BillboardEntity({
id: item.id,
position: [item.lng, item.lat, item.alt],
style: this.getStyle(item.name),
attr: {
2023-07-18 09:04:15 +08:00
name: item.name || "",
remark: item.remark || "",
2023-07-08 15:37:34 +08:00
},
2023-07-18 09:04:15 +08:00
});
this.graphicLayer.addGraphic(graphic);
2023-07-08 15:37:34 +08:00
2023-07-18 09:04:15 +08:00
arrGraphic.push(graphic);
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
this.map.flyToGraphic(arrGraphic, { duration: 2.0, scale: 2 });
2023-07-08 15:37:34 +08:00
if (this.viewWindow) {
2023-07-18 09:04:15 +08:00
this.viewWindow.refMarkerList();
2023-07-08 15:37:34 +08:00
}
}
getList() {
if (window.hasServer) {
//读取服务端存储
// window.sendAjax({
// url: 'map/mark/list',
// type: 'get',
// success: (arr) => {
// this.loadJson(arr, true)
// },
// })
} else {
//读取本地存储
2023-07-18 09:04:15 +08:00
var laststorage = haoutil.storage.get(this.storageName); //读取localStorage值
if (laststorage != null && laststorage != "null") {
var arr = JSON.parse(laststorage);
this.loadJson(arr, true);
2023-07-08 15:37:34 +08:00
}
}
}
saveEntity(graphic, endfun) {
if (window.hasServer) {
//服务端存储
// var attr = graphic.attr
// var point = graphic.point
// window.sendAjax({
// url: 'map/mark/update',
// data: JSON.stringify({
// id: graphic.id,
// name: attr.name,
// remark: attr.remark,
// lng: point.lng,
// lat: point.lat,
// alt: point.alt,
// }),
// type: 'post',
// contentType: 'application/json',
// success: (data) => {
// if (endfun) {
// endfun()
// }
// this.viewWindow.refMarkerList()
// },
// })
} else {
//浏览器本地存储
2023-07-18 09:04:15 +08:00
var storagedata = this.getJsonData();
haoutil.storage.add(this.storageName, storagedata);
2023-07-08 15:37:34 +08:00
if (endfun) {
2023-07-18 09:04:15 +08:00
endfun();
2023-07-08 15:37:34 +08:00
}
2023-07-18 09:04:15 +08:00
this.viewWindow.refMarkerList();
2023-07-08 15:37:34 +08:00
}
}
deleteItem(id) {
2023-07-18 09:04:15 +08:00
this.graphicLayer.closePopup();
this.viewWindow.refMarkerList();
2023-07-08 15:37:34 +08:00
//服务端存储
if (window.hasServer) {
// window.sendAjax({
// url: 'map/mark/' + id,
// type: 'delete',
// success: function (data) {},
// })
} else {
//浏览器本地存储
2023-07-18 09:04:15 +08:00
var storagedata = this.getJsonData();
haoutil.storage.add(this.storageName, storagedata);
2023-07-08 15:37:34 +08:00
}
}
deleteAll() {
2023-07-18 09:04:15 +08:00
this.graphicLayer.clear();
2023-07-08 15:37:34 +08:00
if (this.viewWindow) {
2023-07-18 09:04:15 +08:00
this.viewWindow.refMarkerList();
2023-07-08 15:37:34 +08:00
}
if (window.hasServer && window.userId) {
//服务端存储
// window.sendAjax({
// url: 'map/mark/del/' + window.userId,
// type: 'delete',
// dataType: 'json',
// success: function (data) {},
// })
} else {
//浏览器本地存储
2023-07-18 09:04:15 +08:00
haoutil.storage.del(this.storageName);
2023-07-08 15:37:34 +08:00
}
}
}
//注册到widget管理器中。
2023-07-18 09:04:15 +08:00
mars3d.widget.bindClass(MyWidget);
2023-07-08 15:37:34 +08:00
//每个widet之间都是直接引入到index.html中会存在彼此命名冲突所以闭包处理下。
2023-07-18 09:04:15 +08:00
})(window, mars3d);