校园-江西科技师范大学-前端
shuishen
2023-12-14 e733164e1c779b3aef7245936b9daf7875bf791e
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
/*
 * @Author: shuishen 1109946754@qq.com
 * @Date: 2022-09-09 17:54:08
 * @LastEditors: shuishen 1109946754@qq.com
 * @LastEditTime: 2023-12-07 18:11:59
 * @FilePath: \sd-jg-school-web-ksd\src\store\modules\mobilePosition.js
 * @Description:
 *
 * Copyright (c) 2022 by shuishen 1109946754@qq.com, All Rights Reserved.
 */
 
let extent = [
  {
    key: 1,
    xMin: 112.51303616638316,
    yMin: -0.103420786429659,
    xMax: 112.67880038471827,
    yMax: 0.000004508906477476281,
 
    x: (x, y) => {
      return -250.6503 + 2.0389 * x + 4.4338 * y;
    },
 
    x: (x, y) => {
      return 228.1454 - 2.321 * x + 1.4165 * y;
    },
  },
 
  {
    key: 2,
    xMin: 112.51302551881798,
    yMin: -0.16235359398493399,
    xMax: 112.72742295590702,
    yMax: 0.000004509195932360471,
 
    x: (x, y) => {
      return -802.9905 + 6.1504 * x + 7.0694 * y;
    },
 
    x: (x, y) => {
      return 295.4321 - 3.4878 * x + 3.775 * y;
    },
  },
];
 
import { GCJ02ToWGS84 } from "@/utils/CoordTransform";
 
const mobilePosition = {
  state: {
    getSPosition: null,
    currentCampus: 1,
  },
  mutations: {
    GET_NOWPOSITION(state, val) {
      var outData =
        val ||
        function (res) {
          console.log(res);
        };
 
      let cur = extent.find((item) => item.key == state.currentCampus);
 
      global.Geolocation.getLocation(
        function success(result) {
          let positionWgs = GCJ02ToWGS84(result.lng, result.lat);
 
          let position = {
            lng: cur.x(positionWgs[0], positionWgs[1]),
            lat: cur.y(positionWgs[0], positionWgs[1]),
          };
 
          if (
            position.lng > cur.xMin &&
            position.lng < cur.xMax &&
            position.lat > cur.yMin &&
            position.lat < cur.yMax
          ) {
            outData([position.lng, position.lat]);
          } else {
            Message({
              message: "您当前位置超出学校范围",
              type: "warning",
              duration: 2000,
            });
 
            outData(["失败"]);
          }
        },
        function error(result) {
          if (result.code == 1) {
            confirm("您拒绝了定位功能");
          } else if (result.code == 3 || result.code == 5) {
            confirm("请求超时");
          }
        },
        {
          timeout: 8000, // 默认值为10s;
          failTipFlag: true,
        }
      );
    },
 
    CLOSE_NOWPOSITION(state) {
      // if (state.getSPosition && navigator.geolocation) {
      //   navigator.geolocation.clearWatch(state.getSPosition);
      //   state.getSPosition = null;
      // }
    },
 
    SET_CURRENTCAMPUS(state, val) {
      state.currentCampus = val;
    },
  },
  actions: {},
};
 
export default mobilePosition;