import * as mars3d from "mars3d" export let map // mars3d.Map三维地图对象 export let graphicLayer // 矢量图层对象 // 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并) export const mapOptions = { scene: { center: { lat: 27.375302, lng: 115.43395, alt: 631709, heading: 26, pitch: -49 } } } /** * 初始化地图业务,生命周期钩子函数(必须) * 框架在地图初始化完成后自动调用该函数 * @param {mars3d.Map} mapInstance 地图对象 * @returns {void} 无 */ export function onMounted(mapInstance) { map = mapInstance // 记录map // 创建Graphic图层 graphicLayer = new mars3d.layer.GraphicLayer() map.addLayer(graphicLayer) bindLayerEvent() // 对图层绑定相关事件 bindLayerPopup() // 在图层上绑定popup,对所有加到这个图层的矢量数据都生效 bindLayerContextMenu() // 在图层绑定右键菜单,对所有加到这个图层的矢量数据都生效 // 合肥市 const lightCone = new mars3d.graphic.LightCone({ position: Cesium.Cartesian3.fromDegrees(117.29, 32.0581, 117.8), style: { color: "rgba(255,0,0,0.9)", radius: 8000, // 底部半径 height: 150000 // 椎体高度 }, show: true }) graphicLayer.addGraphic(lightCone) // 演示个性化处理graphic initGraphicManager(lightCone) const cities = [ { name: "六安市", lon: 116.3123, lat: 31.8329 }, { name: "安庆市", lon: 116.7517, lat: 30.5255 }, { name: "滁州市", lon: 118.1909, lat: 32.536 }, { name: "宣城市", lon: 118.8062, lat: 30.6244 }, { name: "阜阳市", lon: 115.7629, lat: 32.9919 }, { name: "宿州市", lon: 117.5208, lat: 33.6841 }, { name: "黄山市", lon: 118.0481, lat: 29.9542 }, { name: "巢湖市", lon: 117.7734, lat: 31.4978 }, { name: "亳州市", lon: 116.1914, lat: 33.4698 }, { name: "池州市", lon: 117.3889, lat: 30.2014 }, { name: "蚌埠市", lon: 117.4109, lat: 33.1073 }, { name: "芜湖市", lon: 118.3557, lat: 31.0858 }, { name: "淮北市", lon: 116.6968, lat: 33.6896 }, { name: "淮南市", lon: 116.7847, lat: 32.7722 }, { name: "马鞍山市", lon: 118.6304, lat: 31.5363 }, { name: "铜陵市", lon: 117.9382, lat: 30.9375 } ] for (let i = 0; i < cities.length; i++) { const item = cities[i] const coneGlow2 = new mars3d.graphic.LightCone({ position: Cesium.Cartesian3.fromDegrees(item.lon, item.lat, 0), style: { radius: 5000, height: 80000, distanceDisplayCondition: new Cesium.DistanceDisplayConditionGeometryInstanceAttribute(80000, 3010000), // 高亮时的样式(默认为鼠标移入,也可以指定type:'click'单击高亮),构造后也可以openHighlight、closeHighlight方法来手动调用 highlight: { type: mars3d.EventType.click, color: "#ffff00" } } // popup: item.name, }) graphicLayer.addGraphic(coneGlow2) } } /** * 释放当前地图业务的生命周期函数 * @returns {void} 无 */ export function onUnmounted() { map = null } // 在图层绑定Popup弹窗 export function bindLayerPopup() { graphicLayer.bindPopup(function (event) { const attr = event.graphic.attr || {} attr["类型"] = event.graphic.type attr["来源"] = "我是layer上绑定的Popup" attr["备注"] = "我支持鼠标交互" return mars3d.Util.getTemplateHtml({ title: "矢量图层", template: "all", attr: attr }) }) } // 在图层级处理一些事物 function bindLayerEvent() { // 在layer上绑定监听事件 graphicLayer.on(mars3d.EventType.click, function (event) { console.log("监听layer,单击了矢量对象", event) }) /* graphicLayer.on(mars3d.EventType.mouseOver, function (event) { console.log("监听layer,鼠标移入了矢量对象", event) }) graphicLayer.on(mars3d.EventType.mouseOut, function (event) { console.log("监听layer,鼠标移出了矢量对象", event) }) */ } // 绑定右键菜单 export function bindLayerContextMenu() { graphicLayer.bindContextMenu([ { text: "删除对象", icon: "fa fa-trash-o", show: (event) => { const graphic = event.graphic if (!graphic || graphic.isDestroy) { return false } else { return true } }, callback: function (e) { const graphic = e.graphic if (!graphic) { return } const parent = graphic._parent // 右击是编辑点时 graphicLayer.removeGraphic(graphic) if (parent) { graphicLayer.removeGraphic(parent) } } } ]) } // 也可以在单个Graphic上做个性化管理及绑定操作 function initGraphicManager(graphic) { // 3.在graphic上绑定监听事件 /* graphic.on(mars3d.EventType.click, function (event) { console.log("监听graphic,单击了矢量对象", event) }) graphic.on(mars3d.EventType.mouseOver, function (event) { console.log("监听graphic,鼠标移入了矢量对象", event) }) graphic.on(mars3d.EventType.mouseOut, function (event) { console.log("监听graphic,鼠标移出了矢量对象", event) }) */ // 绑定Tooltip // graphic.bindTooltip('我是graphic上绑定的Tooltip') //.openTooltip() // 绑定Popup const inthtml = `
我是graphic上绑定的Popup
提示: 这只是测试信息,可以任意html
` graphic.bindPopup(inthtml).openPopup() // 绑定右键菜单 graphic.bindContextMenu([ { text: "删除对象[graphic绑定的]", icon: "fa fa-trash-o", callback: function (e) { const graphic = e.graphic if (graphic) { graphic.remove() } } } ]) }