Yansen 1 vuosi sitten
vanhempi
commit
7c96ade2aa
5 muutettua tiedostoa jossa 46 lisäystä ja 0 poistoa
  1. 1
    0
      package.json
  2. 1
    0
      src/components/map/index.jsx
  3. 5
    0
      src/index.html
  4. 34
    0
      src/utils/map.js
  5. 5
    0
      yarn.lock

+ 1
- 0
package.json Näytä tiedosto

@@ -52,6 +52,7 @@
52 52
     "@zjxpcyc/react-tiny-store": "^2.0.1",
53 53
     "echarts": "^5.4.1",
54 54
     "echarts-for-weixin": "^1.0.2",
55
+    "gcoord": "^1.0.6",
55 56
     "md5": "^2.3.0",
56 57
     "postcss-modules": "^6.0.0",
57 58
     "react": "^18.0.0",

+ 1
- 0
src/components/map/index.jsx Näytä tiedosto

@@ -3,6 +3,7 @@ import Taro from "@tarojs/taro";
3 3
 import { View, Map } from "@tarojs/components";
4 4
 import { Icon, Loading } from "@antmjs/vantui";
5 5
 import { getLocation, h5GetLocation } from "@/utils/authorize";
6
+// import { getLocation } from "@/utils/map";
6 7
 import iconPath from "@/assets/icons/marker.png";
7 8
 import style from "./style.module.less";
8 9
 

+ 5
- 0
src/index.html Näytä tiedosto

@@ -16,6 +16,11 @@
16 16
       charset="utf-8"
17 17
       src="https://map.qq.com/api/gljs?v=1.exp&key=HTPBZ-HHJA7-XD2XD-PRS37-H3HVJ-U5BAA&libraries=service"
18 18
     ></script>
19
+    <!-- <script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script>
20
+    <script>
21
+      // VConsole 默认会挂载到 `window.VConsole` 上
22
+      var vConsole = new window.VConsole();
23
+    </script> -->
19 24
 
20 25
     <title>civilized-miniapp</title>
21 26
     <script>

+ 34
- 0
src/utils/map.js Näytä tiedosto

@@ -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
+}

+ 5
- 0
yarn.lock Näytä tiedosto

@@ -6978,6 +6978,11 @@ gaze@^0.5.1:
6978 6978
   dependencies:
6979 6979
     globule "~0.1.0"
6980 6980
 
6981
+gcoord@^1.0.6:
6982
+  version "1.0.6"
6983
+  resolved "https://registry.npmmirror.com/gcoord/-/gcoord-1.0.6.tgz#bfb845b2014e9ef95012787517c75edaf04716ec"
6984
+  integrity sha512-Z+uyWrD2BTWmJOUvAdEAZQdQIVA5MFwUu7P5BfBdQEpLiVAsNw9mZF3/QbDJ4ZbfcJEnFKgHXOgUFXo5r/aVPg==
6985
+
6981 6986
 generic-names@^4.0.0:
6982 6987
   version "4.0.0"
6983 6988
   resolved "https://registry.npmmirror.com/generic-names/-/generic-names-4.0.0.tgz"