洛阳学员端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
11 KiB

10 months ago
10 months ago
10 months ago
10 months ago
9 months ago
9 months ago
9 months ago
9 months ago
10 months ago
10 months ago
  1. <template>
  2. <view class="content">
  3. <view style="text-align: center;">------权限-------</view>
  4. <button type="primary" @click="isProviderEnabled">GPS是否开启(android)</button>
  5. <button type="primary" @click="gotoLocationSetting">跳转定位设置页面</button>
  6. <button type="primary" @click="judgePermission">获取权限状态(ios)</button>
  7. <button type="primary" @click="requestPermission">请求权限(ios)</button>
  8. <view style="text-align: center;">------andorid位置提供者-------</view>
  9. <button type="primary" @click="getProviders">获取可用位置提供者(android)</button>
  10. <button type="primary" @click="getAllProviders">获取所有位置提供者(android)</button>
  11. <button type="primary" @click="getBestProvider">获取最佳的位置提供者(android)</button>
  12. <view style="text-align: center;">------定位-------</view>
  13. <button type="primary" @click="getLastKnownLocation">获取最新位置(android)</button>
  14. <button type="primary" @click="requestLocation">单次定位(ios)</button>
  15. <button type="primary" @click="startUpdatingLocation">开始持续定位</button>
  16. <view class="json">{{startUpdatingLocationJson}}</view>
  17. <button type="primary" @click="stopUpdatingLocation">取消持续定位</button>
  18. <view style="text-align: center;">------正反地理编码-------</view>
  19. <button type="primary" @click="reverseGeocode">反地理编码</button>
  20. <view class="json">{{reverseGeocodeJson}}</view>
  21. <button type="primary" @click="reverseGeocodeV2">反地理编码V2(andorid),不受google服务影响</button>
  22. <view class="json">{{reverseGeocodeV2Json}}</view>
  23. <button type="primary" @click="geocode">地理编码</button>
  24. <view class="json">{{geocodeJson}}</view>
  25. <view style="text-align: center;">------以下方法andorid7.0以上才支持-------</view>
  26. <button type="primary" @click="registerGnssStatus">注册监听卫星信息(GNSS处理后信息)</button>
  27. <view class="json">{{registerGnssStatusJson}}</view>
  28. <button type="primary" @click="unregisterGnssStatus">取消注册监听卫星信息</button>
  29. <button type="primary" @click="registerGnssMeasurements">注册监听卫星信息(GNSS测量原始信息)</button>
  30. <view class="json">{{registerGnssMeasurementsJson}}</view>
  31. <button type="primary" @click="unregisterGnssMeasurements">取消注册监听卫星信息</button>
  32. </view>
  33. </template>
  34. <script>
  35. var KJGNSS = uni.requireNativePlugin("KJ-GNSS");
  36. export default {
  37. data() {
  38. return {
  39. startUpdatingLocationJson: "",
  40. reverseGeocodeJson: "",
  41. reverseGeocodeV2Json: "",
  42. geocodeJson: "",
  43. registerGnssStatusJson: "",
  44. registerGnssMeasurementsJson: ""
  45. }
  46. },
  47. onLoad() {
  48. if (plus.os.name == 'Android') {
  49. plus.android.requestPermissions(
  50. ['android.permission.ACCESS_FINE_LOCATION',
  51. 'android.permission.ACCESS_COARSE_LOCATION'
  52. ],
  53. function(resultObj) {
  54. var result = 0;
  55. for (var i = 0; i < resultObj.granted.length; i++) {
  56. var grantedPermission = resultObj.granted[i];
  57. console.log('已获取的权限:' + grantedPermission);
  58. result = 1
  59. }
  60. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  61. var deniedPresentPermission = resultObj.deniedPresent[i];
  62. console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  63. result = 0
  64. }
  65. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  66. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  67. console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  68. result = -1
  69. }
  70. },
  71. function(error) {
  72. console.log('申请权限错误:' + error.code + " = " + error.message);
  73. }
  74. );
  75. }
  76. },
  77. methods: {
  78. isProviderEnabled() {
  79. var dic = {
  80. "provider": "gps" //位置提供者
  81. }
  82. KJGNSS.isProviderEnabled(dic, (res) => {
  83. console.log("isProviderEnabled:" + JSON.stringify(res));
  84. });
  85. },
  86. gotoLocationSetting() {
  87. KJGNSS.gotoLocationSetting();
  88. },
  89. judgePermission() {
  90. KJGNSS.judgePermission((res) => {
  91. console.log("judgePermission:" + JSON.stringify(res));
  92. });
  93. },
  94. requestPermission() {
  95. KJGNSS.requestPermission((res) => {
  96. console.log("requestPermission:" + JSON.stringify(res));
  97. });
  98. },
  99. getProviders() {
  100. var dic = {
  101. "enabledOnly": true //表示仅获取可用的位置提供者
  102. }
  103. KJGNSS.getProviders(dic, (res) => {
  104. console.log("getProviders:" + JSON.stringify(res));
  105. });
  106. },
  107. getAllProviders() {
  108. KJGNSS.getAllProviders((res) => {
  109. console.log("getAllProviders:" + JSON.stringify(res));
  110. });
  111. },
  112. getBestProvider() {
  113. var dic = {
  114. "accuracy": 2, //表示纬度和经度的理想精度 1-精度低,误差大于500米 2-精度中等,误差在100米到500米之间 3-精度高,误差小于100米
  115. //"bearingAccuracy":2,//指示所需的轴承精度
  116. //"horizontalAccuracy":2,//指示所需的水平精度(经度和纬度)
  117. //"speedAccuracy":2,//指示所需的速度准确度
  118. //"verticalAccuracy":2,//指示所需的垂直精度(高度)
  119. "powerRequirement": 1, //对电源的需求 1-耗电低 2-耗电中等 3-耗电高
  120. "altitudeRequired": false, //指示提供者是否必须提供海拔信息
  121. "bearingRequired": false, //指示提供者是否必须提供方位信息
  122. "costAllowed": false, //指示提供者是否被允许产生货币成本
  123. "speedRequired": false, //指示提供者是否必须提供速度信
  124. "enabledOnly": true //表示仅获取可用的位置提供者
  125. }
  126. KJGNSS.getBestProvider(dic, (res) => {
  127. console.log("getBestProvider:" + JSON.stringify(res));
  128. });
  129. },
  130. getLastKnownLocation() {
  131. var dic = {
  132. "provider": "gps" //位置提供者
  133. }
  134. // KJGNSS.getLastKnownLocation(dic, (res) => {
  135. // console.log("getLastKnownLocation:" + JSON.stringify(res));
  136. // });
  137. let KJGNS = uni.requireNativePlugin("KJ-GNSS");
  138. console.log(KJGNS)
  139. },
  140. requestLocation() {
  141. var dic = {
  142. "desiredAccuracy": 1,
  143. "distanceFilter": 10,
  144. "allowsBackgroundLocationUpdates": true,
  145. "showsBackgroundLocationIndicator": true,
  146. "pausesLocationUpdatesAutomatically": false
  147. }
  148. KJGNSS.requestLocation(dic, (res) => {
  149. console.log("requestLocation:" + JSON.stringify(res));
  150. });
  151. },
  152. startUpdatingLocation() {
  153. var dic;
  154. if (plus.os.name == 'Android') {
  155. dic = {
  156. "providers": ["gps", "network", "passive"], //位置提供者数组,优先gps
  157. "minTime": 1000, //更新的最小时间
  158. "minDistance": 1 //更新的最小距离
  159. }
  160. } else {
  161. dic = {
  162. "isSingleLocation": false,
  163. "desiredAccuracy": "Best", //BestForNavigation(最精准) Best(最好的,米级) NearestTenMeters(十米) HundredMeters(百米) Kilometer(一公里) ThreeKilometers(三公里)
  164. "distanceFilter": 10, //刷新距离
  165. "allowsBackgroundLocationUpdates": false, //是否开启后台定位,为true 需要配置manifest.json->App常用其它设置->后台运行能力 location
  166. "showsBackgroundLocationIndicator": false, //是否显示后台定位提示(状态栏蓝色背景)
  167. "pausesLocationUpdatesAutomatically": false //定位是否会被系统自动暂停
  168. }
  169. }
  170. KJGNSS.startUpdatingLocation(dic, (res) => {
  171. console.log("startUpdatingLocation:" + JSON.stringify(res));
  172. this.startUpdatingLocationJson = JSON.stringify(res)
  173. if (plus.os.name == 'Android') {
  174. //返回参数说明参考:https://www.xfqiao.com/api/android-zh/android/location/Location.html
  175. /**
  176. * {"method":"onLocationChanged","result":{"longitude":113.281456,"time":1687341181822,"hasBearing":false,"accuracy":40,"elapsedRealtimeNanos":1239298029066278,"provider":"network",
  177. * "hasAccuracy":true,"bearing":0,"isFromMockProvider":false,"hasSpeed":false,"latitude":23.07654,"speed":0,"hasAltitude":false,"altitude":0}}
  178. * isFromMockProvider - 如果位置来自模拟提供者则返回true
  179. * */
  180. if (res.method == "onLocationChanged") {
  181. this.reverseGeocode(res.result.latitude, res.result.longitude)
  182. this.reverseGeocodeV2(res.result.latitude, res.result.longitude)
  183. }
  184. } else {
  185. /**
  186. * {"result":[{"course":-1,"speed":-1,"longitude":113.2814133388547,"horizontalAccuracy":43,
  187. * "timestamp":"2023-06-21 18:01:04","latitude":23.076741614788542,"courseAccuracy":-1,
  188. * "ellipsoidalAltitude":1.8153233528137207,"altitude":8.372057914733887,"speedAccuracy":-1}]}
  189. * course - 航向 位置的路线以正北度数为单位如果无效则为否定0.0-359.90为正北
  190. * horizontalAccuracy - 水平的准确度负数无效
  191. * ellipsoidalAltitude - WGS 84参考系下位置的椭球高度
  192. * courseAccuracy - 航向精度以度为单位如果无效则返回负数
  193. * altitude - 海拔高度 可以是正海平面以上或负海平面以下
  194. * speed - 速度 单位为m/s如果速度无效则为负数
  195. * speedAccuracy - 速度精度 单位为m/s如果无效则返回-1
  196. * */
  197. var arr = res.result;
  198. var dic = arr[arr.length - 1];
  199. this.reverseGeocode(dic.latitude, dic.longitude)
  200. }
  201. });
  202. },
  203. stopUpdatingLocation() {
  204. KJGNSS.stopUpdatingLocation();
  205. },
  206. reverseGeocode(latitude, longitude) {
  207. var dic = {
  208. "latitude": latitude,
  209. "longitude": longitude,
  210. "maxResults": 1 //最大返回的结果,andorid才有
  211. }
  212. KJGNSS.reverseGeocode(dic, (res) => {
  213. console.log("reverseGeocode:" + JSON.stringify(res));
  214. this.reverseGeocodeJson = JSON.stringify(res)
  215. var arr = res.result;
  216. var dic = arr[arr.length - 1];
  217. this.geocode(dic.adminArea + dic.locality + dic.subLocality + dic.fetFeatureName);
  218. });
  219. },
  220. reverseGeocodeV2(latitude, longitude) {
  221. var dic = {
  222. "latitude": latitude,
  223. "longitude": longitude,
  224. }
  225. KJGNSS.reverseGeocodeV2(dic, (res) => {
  226. console.log("reverseGeocodeV2:" + JSON.stringify(res));
  227. this.reverseGeocodeV2Json = JSON.stringify(res)
  228. });
  229. },
  230. geocode(address) {
  231. var dic = {
  232. "address": address,
  233. "maxResults": 1 //最大返回的结果,andorid才有
  234. }
  235. KJGNSS.geocode(dic, (res) => {
  236. console.log("geocode:" + JSON.stringify(res));
  237. this.geocodeJson = JSON.stringify(res)
  238. });
  239. },
  240. registerGnssStatus() {
  241. KJGNSS.registerGnssStatus((res) => {
  242. console.log("registerGnssStatus:" + JSON.stringify(res));
  243. //返回参数说明参考:https://www.xfqiao.com/api/android-zh/android/location/GnssStatus.html
  244. this.registerGnssStatusJson = JSON.stringify(res)
  245. });
  246. },
  247. unregisterGnssStatus() {
  248. KJGNSS.unregisterGnssStatus();
  249. },
  250. registerGnssMeasurements() {
  251. KJGNSS.registerGnssMeasurements((res) => {
  252. console.log("registerGnssMeasurements:" + JSON.stringify(res));
  253. //返回参数说明参考:https://www.xfqiao.com/api/android-zh/android/location/GnssMeasurementsEvent.html
  254. this.registerGnssMeasurementsJson = JSON.stringify(res)
  255. });
  256. },
  257. unregisterGnssMeasurements() {
  258. KJGNSS.unregisterGnssMeasurements();
  259. }
  260. }
  261. }
  262. </script>
  263. <style>
  264. button {
  265. font-size: 15px
  266. }
  267. .json {
  268. word-wrap: break-word;
  269. }
  270. </style>