1
mayisheng
2022-08-15 81f54040c2cb65537c6c6e1db8358a39a57dea0d
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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;