liuyg
2022-02-21 e3dcaeb0180a4e37347ac9ceb44c8a59ae7af37f
leafletMapOur/grabOrdersMap/js/getPosition.js
@@ -13,7 +13,8 @@
      this.getOnceSPosition = null;
      this.state = false;
      this.isContinued = false;
      this.isContinuedTime = null;
      this.isContinuedTimeInt = null;
      this.isContinuedTimeOut = null;
      return this;
   }
   //开始获取
@@ -27,7 +28,7 @@
      console.log("开始持续定位")
      let that = this;
      this.isContinued = true;
      if (this.isContinuedTime) {
      if (this.isContinuedTimeInt) {
         console.log("重复开始持续定位")
         return;
      } else {
@@ -36,7 +37,7 @@
               console.log(res.intlat)
            }
         })
         this.isContinuedTime = setInterval(() => {
         this.isContinuedTimeInt = setInterval(() => {
            that.once(function(res) {
               if (res.state == false) {
                  console.log(res.intlat)
@@ -45,24 +46,69 @@
         }, 5000)
      }
   }
   continuedTime(val) {
      //开始持续获取定位
      let that = this;
      if (val != 1) {
         // console.log(that.id,"开始持续定位")
         this.isContinued = true;
         that.once(function(res) {
            if (res.state == false) {
               // console.log(res.intlat)
               that.outData();
            }
         })
         if (this.isContinuedTimeOut) {
            // console.log("重复开始持续定位")
            return;
         }
      }
      this.isContinuedTimeOut = setTimeout(() => {
         that.once(function(res) {
            if (res.state == false) {
               // console.log(res.intlat)
               that.outData();
               if (res.ok) {
                  //返回成功
                  that.continuedTime(1);
               } else {
                  //返回失败
                  that.continuedTime(1);
               }
            }
         })
      }, 3000)
   }
   overContinued() {
      console.log("结束持续定位")
      // console.log("结束持续定位")
      let that = this;
      if (this.isContinued) {
         this.isContinued = false;
         clearInterval(that.isContinuedTime);
         this.isContinuedTime = null;
         if (this.isContinuedTimeInt) {
            clearInterval(that.isContinuedTimeInt);
            this.isContinuedTimeInt = null;
         }
         if (this.isContinuedTimeOut) {
            clearTimeout(that.isContinuedTimeOut);
            this.isContinuedTimeOut = null;
         }
      }
      this.isContinued = false;
      that.outData('out');
   }
   once(fn) {
      console.log("开始获取一次定位");
      // console.log("开始获取一次定位");
      this.state = true;
      let that = this,fns = '',
      let ok = true;
      let that = this,
         fns = fn || '',
         outit = function() {
            if (fns) {
               fns({
                  state: that.state,
                  intlat: that.onceNowPosition
                  intlat: that.onceNowPosition,
                  ok: ok
               })
            } else {
               that.outData();
@@ -71,12 +117,13 @@
      outit();
      let success = function(position) {
         // console.log(position);
         var lat = position.coords.latitude;
         var lon = position.coords.longitude;
         var lat = position.coords.longitude;//经度
         var lon = position.coords.latitude;//纬度
         // console.log("成功返回经纬度信息");
         // console.log(lon, lat, "成功");
         that.onceNowPosition = [lat, lon];
         that.state = false;
         ok = true;
         outit();
      };
      let error = function(error) {
@@ -98,6 +145,7 @@
         }
         that.onceNowPosition = msg;
         that.state = false;
         ok = false;
         outit();
      };
      let options = {
@@ -111,53 +159,22 @@
         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() {
   outData(val) {
      let d = {
         state: this.state,
         intlat: this.onceNowPosition
      }
      if (val) {
         d[val] = val
      }
      this.fn(d)
   }
}
// export default $getPositions;