fuchengshen
2022-07-09 3a25c05b5ab837714f30469cdcb1e2a77d8d7f6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
let routeLayer
let gaodeRoute
 
// 当前页面业务相关
let startGraphic
let endPointArr
let poiLayer
let queryGaodePOI
 
// 需要覆盖config.json中地图属性参数(当前示例框架中自动处理合并)
export const mapOptions = {
  scene: {
    center: { lat: 31.812769, lng: 117.250545, alt: 18500, 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({})
 
  queryGaodePOI = new mars3d.query.GaodePOI({})
 
  // 创建矢量数据图层
  poiLayer = new mars3d.layer.GraphicLayer()
  map.addLayer(poiLayer)
 
  poiLayer.bindPopup(function (event) {
    const item = event.graphic.attr
 
    let inHtml = '<div class="mars3d-template-titile">' + item.name + '</div><div class="mars3d-template-content" >'
 
    const type = String(item.type).trim()
    if (type) {
      inHtml += "<div><label>类别</label>" + type + "</div>"
    }
 
    const xzqh = String(item.xzqh).trim()
    if (xzqh) {
      inHtml += "<div><label>区域</label>" + xzqh + "</div>"
    }
 
    const tel = String(item.tel).trim()
    if (tel) {
      inHtml += "<div><label>电话</label>" + tel + "</div>"
    }
 
    if (item.address) {
      const address = item.address.trim()
      inHtml += "<div><label>地址</label>" + address + "</div>"
    }
    inHtml += "</div>"
    return inHtml
  })
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
// 起点
export function startPoint() {
  if (startGraphic) {
    startGraphic.remove()
    startGraphic = null
  }
  routeLayer.clear()
 
  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()
 
      return point.lng + "," + point.lat
    })
}
 
// 终点
export function endPoint() {
  showLoading()
  routeLayer.clear()
  poiLayer.clear()
  endPointArr = null
 
  const extent = map.getExtent() // 当前视域内
 
  queryGaodePOI.queryPolygon({
    text: "企业",
    polygon: [
      [extent.xmin, extent.ymin],
      [extent.xmin, extent.ymax],
      [extent.xmax, extent.ymax],
      [extent.xmax, extent.ymin]
    ],
    page: 0,
    count: 10,
    success: function (res) {
      hideLoading()
 
      const count = res.count
      eventTarget.fire("end", { count })
 
      addEndPointEntity(res.list)
    },
    error: function (msg) {
      globalMsg(msg)
      hideLoading()
    }
  })
}
 
// 开始分析
export function btnAnalyse(type, count) {
  if (!startGraphic || !endPointArr || endPointArr.length === 0) {
    globalMsg("请设置起点和查询目的地")
    return
  }
  showLoading()
 
  queryRoute(type)
}
 
function queryRoute(type) {
  const startPoint = startGraphic.coordinate
  const points = []
 
  endPointArr.forEach((item) => {
    points.push([startPoint, [item.x, item.y]])
  })
 
  gaodeRoute.queryArr({
    type: Number(type), // GaodeRouteType枚举类型
    points: points,
    success: function (data) {
      hideLoading()
 
      showRouteResult(data)
    },
    error: function (msg) {
      hideLoading()
      globalAlert(msg)
    }
  })
}
 
function showRouteResult(data) {
  for (let i = 0; i < data.length; i++) {
    const item = data[i]
    if (!item) {
      continue
    }
 
    const lnglats = item.points
    if (!lnglats || lnglats.length < 1) {
      continue
    }
 
    const name = endPointArr[i].name
 
    const time = mars3d.Util.formatTime(item.allDuration)
    const distance = mars3d.MeasureUtil.formatDistance(item.allDistance)
    const html = "目的地:" + name + "<br/>总距离:" + distance + "<br/>所需时间:" + time + ""
 
    const graphic = new mars3d.graphic.PolylineEntity({
      positions: lnglats,
      style: {
        clampToGround: true,
        material: Cesium.Color.fromRandom({
          alpha: 0.7
        }),
        width: 4
      },
      popup: html
    })
    routeLayer.addGraphic(graphic)
 
    graphic.entityGraphic.material_old = graphic.entityGraphic.material
    graphic.entityGraphic.width_old = graphic.entityGraphic.width
 
    const id = graphic.id
    eventTarget.fire("analyse", { i, name, distance, time, id })
  }
}
 
// 终点的POI查询
function addEndPointEntity(arr) {
  console.log("查询数据结果", arr)
 
  endPointArr = arr
 
  for (let i = 0; i < arr.length; i++) {
    const item = arr[i]
 
    const graphic = new mars3d.graphic.BillboardEntity({
      position: Cesium.Cartesian3.fromDegrees(item.x, item.y),
      style: {
        image: "img/marker/end.png",
        scale: 1,
        clampToGround: true, // 贴地
        label: {
          text: item.name,
          font: "20px 楷体",
          color: Cesium.Color.AZURE,
          outline: true,
          outlineColor: Cesium.Color.BLACK,
          outlineWidth: 2,
          horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
          verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          pixelOffset: new Cesium.Cartesian2(0, -30), // 偏移量
          distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000),
          clampToGround: true // 贴地
        }
      },
      attr: item
    })
    poiLayer.addGraphic(graphic)
  }
}
 
let lastRoute
export function centerAtRoute(id) {
  const graphic = routeLayer.getGraphicById(id)
 
  if (lastRoute) {
    lastRoute.entityGraphic.material = lastRoute.entityGraphic.material_old
    lastRoute.entityGraphic.width = lastRoute.entityGraphic.width_old
  }
 
  // 动画线材质
  graphic.entityGraphic.width = 5
  graphic.entityGraphic.material = mars3d.MaterialUtil.createMaterialProperty(mars3d.MaterialType.LineFlow, {
    color: Cesium.Color.CHARTREUSE,
    image: "img/textures/line-color-yellow.png",
    speed: 20
  })
 
  map.flyToGraphic(graphic)
 
  lastRoute = graphic
}
 
// 清除按钮
export function removeAll() {
  if (startGraphic) {
    startGraphic.remove()
    startGraphic = null
  }
  routeLayer.clear()
  poiLayer.clear()
  endPointArr = null
}