李志伟 3 jaren geleden
bovenliggende
commit
92d453abc6
1 gewijzigde bestanden met toevoegingen van 9 en 8 verwijderingen
  1. 9
    8
      src/pages/index/index.jsx

+ 9
- 8
src/pages/index/index.jsx Bestand weergeven

@@ -36,23 +36,24 @@ export default withLayout((props) => {
36 36
       Taro.redirectTo({ url: `/pages/index/index?tab=${val}` });
37 37
     }
38 38
   };
39
+  const getAmap = (loc) => {
40
+    setAmap({ params: 'location=' + loc, path: '/v3/geocode/regeo' }).then(res2 => {
41
+      //josn对象取值
42
+      setAddress(JSON.parse(res2)['regeocode'].formatted_address)
43
+    })
44
+  }
45
+
39 46
   useEffect(() => {
40 47
     if (!location) {
41 48
       Taro.getLocation({
42 49
         type: 'gcj02',
43 50
         success: function (res) {
44 51
           setLocation(res.longitude + ',' + res.latitude)
45
-          setAmap({ params: 'location=' + res.longitude + ',' + res.latitude, path: '/v3/geocode/regeo' }).then(res2 => {
46
-            //josn对象取值
47
-            setAddress(JSON.parse(res2)['regeocode'].formatted_address)
48
-          })
52
+          getAmap(res.longitude + ',' + res.latitude)
49 53
         }
50 54
       })
51 55
     } else {
52
-      setAmap({ params: 'location=' + location, path: '/v3/geocode/regeo' }).then(res2 => {
53
-        //josn对象取值
54
-        setAddress(JSON.parse(res2)['regeocode'].formatted_address)
55
-      })
56
+      getAmap(location)
56 57
     }
57 58
   }, [])
58 59
   return (