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
// 采用高德地图定位的算法
// 参考帮助文档:https://lbs.amap.com/api/javascript-api/guide/services/geolocation
 
class Geolocation extends mars3d.control.ToolButton {
  /**
   * 创建_container控件容器对象的方法,
   * 只会调用一次
   * @return {void}  无
   * @private
   */
  _mountedHook() {
    // 缩小
    this._container = mars3d.DomUtil.create("div", "cesium-button cesium-toolbar-button tracking-deactivated")
    this._container.setAttribute("title", "查看GPS位置")
 
    this._container.addEventListener("click", (e) => {
      // one time tracking
      this.startTracking()
    })
  }
 
  stopTracking() {
    mars3d.DomUtil.removeClass(this._container, "tracking-activated")
    mars3d.DomUtil.addClass(this._container, "tracking-deactivated")
 
    this.clearLocationPoint()
  }
 
  startTracking() {
    AMap.plugin("AMap.Geolocation", () => {
      mars3d.DomUtil.removeClass(this._container, "tracking-deactivated")
      mars3d.DomUtil.addClass(this._container, "tracking-activated")
 
      if (!this.geolocation) {
        this.geolocation = new AMap.Geolocation({
          enableHighAccuracy: true, // 是否使用高精度定位,默认:true
          timeout: 10000, // 设置定位超时时间,默认:无穷大
          convert: true // 自动偏移坐标,偏移后的坐标为高德坐标,默认:true
        })
      }
 
      this.geolocation.getCurrentPosition()
      AMap.event.addListener(this.geolocation, "complete", (data) => {
        // data是具体的定位信息
        const wgsPoint = mars3d.PointTrans.gcj2wgs([data.position.lng, data.position.lat])
        this.flyToLocation({ lng: wgsPoint[0], lat: wgsPoint[1] })
      })
      AMap.event.addListener(this.geolocation, "error", (data) => {
        // 定位出错,参考:https://lbs.amap.com/faq/js-api/map-js-api/position-related
        globalMsg("定位失败")
      })
    })
  }
 
  flyToLocation(position) {
    mars3d.DomUtil.removeClass(this._container, "tracking-activated")
    mars3d.DomUtil.addClass(this._container, "tracking-deactivated")
 
    this.clearLocationPoint()
    const graphic = new mars3d.graphic.DivLightPoint({
      position: position,
      style: {
        color: "#ffff00",
        clampToGround: true
      },
      tooltip: "我的位置:" + position.lng + "," + position.lat
    })
    this._map.graphicLayer.addGraphic(graphic)
 
    graphic.flyTo({
      radius: 2000,
      complete: function () {
        console.log("飞行操作完成")
      }
    })
 
    this.graphic = graphic
  }
 
  clearLocationPoint() {
    if (!this.graphic) {
      return
    }
    this.graphic.destroy()
    this.graphic = null
  }
}