liuyg
2022-02-19 93dade2fc9c58d69b2fe2361675b9d9647e5c130
leafletMapOur/grabOrdersMap/js/getPosition.js
@@ -0,0 +1,163 @@
class $getPositions {
   constructor(arg) {
      this.id = arg.id || 0; //任务id
      this.fn = arg.fn ? function(data) {
         arg.fn(data);
      } : function(data) {
         console.log("未设置输出回调")
         return data;
      };
      this.ourPosition = []; //全部位置
      this.nowPosition = []; //当前位置
      this.onceNowPosition = []; //当前一次位置
      this.getOnceSPosition = null;
      this.state = false;
      this.isContinued = false;
      this.isContinuedTime = null;
      return this;
   }
   //开始获取
   init() {
      //判断权限
      return this;
   }
   continued() {
      //开始持续获取定位
      console.log("开始持续定位")
      let that = this;
      this.isContinued = true;
      if (this.isContinuedTime) {
         console.log("重复开始持续定位")
         return;
      } else {
         that.once(function(res) {
            if (res.state == false) {
               console.log(res.intlat)
            }
         })
         this.isContinuedTime = setInterval(() => {
            that.once(function(res) {
               if (res.state == false) {
                  console.log(res.intlat)
               }
            })
         }, 5000)
      }
   }
   overContinued() {
      console.log("结束持续定位")
      let that = this;
      if (this.isContinued) {
         this.isContinued = false;
         clearInterval(that.isContinuedTime);
         this.isContinuedTime = null;
      }
   }
   once(fn) {
      console.log("开始获取一次定位");
      this.state = true;
      let that = this,fns = '',
         outit = function() {
            if (fns) {
               fns({
                  state: that.state,
                  intlat: that.onceNowPosition
               })
            } else {
               that.outData();
            }
         };
      outit();
      let success = function(position) {
         // console.log(position);
         var lat = position.coords.latitude;
         var lon = position.coords.longitude;
         // console.log("成功返回经纬度信息");
         // console.log(lon, lat, "成功");
         that.onceNowPosition = [lat, lon];
         that.state = false;
         outit();
      };
      let error = function(error) {
         // console.log(error, "失败");
         let msg = "none";
         switch (error.code) {
            case error.PERMISSION_DENIED:
               msg = "User denied the request for Geolocation.";
               break;
            case error.POSITION_UNAVAILABLE:
               msg = "Location information is unavailable.";
               break;
            case error.TIMEOUT:
               msg = "The request to get user location timed out.";
               break;
            case error.UNKNOWN_ERROR:
               msg = "An unknown error occurred.";
               break;
         }
         that.onceNowPosition = msg;
         that.state = false;
         outit();
      };
      let options = {
         enableHighAccuracy: true,
         // timeout: 5000,
         timeout: Infinity,
         maximumAge: 0,
      };
      this.getOnceSPosition = navigator.geolocation.getCurrentPosition(
         success,
         error,
         options
      );
      //高德定位
      // console.log("开始获取一次定位");
      // this.state = true;
      // let that = this;
      // that.outData();
      // AMap.plugin("AMap.Geolocation", function() {
      //    var geolocation = new AMap.Geolocation({
      //       // 是否使用高精度定位,默认:true
      //       enableHighAccuracy: true,
      //       // 设置定位超时时间,默认:无穷大
      //       // timeout: 10000,
      //    });
      //    geolocation.getCurrentPosition();
      //    AMap.event.addListener(geolocation, "complete", onComplete);
      //    AMap.event.addListener(geolocation, "error", onError);
      //    function onComplete(data) {
      //       // data是具体的定位信息
      //       // console.log(data);
      //       var lat = data.position.lat;
      //       var lon = data.position.lng;
      //       // console.log("成功返回经纬度信息");
      //       // console.log(lon, lat, "成功");
      //       that.onceNowPosition = [lon, lat];
      //       this.state = false;
      //       that.outData();
      //    }
      //    function onError(data) {
      //       // 定位出错
      //       // console.log(data);
      //       that.onceNowPosition = data;
      //       this.state = false;
      //       that.outData();
      //    }
      // });
   }
   //结束获取
   outInit() {
   }
   //抛出数据
   outData() {
      let d = {
         state: this.state,
         intlat: this.onceNowPosition
      }
      this.fn(d)
   }
}