import * as mars3d from "mars3d" export let map // mars3d.Map三维地图对象 let routeLayer // 矢量数据图层 let gaodeRoute // 高德 路径规划 // 当前页面业务相关 let startGraphic, endGraphic // 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并) export const mapOptions = { scene: { center: { lat: 31.797919, lng: 117.281329, alt: 36236, heading: 358, pitch: -81 } } } export const eventTarget = new mars3d.BaseClass() // 事件对象,用于抛出事件到面板中 /** * 初始化地图业务,生命周期钩子函数(必须) * 框架在地图初始化完成后自动调用该函数 * @param {mars3d.Map} mapInstance 地图对象 * @returns {void} 无 */ export function onMounted(mapInstance) { map = mapInstance // 记录map // 创建矢量数据图层 routeLayer = new mars3d.layer.GraphicLayer() map.addLayer(routeLayer) gaodeRoute = new mars3d.query.GaodeRoute({}) } /** * 释放当前地图业务的生命周期函数 * @returns {void} 无 */ export function onUnmounted() { map = null } // 开始分析按钮 export function btnAnalyse(type) { if (!startGraphic || !endGraphic) { globalMsg("请设置起点和终点") return } queryRoute(type) } // 清除按钮 export function removeAll() { if (startGraphic) { startGraphic.remove() startGraphic = null } if (endGraphic) { endGraphic.remove() endGraphic = null } routeLayer.clear() } /** * 起点按钮 * * @export * @param {string} type 不同方式路线查询 * @returns {string} */ export function startPoint(type) { if (startGraphic) { startGraphic.remove() startGraphic = null } return map.graphicLayer .startDraw({ type: "billboard", style: { image: "img/marker/start.png", horizontalOrigin: Cesium.HorizontalOrigin.CENTER, verticalOrigin: Cesium.VerticalOrigin.BOTTOM } }) .then((graphic) => { startGraphic = graphic const point = graphic.point point.format() // 触发自定义事件,改变输入框的值 queryRoute(type) return point.lng + "," + point.lat }) } /** * 终点按钮 * * @export * @param {string} type 不同方式路线查询 * @returns {string} */ export function endPoint(type) { if (endGraphic) { endGraphic.remove() endGraphic = null } return map.graphicLayer .startDraw({ type: "billboard", style: { image: "img/marker/end.png", horizontalOrigin: Cesium.HorizontalOrigin.CENTER, verticalOrigin: Cesium.VerticalOrigin.BOTTOM } }) .then((graphic) => { endGraphic = graphic const point = graphic.point point.format() queryRoute(type) return point.lng + "," + point.lat }) } function queryRoute(type) { if (!startGraphic || !endGraphic) { return } routeLayer.clear() showLoading() gaodeRoute.query({ type: Number(type), points: [startGraphic.coordinate, endGraphic.coordinate], success: function (data) { hideLoading() const firstItem = data.paths[0] const points = firstItem.points if (!points || points.length < 1) { return } const time = mars3d.Util.formatTime(firstItem.allDuration) const distance = mars3d.MeasureUtil.formatDistance(firstItem.allDistance) const html = "