园林绿化
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.2 KiB

  1. // import * as mars3d from "mars3d"
  2. // const Cesium = mars3d.Cesium
  3. (function () {
  4. //采用高德地图定位的算法,参考帮助文档:https://lbs.amap.com/api/javascript-api/guide/services/geolocation
  5. document.write('<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=616e615727a1134331ff9459856653f1"></script>');
  6. class Geolocation extends mars3d.control.ToolButton {
  7. /**
  8. * 创建_container控件容器对象的方法
  9. * 只会调用一次
  10. * @return {void}
  11. * @private
  12. */
  13. _mountedHook() {
  14. //缩小
  15. this._container = mars3d.DomUtil.create("div", "cesium-button cesium-toolbar-button tracking-deactivated");
  16. this._container.setAttribute("title", "查看GPS位置");
  17. this._container.addEventListener("click", (e) => {
  18. // one time tracking
  19. this.startTracking();
  20. });
  21. }
  22. stopTracking() {
  23. mars3d.DomUtil.removeClass(this._container, "tracking-activated");
  24. mars3d.DomUtil.addClass(this._container, "tracking-deactivated");
  25. this.clearLocationPoint();
  26. }
  27. startTracking() {
  28. AMap.plugin("AMap.Geolocation", ()=> {
  29. mars3d.DomUtil.removeClass(this._container, "tracking-deactivated");
  30. mars3d.DomUtil.addClass(this._container, "tracking-activated");
  31. if (!this.geolocation) {
  32. this.geolocation = new AMap.Geolocation({
  33. enableHighAccuracy: true, // 是否使用高精度定位,默认:true
  34. timeout: 10000, // 设置定位超时时间,默认:无穷大
  35. convert: true, //自动偏移坐标,偏移后的坐标为高德坐标,默认:true
  36. });
  37. }
  38. var that = this;
  39. this.geolocation.getCurrentPosition();
  40. function onComplete(data) {
  41. // data是具体的定位信息
  42. var wgsPoint = mars3d.PointTrans.gcj2wgs([data.position.lng, data.position.lat]);
  43. that.flyToLocation({ lng: wgsPoint[0], lat: wgsPoint[1] });
  44. }
  45. function onError(data) {
  46. // 定位出错,参考:https://lbs.amap.com/faq/js-api/map-js-api/position-related
  47. mars3d.Util.msg(data.message, "定位失败");
  48. }
  49. AMap.event.addListener(this.geolocation, "complete", onComplete);
  50. AMap.event.addListener(this.geolocation, "error", onError);
  51. });
  52. }
  53. flyToLocation(position) {
  54. mars3d.DomUtil.removeClass(this._container, "tracking-activated");
  55. mars3d.DomUtil.addClass(this._container, "tracking-deactivated");
  56. this._map.flyToPoint(position, {
  57. radius: 2000,
  58. complete: function () {},
  59. });
  60. this.clearLocationPoint();
  61. var graphic = new mars3d.graphic.DivLightPoint({
  62. position: position,
  63. style: {
  64. color: "#ffff00",
  65. clampToGround: true,
  66. },
  67. tooltip: "我的位置:" + position.lng + "," + position.lat,
  68. });
  69. this._map.graphicLayer.addGraphic(graphic);
  70. this.graphic = graphic;
  71. }
  72. clearLocationPoint() {
  73. if (!this.graphic) return;
  74. this.graphic.destroy();
  75. this.graphic = null;
  76. }
  77. }
  78. //对外接口
  79. mars3d.control.Geolocation = Geolocation;
  80. })();
  81. // export { Geolocation }