+
liuyg
2022-02-08 a0c49f120832ec784bef5361eb394dea826fb360
+
2 files modified
3 files added
738 ■■■■■ changed files
src/components/mobileWindow/index.vue 4 ●●●● patch | view | raw | blame | history
src/store/modules/mobilePosition copy.js 163 ●●●●● patch | view | raw | blame | history
src/store/modules/mobilePosition.js 263 ●●●●● patch | view | raw | blame | history
src/store/modules/mobilePositionLID.js 57 ●●●●● patch | view | raw | blame | history
src/store/modules/mobilePositionLID1.js 251 ●●●●● patch | view | raw | blame | history
src/components/mobileWindow/index.vue
@@ -267,6 +267,10 @@
        // console.log(this.query.query);
        // that.$store.commit("set_endPosition", this.query.query.lntLat);
        let doit = (now) => {
          if (now[0] == "失败") {
            that.quzheD = false;
            return;
          }
          let ints = {
            start: [+now[0], +now[1]],
            end: [+query.lntLat[0], +query.lntLat[1], 360],
src/store/modules/mobilePosition copy.js
New file
@@ -0,0 +1,163 @@
import { Message } from "element-ui";
// import BMap from "./mobilePositionLID";
import RemoGeoLocation from "./mobilePositionLID1";
const mobilePosition = {
  state: { getSPosition: null },
  mutations: {
    GET_NOWPOSITION(state, val) {
      let outData =
        val ||
        function (res) {
          console.log(res);
        };
      let startTime = new Date().getTime(),
        endTime;
      console.log(startTime, "startTime");
      let times = (dateBegin) => {
        var dateDiff = new Date().getTime() - dateBegin; //时间差的毫秒数
        var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数
        var leave1 = dateDiff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
        var hours = Math.floor(leave1 / (3600 * 1000)); //计算出小时数
        //计算相差分钟数
        var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
        var minutes = Math.floor(leave2 / (60 * 1000)); //计算相差分钟数
        //计算相差秒数
        var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
        var seconds = Math.round(leave3 / 1000);
        var leave4 = leave3 % (60 * 1000); //计算分钟数后剩余的毫秒数
        var minseconds = Math.round(leave4 / 1000);
        var timeFn =
          "耗时:" +
          dayDiff +
          "天 " +
          hours +
          "小时 " +
          minutes +
          " 分钟" +
          seconds +
          " 秒" +
          minseconds +
          "毫秒";
        console.log(timeFn);
        return timeFn;
      };
      if (navigator.geolocation) {
        let success = function (position) {
          console.log(position);
          times(startTime);
          var lat = position.coords.latitude;
          var lon = position.coords.longitude;
          console.log("成功返回经纬度信息");
          Message({
            showClose: true,
            message:
              "成功返回经纬度信息" +
              lon +
              "_" +
              lat +
              "--反应时间:" +
              times(startTime),
            type: "success",
            duration: 0,
          });
          console.log(lon, lat, "成功");
          outData([lon, lat]);
        };
        let error = function (error) {
          // console.log(error, "失败");
          outData(["失败", "失败"]);
          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;
          }
          Message({
            showClose: true,
            message:
              "定位获取失败-msg:" +
              msg +
              "------" +
              error.message +
              "--反应时间:" +
              times(startTime),
            type: "error",
            duration: 0,
          });
        };
        let options = {
          enableHighAccuracy: true,
          // timeout: 5000,
          maximumAge: 0,
        };
        // console.log(navigator);
        // console.log(navigator.platform);
        Message({
          showClose: true,
          message: navigator.platform,
          type: "error",
          duration: 0,
        });
        // console.log(CLLocationManager);
        // return;
        if (navigator.platform === "iPhone") {
          //iphone请求权限
          // let remoGeo = new RemoGeoLocation();
          // navigator.geolocation.getCurrentPosition = function () {
          //   return remoGeo.getCurrentPosition.apply(remoGeo, arguments);
          // };
          // navigator.geolocation.watchPosition = function () {
          //   return remoGeo.watchPosition.apply(remoGeo, arguments);
          // };
          // Message({
          //   showClose: true,
          //   message: "ipone",
          //   type: "error",
          //   duration: 0,
          // });
          // console.log(CLLocationManager);
          // Message({
          //   showClose: true,
          //   message: CLLocationManager,
          //   type: "error",
          //   duration: 0,
          // });
          // return;
        }
        state.getSPosition = navigator.geolocation.getCurrentPosition(
          success,
          error,
          options
        );
      } else {
        console.log("不支持navigator.geolocation");
        Message({
          showClose: true,
          message: "不支持定位",
          type: "warning",
          duration: 0,
        });
        return "不支持navigator.geolocation";
      }
    },
    CLOSE_NOWPOSITION(state) {
      // if (state.getSPosition && navigator.geolocation) {
      //   navigator.geolocation.clearWatch(state.getSPosition);
      //   state.getSPosition = null;
      // }
    },
  },
  actions: {},
};
export default mobilePosition;
src/store/modules/mobilePosition.js
@@ -1,3 +1,7 @@
import { Message } from "element-ui";
// import BMap from "./mobilePositionLID";
import RemoGeoLocation from "./mobilePositionLID1";
const mobilePosition = {
  state: { getSPosition: null },
  mutations: {
@@ -7,29 +11,258 @@
        function (res) {
          console.log(res);
        };
      let startTime = new Date().getTime(),
        endTime;
      console.log(startTime, "startTime");
      let times = (dateBegin) => {
        var dateDiff = new Date().getTime() - dateBegin; //时间差的毫秒数
        var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数
        var leave1 = dateDiff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
        var hours = Math.floor(leave1 / (3600 * 1000)); //计算出小时数
        //计算相差分钟数
        var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
        var minutes = Math.floor(leave2 / (60 * 1000)); //计算相差分钟数
        //计算相差秒数
        var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
        var seconds = Math.round(leave3 / 1000);
        var leave4 = leave3 % (60 * 1000); //计算分钟数后剩余的毫秒数
        var minseconds = Math.round(leave4 / 1000);
        var timeFn =
          "耗时:" +
          dayDiff +
          "天 " +
          hours +
          "小时 " +
          minutes +
          " 分钟" +
          seconds +
          " 秒" +
          minseconds +
          "毫秒";
        console.log(timeFn);
        return timeFn;
      };
      if (navigator.geolocation) {
        console.log("支持navigator.geolocation-111111111111111");
        try {
          state.getSPosition = navigator.geolocation.getCurrentPosition(
            function (position) {
              console.log(position);
              var lat = position.coords.latitude;
              var lon = position.coords.longitude;
              console.log("成功返回经纬度信息");
              console.log(lon, lat);
              outData([lon, lat]);
            }
          );
        } catch {
          console.log("支持navigator.geolocation-需要HTTPS");
        let success = function (position) {
          console.log(position);
          times(startTime);
          var lat = position.coords.latitude;
          var lon = position.coords.longitude;
          console.log("成功返回经纬度信息");
          Message({
            showClose: true,
            message:
              "成功返回经纬度信息(1)" +
              lon +
              "_" +
              lat +
              "--反应时间:" +
              times(startTime),
            type: "success",
            duration: 0,
          });
          console.log(lon, lat, "成功");
          outData([lon, lat]);
        };
        let error = function (error) {
          // console.log(error, "失败");
          outData(["失败", "失败"]);
          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;
          }
          // Message({
          //   showClose: true,
          //   message:
          //     "定位获取失败-msg:" +
          //     msg +
          //     "------" +
          //     error.message +
          //     "--反应时间:" +
          //     times(startTime),
          //   type: "error",
          //   duration: 0,
          // });
          Message({
            showClose: true,
            message: "未开启位置权限",
            type: "error",
            duration: 2000,
          });
        };
        let options = {
          enableHighAccuracy: true,
          // timeout: 5000,
          timeout: Infinity,
          maximumAge: 0,
        };
        // console.log(navigator);
        // console.log(navigator.platform);
        // Message({
        //   showClose: true,
        //   message: navigator.platform,
        //   type: "error",
        //   duration: 0,
        // });
        // console.log(CLLocationManager);
        // return;
        if (navigator.platform === "iPhone") {
          //iphone请求权限
          // let remoGeo = new RemoGeoLocation();
          // navigator.geolocation.getCurrentPosition = function () {
          //   return remoGeo.getCurrentPosition.apply(remoGeo, arguments);
          // };
          // navigator.geolocation.watchPosition = function () {
          //   return remoGeo.watchPosition.apply(remoGeo, arguments);
          // };
          // Message({
          //   showClose: true,
          //   message: "ipone",
          //   type: "error",
          //   duration: 0,
          // });
          // console.log(CLLocationManager);
          // Message({
          //   showClose: true,
          //   message: CLLocationManager,
          //   type: "error",
          //   duration: 0,
          // });
          // return;
        }
        // return
        // console.log(BMap);
        //百度定位
        //       <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=zTNaMXHcfGsG0nFvwd6G72aMOo98uzH2">
        // </script>
        // var geolocation = new BMap.Geolocation();
        // geolocation.getCurrentPosition(function (r) {
        //   if (this.getStatus() == BMAP_STATUS_SUCCESS) {
        //     // var mk = new BMap.Marker(r.point);
        //     // map.addOverlay(mk);
        //     // map.panTo(r.point);
        //     console.log("您的位置:" + r.point.lng + "," + r.point.lat);
        //     outData([r.point.lng, r.point.lat]);
        //     Message({
        //       showClose: true,
        //       message:
        //         "成功返回经纬度信息" +
        //         r.point.lng +
        //         "_" +
        //         r.point.lat +
        //         "--反应时间:" +
        //         times(startTime),
        //       type: "success",
        //       duration: 0,
        //     });
        //   } else {
        //     console.log("failed" + this.getStatus());
        //     Message({
        //       showClose: true,
        //       message:
        //         "定位获取失败-msg:" +
        //         "failed" +
        //         this.getStatus() +
        //         "------" +
        //         "--反应时间:" +
        //         times(startTime),
        //       type: "error",
        //       duration: 0,
        //     });
        //   }
        // });
        // return;
        //高德定位
        // AMap.plugin("AMap.Geolocation", function () {
        //   var geolocation = new AMap.Geolocation({
        //     // 是否使用高精度定位,默认:true
        //     enableHighAccuracy: true,
        //     // 设置定位超时时间,默认:无穷大
        //     timeout: 10000,
        //     // 定位按钮的停靠位置的偏移量,默认:Pixel(10, 20)
        //     // buttonOffset: new AMap.Pixel(10, 20),
        //     //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
        //     // zoomToAccuracy: true,
        //     //  定位按钮的排放位置,  RB表示右下
        //     // buttonPosition: "RB",
        //   });
        //   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("成功返回经纬度信息");
        //     Message({
        //       showClose: true,
        //       message:
        //         "成功返回经纬度信息" +
        //         lon +
        //         "_" +
        //         lat +
        //         "--反应时间:" +
        //         times(startTime),
        //       type: "success",
        //       duration: 0,
        //     });
        //     console.log(lon, lat, "成功");
        //     outData([lon, lat]);
        //   }
        //   function onError(data) {
        //     // 定位出错
        //     console.log(data);
        //     Message({
        //       showClose: true,
        //       message:
        //         "定位获取失败-msg:" +
        //         data.message +
        //         "------" +
        //         "--反应时间:" +
        //         times(startTime),
        //       type: "error",
        //       duration: 0,
        //     });
        //   }
        // });
        // return;
        state.getSPosition = navigator.geolocation.getCurrentPosition(
          success,
          error,
          options
        );
      } else {
        console.log("不支持navigator.geolocation");
        Message({
          showClose: true,
          message: "不支持定位",
          type: "warning",
          duration: 0,
        });
        return "不支持navigator.geolocation";
      }
    },
    CLOSE_NOWPOSITION(state) {
      state.getSPosition = null;
      // if (state.getSPosition && navigator.geolocation) {
      //   navigator.geolocation.clearWatch(state.getSPosition);
      //   state.getSPosition = null;
      // }
    },
  },
  actions: {},
src/store/modules/mobilePositionLID.js
New file
@@ -0,0 +1,57 @@
(function () {
  window.BMap_loadScriptTime = new Date().getTime();
  document.write(
    '<script type="text/javascript" src="http://api.map.baidu.com/getscript?v=2.0&ak=6yAoynmTPNlTBa8z1X4LfwGE&services=&t=20220113143013"></script>'
  );
  // })();
  // (function () {
  //闭包
  function load_script(xyUrl, callback) {
    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = xyUrl;
    //借鉴了jQuery的script跨域方法
    script.onload = script.onreadystatechange = function () {
      if (
        !this.readyState ||
        this.readyState === "loaded" ||
        this.readyState === "complete"
      ) {
        callback && callback();
        // Handle memory leak in IE
        script.onload = script.onreadystatechange = null;
        if (head && script.parentNode) {
          head.removeChild(script);
        }
      }
    };
    // Use insertBefore instead of appendChild  to circumvent an IE6 bug.
    head.insertBefore(script, head.firstChild);
  }
  function translate(point, type, callback) {
    var callbackName = "cbk_" + Math.round(Math.random() * 10000); //随机函数名
    var xyUrl =
      "http://api.map.baidu.com/ag/coord/convert?from=" +
      type +
      "&to=4&x=" +
      point.lng +
      "&y=" +
      point.lat +
      "&callback=BMap.Convertor." +
      callbackName;
    //动态创建script标签
    load_script(xyUrl);
    BMap.Convertor[callbackName] = function (xyResult) {
      delete BMap.Convertor[callbackName]; //调用完需要删除改函数
      var point = new BMap.Point(xyResult.x, xyResult.y);
      callback && callback(point);
    };
  }
  window.BMap = window.BMap || {};
  BMap.Convertor = {};
  BMap.Convertor.translate = translate;
})();
export default BMap;
src/store/modules/mobilePositionLID1.js
New file
@@ -0,0 +1,251 @@
function RemoGeoLocation() {
  this._remoteSvrUrl = "https://webapi.amap.com/html/geolocate.html";
  this._callbackList = [];
  this._seqBase = 1;
  this._frameReady = 0;
  this._watchIdMap = {};
}
RemoGeoLocation.prototype = {
  _getSeq: function () {
    return this._seqBase++;
  },
  _onRrameReady: function (callback) {
    if (this._frameReady === 0) {
      if (!this._frameReadyList) {
        this._frameReadyList = [];
      }
      this._frameReadyList.push(callback);
      this._prepareIframe();
      return;
    }
    callback.call(this);
  },
  _prepareIframe: function () {
    if (this._iframeWin) {
      return;
    }
    var ifrm = document.createElement("iframe");
    ifrm.src =
      this._remoteSvrUrl + (this._remoteSvrUrl.indexOf("?") > 0 ? "&" : "?");
    ifrm.width = "0px";
    ifrm.height = "0px";
    ifrm.style.position = "absolute";
    ifrm.style.display = "none";
    var self = this;
    var timeoutId = setTimeout(function () {
      self._frameReady = false;
      self._callbackFrameReadyList();
    }, 5000);
    ifrm.onload = function () {
      clearTimeout(timeoutId);
      self._frameReady = true;
      self._callbackFrameReadyList();
      ifrm.onload = null;
    };
    document.body.appendChild(ifrm);
    this._iframeWin = ifrm.contentWindow;
    window.addEventListener(
      "message",
      function (e) {
        if (self._remoteSvrUrl.indexOf(e["origin"]) !== 0) {
          return;
        }
        self._handleRemoteMsg(e["data"]);
      },
      false
    );
  },
  _callbackFrameReadyList: function () {
    if (this._frameReadyList) {
      var list = this._frameReadyList;
      this._frameReadyList = null;
      for (var i = 0, len = list.length; i < len; i++) {
        list[i].call(this, this._frameReady);
      }
    }
  },
  _pickCallback: function (seqNum, keepInList) {
    var callbackList = this._callbackList;
    for (var i = 0, len = callbackList.length; i < len; i++) {
      var cbkInfo = callbackList[i];
      if (seqNum === cbkInfo.seq) {
        if (!keepInList) {
          callbackList.splice(i, 1);
        }
        return cbkInfo;
      }
    }
  },
  _handleRemoteMsg: function (msg) {
    var seqNum = msg["seq"];
    var cbkInfo = this._pickCallback(seqNum, !!msg["notify"]);
    if (cbkInfo) {
      cbkInfo.cbk.call(null, msg["error"], msg["result"]);
    } else {
      console.warn("Receive remote msg: ", msg);
    }
  },
  _postMessage: function (cmd, args, callback, seq) {
    this._prepareIframe();
    var msg = {
      cmd: cmd,
      args: args,
      seq: seq || this._getSeq(),
    };
    this._callbackList.push({
      cbk: callback,
      seq: msg["seq"],
    });
    this._onRrameReady(function () {
      if (this._frameReady === true) {
        try {
          this._iframeWin.postMessage(msg, "*");
        } catch (e) {
          this._pickCallback(msg["seq"]);
          callback(e);
        }
      } else {
        this._pickCallback(msg["seq"]);
        callback({
          message: "iFrame load failed!",
        });
      }
    });
  },
  getCurrentPosition: function (succHandler, errHandler, options) {
    this._postMessage("getCurrentPosition", [options], function (err, result) {
      if (err) {
        if (errHandler) {
          errHandler(err);
        }
        return;
      }
      if (succHandler) {
        succHandler(result);
      }
    });
  },
  watchPosition: function (succHandler, errHandler, options) {
    var watchKey = "wk" + this._getSeq(),
      cmdSeq = this._getSeq();
    this._watchIdMap[watchKey] = {
      stat: 0,
      seq: cmdSeq,
    };
    var self = this;
    this._postMessage(
      "watchPosition",
      [options],
      function (err, result) {
        var id = null;
        if (result) {
          id = result["id"];
        }
        var watchInfo = self._watchIdMap[watchKey];
        watchInfo.id = id;
        watchInfo.stat = 1;
        if (watchInfo.callbackList) {
          var list = watchInfo.callbackList;
          watchInfo.callbackList = null;
          for (var i = 0, len = list.length; i < len; i++) {
            list[i].call(self, id);
          }
        }
        if (err) {
          if (errHandler) {
            errHandler(err);
          }
          return;
        }
        if (succHandler) {
          succHandler(result["pos"]);
        }
      },
      cmdSeq
    );
    return watchKey;
  },
  clearWatch: function (watchKey, callback) {
    if (!this._watchIdMap[watchKey]) {
      callback("Id not exists: " + watchKey);
      return;
    }
    var watchInfo = this._watchIdMap[watchKey];
    var self = this;
    function clearId(id) {
      self._postMessage("clearWatch", [id], function (err, result) {
        if (!err) {
          self._pickCallback(watchInfo.seq);
          delete self._watchIdMap[watchKey];
        }
        if (callback) {
          callback(err, result);
        }
      });
    }
    if (watchInfo.stat < 1) {
      if (!watchInfo.callbackList) {
        watchInfo.callbackList = [];
      }
      watchInfo.callbackList.push(function (id) {
        clearId(id);
      });
    } else {
      clearId(watchInfo.id);
    }
  },
};
export default RemoGeoLocation;