zengh
2022-05-09 52c39f5e2711e5535b689b66dfa5e100c7882b7f
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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.isContinuedTimeInt = null;
        this.isContinuedTimeOut = null;
        this.geolocation = null; //定位实例
        return this;
    }
    //开始获取
    init() {
        //判断权限
        return this;
 
    }
    continued() {
        //开始持续获取定位
        console.log("开始持续定位")
        let that = this;
        this.isContinued = true;
        if (this.isContinuedTimeInt) {
            console.log("重复开始持续定位")
            return;
        } else {
            that.once(function (res) {
                if (res.state == false) {
                    console.log(res.intlat)
                }
            })
            this.isContinuedTimeInt = setInterval(() => {
                that.once(function (res) {
                    if (res.state == false) {
                        console.log(res.intlat)
                    }
                })
            }, 5000)
        }
    }
    continuedTime(val) {
        //开始持续获取定位
        let that = this;
        that.geolocation = new qq.maps.Geolocation("T7RBZ-62U3X-RSQ4P-ZZVCB-WE7JT-HRBOG", "mapqq");
        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("结束持续定位")
        let that = this;
        if (this.isContinued) {
            this.isContinued = false;
            if (this.isContinuedTimeInt) {
                clearInterval(that.isContinuedTimeInt);
                this.isContinuedTimeInt = null;
            }
            if (this.isContinuedTimeOut) {
                clearTimeout(that.isContinuedTimeOut);
                this.isContinuedTimeOut = null;
            }
            if (this.geolocation) {
                this.geolocation = null;
            }
        }
        this.isContinued = false;
        that.outData('out');
    }
    once(fn) {
        // console.log("开始获取一次定位");
        this.state = true;
        let ok = true;
        let that = this,
            fns = fn || '',
            outit = function () {
                if (fns) {
                    fns({
                        state: that.state,
                        intlat: that.onceNowPosition,
                        ok: ok
                    })
                } else {
                    that.outData();
                }
            };
        outit();
        // uni定位
        console.log(uni.getLocation)
        //qq定位
        // var geolocation = new qq.maps.Geolocation("T7RBZ-62U3X-RSQ4P-ZZVCB-WE7JT-HRBOG", "mapqq");
        // console.log(that.geolocation)
        // var options = {
        //     // timeout: 1000
        // };
 
        // function showPosition(position) {
        //     console.log("position:", position)
        //     console.log("position:", JSON.stringify(position))
        //     let lat = position.lat; //
        //     let lng = position.lng; //火星坐标 //TODO 实现业务代码逻辑 
        //     that.onceNowPosition = [lng, lat];
        //     // console.log(that.onceNowPosition)
        //     that.state = false;
        //     ok = true;
        //     outit();
        // }
 
        // function showErr(data) {
        //     console.log(data)
        //     that.onceNowPosition = data;
        //     that.state = false;
        //     ok = true;
        //     outit();
        // };
        // that.geolocation.getLocation(showPosition, showErr, options);
        // 高德定位
        // console.log(AMap)
        // 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;
        //         that.onceNowPosition = [lat, lon];
        //         that.state = false;
        //         ok = true;
        //         outit();
        //     }
 
        //     function onError(data) {
        //         // 定位出错
        //         console.log(data);
        //         that.onceNowPosition = data.message;
        //         that.state = false;
        //         ok = false;
        //         outit();
        //     }
        // });
        // return
        // let success = function(position) {
        //     // console.log(position);
        //     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) {
        //     // 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;
        //     ok = false;
        //     outit();
        // };
        // let options = {
        //     enableHighAccuracy: true,
        //     // timeout: 5000,
        //     timeout: Infinity,
        //     maximumAge: 0,
        // };
        // this.getOnceSPosition = navigator.geolocation.getCurrentPosition(
        //     success,
        //     error,
        //     options
        // );
    }
    //结束获取
    outInit() {
 
    }
    //抛出数据
    outData(val) {
        let d = {
            state: this.state,
            intlat: this.onceNowPosition
        }
        if (val) {
            d[val] = val
        }
        this.fn(d)
    }
}
 
// export default $getPositions;