zhitong.yu
7 天以前 378d781e6f35f89652aa36e079a8b7fc44cea77e
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
// import * as mars3d from "mars3d"
// const Cesium = mars3d.Cesium
 
(function () {
  //采用高德地图定位的算法,参考帮助文档:https://lbs.amap.com/api/javascript-api/guide/services/geolocation
  document.write('<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=616e615727a1134331ff9459856653f1"></script>');
 
  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
          });
        }
 
        var that = this;
        this.geolocation.getCurrentPosition();
 
        function onComplete(data) {
          // data是具体的定位信息
          var wgsPoint = mars3d.PointTrans.gcj2wgs([data.position.lng, data.position.lat]);
          that.flyToLocation({ lng: wgsPoint[0], lat: wgsPoint[1] });
        }
 
        function onError(data) {
          // 定位出错,参考:https://lbs.amap.com/faq/js-api/map-js-api/position-related
          mars3d.Util.msg(data.message, "定位失败");
        }
        AMap.event.addListener(this.geolocation, "complete", onComplete);
        AMap.event.addListener(this.geolocation, "error", onError);
      });
    }
    flyToLocation(position) {
      mars3d.DomUtil.removeClass(this._container, "tracking-activated");
      mars3d.DomUtil.addClass(this._container, "tracking-deactivated");
 
      this._map.flyToPoint(position, {
        radius: 2000,
        complete: function () {},
      });
 
      this.clearLocationPoint();
      var graphic = new mars3d.graphic.DivLightPoint({
        position: position,
        style: {
          color: "#ffff00",
          clampToGround: true,
        },
        tooltip: "我的位置:" + position.lng + "," + position.lat,
      });
      this._map.graphicLayer.addGraphic(graphic);
 
      this.graphic = graphic;
    }
    clearLocationPoint() {
      if (!this.graphic) return;
      this.graphic.destroy();
      this.graphic = null;
    }
  }
  //对外接口
  mars3d.control.Geolocation = Geolocation;
})();
 
// export { Geolocation }