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
| import React, { useState } from "react"; import Taro, { useReady, useDidShow, useDidHide } from "@tarojs/taro"; import { View, Map } from "@tarojs/components";
const hasOwnProperty = (obj, key) => { return Object.prototype.hasOwnProperty.call(obj, key); };
const HandleLocation = () => { const [systemSetting, setSystemSetting] = useState({}); const [location, setLocation] = useState({ longitude: "", latitude: "" }); const [locationList, setLocationList] = useState([]);
const handleGetLocation = () => { if (Taro.canIUse("startLocationUpdateBackground")) {
Taro.startLocationUpdateBackground({ success() { Taro.onLocationChange((data) => { setLocationList((pre) => [...pre, data]);
setLocation({ longitude: data.longitude, latitude: data.latitude });
}); }, fail(err) { console.log(err); Taro.showToast({ icon: "error", title: "定位失败", });
Taro.openSetting(); }, }); } else { Taro.showToast({ icon: "error", title: "您的设备暂不支持定位", }); } };
const checkMobileLocationAuth = () => { return new Promise((resolve, reject) => { Taro.getSystemInfoAsync({ success(data) { setSystemSetting(data);
if ( data && hasOwnProperty(data, "locationEnabled") && !data.locationEnabled ) { Taro.showModal({ title: "提示", content: "请打开手机设置-位置信息(GPS)开关", confirmText: "确定", showCancel: false, });
reject(); }
if ( data && hasOwnProperty(data, "locationAuthorized") && !data.locationAuthorized ) { Taro.showModal({ title: "提示", content: "请打开手机设置-应用设置-应用管理-微信-权限管理-定位权限开关", confirmText: "确定", showCancel: false, success() { }, });
reject(); }
resolve(); }, fail() { reject(); }, }); }); };
const checkMiniAppLocationAuth = () => { return new Promise((resolve, reject) => { if (!Taro.canIUse("getSetting")) return reject();
Taro.getSetting({ success: function (res) { const authSetting = res.authSetting;
if ( authSetting && hasOwnProperty(authSetting, "scope.userLocation") && hasOwnProperty(authSetting, "scope.userLocationBackground") && authSetting["scope.userLocation"] && authSetting["scope.userLocationBackground"] ) { resolve(); } else { if (Taro.canIUse("openSetting")) { Taro.showModal({ title: "提示", content: "请在点击确定后,在弹出的选项中务必勾选“使用小程序期间和离开小程序之后”选项", confirmText: "确定", showCancel: false, success() { Taro.openSetting(); }, }); } else { Taro.showModal({ title: "提示", content: "请点击右上角“...”更多-设置-位置信息,在弹出的选项中务必勾选“使用小程序期间和离开小程序之后”选项", confirmText: "确定", showCancel: false, }); }
reject(); } }, }); }); };
useReady(() => { console.log("useReady==>"); });
useDidShow(() => { console.log("useDidShow==>"); if (Taro.canIUse("stopLocationUpdate")) { Taro.stopLocationUpdate({ complete() { checkMobileLocationAuth() .then(checkMiniAppLocationAuth) .then(() => { handleGetLocation(); }) .catch(() => { console.log("err==>"); }); }, }); } });
useDidHide(() => { console.log("useDidHide==>"); });
return ( <View> <Map style="width: 100%; height: 200px;" scale={16} longitude={location.longitude} latitude={location.latitude} /> </View> ); };
|