|
@@ -1,4 +1,5 @@
|
1
|
1
|
import Taro from "@tarojs/taro";
|
|
2
|
+import gcoord from 'gcoord';
|
2
|
3
|
|
3
|
4
|
// 腾讯地图接口 - 个人开发者
|
4
|
5
|
const QMAP_WEBSERVICE_KEY = 'HTPBZ-HHJA7-XD2XD-PRS37-H3HVJ-U5BAA';
|
|
@@ -27,3 +28,36 @@ export function geocoder(location, getPoi = 0) {
|
27
|
28
|
});
|
28
|
29
|
})
|
29
|
30
|
}
|
|
31
|
+
|
|
32
|
+// 使用 HTML H5 API 获取当前定位信息
|
|
33
|
+// 返回 promise 对象
|
|
34
|
+// resolve 结果是 { lng, lat }
|
|
35
|
+export function getLocation() {
|
|
36
|
+ return new Promise((resolve, reject) => {
|
|
37
|
+ navigator.geolocation.getCurrentPosition(
|
|
38
|
+ (position) => {
|
|
39
|
+ console.log('----定位结果-->', JSON.stringify(position))
|
|
40
|
+ const { longitude, latitude } = position.coords;
|
|
41
|
+
|
|
42
|
+ const data = gcoord.transform(
|
|
43
|
+ [longitude, latitude],
|
|
44
|
+ gcoord.WGS84,
|
|
45
|
+ gcoord.GCJ02
|
|
46
|
+ );
|
|
47
|
+
|
|
48
|
+ const result = {
|
|
49
|
+ location: { lng: data[0], lat: data[1] }
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ resolve(result);
|
|
53
|
+ },
|
|
54
|
+ (err) => {
|
|
55
|
+ console.error('----定位失败-->', JSON.stringify(err))
|
|
56
|
+ reject(err);
|
|
57
|
+ },
|
|
58
|
+ {
|
|
59
|
+ enableHighAccuracy: true,
|
|
60
|
+ }
|
|
61
|
+ )
|
|
62
|
+ })
|
|
63
|
+}
|