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
import * as mars3d from "mars3d"
 
export let map // mars3d.Map三维地图对象
let graphicLayer // 矢量图层对象
 
let graphic1
let graphic2
 
export const mapOptions = {
  scene: {
    center: { lat: 31.715325, lng: 117.233867, alt: 21228, heading: 2, pitch: -60 }
  },
  layers: []
}
 
/**
 * 初始化地图业务,生命周期钩子函数(必须)
 * 框架在地图初始化完成后自动调用该函数
 * @param {mars3d.Map} mapInstance 地图对象
 * @returns {void} 无
 */
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
  addEntity()
}
 
/**
 * 释放当前地图业务的生命周期函数
 * @returns {void} 无
 */
export function onUnmounted() {
  map = null
}
 
function addEntity() {
  // 创建矢量数据图层
  graphicLayer = new mars3d.layer.GraphicLayer()
  map.addLayer(graphicLayer)
 
  // 面1
  graphic1 = new mars3d.graphic.PolygonEntity({
    positions: [
      [117.182288, 31.854164, 35.2],
      [117.210254, 31.878324, 28.2],
      [117.238229, 31.855796, 22.4],
      [117.242307, 31.826109, 12.5],
      [117.177277, 31.821475, 54.5],
      [117.182288, 31.854164, 35.2]
    ],
    style: {
      color: "#ff0000",
      opacity: 0.5,
      clampToGround: true
    }
  })
  graphicLayer.addGraphic(graphic1)
 
  // 面2
  graphic2 = new mars3d.graphic.PolygonEntity({
    positions: [
      [117.267046, 31.842971, 25.4],
      [117.20963, 31.840323, 37.2],
      [117.230646, 31.787122, 27.5],
      [117.28833, 31.799624, 20.6],
      [117.267046, 31.842971, 25.4]
    ],
    style: {
      color: "#0000FF",
      opacity: 0.5,
      clampToGround: true
    }
  })
  graphicLayer.addGraphic(graphic2)
}
 
// 求交
let intersectGraphic
export function intersect() {
  if (intersectGraphic) {
    graphicLayer.removeGraphic(intersectGraphic, true)
    intersectGraphic = null
  }
 
  const layer1 = graphic1.toGeoJSON()
  const layer2 = graphic2.toGeoJSON()
  console.log("2个面的geojson对象", layer1, layer2)
 
  const geojson = turf.intersect(layer1, layer2)
 
  intersectGraphic = mars3d.Util.geoJsonToGraphics(geojson)[0]
  intersectGraphic.type = "polygon"
  intersectGraphic.style = {
    color: "#00ff00",
    opacity: 0.8,
    outline: true,
    outlineWidth: 3,
    outlineColor: "#ffffff",
    clampToGround: true,
    label: {
      text: "我是相交部分",
      font_size: 18,
      color: "#000000"
    }
  }
  intersectGraphic = graphicLayer.addGraphic(intersectGraphic)
}
 
// 清除
export function clear() {
  if (intersectGraphic) {
    graphicLayer.removeGraphic(intersectGraphic, true)
    intersectGraphic = null
  }
}