dangerarearesidentlocation.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <template>
  2. <view class="container">
  3. <uni-nav-bar dark :fixed="true" backgroundColor="#3F9EFF" statusBar="false" left-icon="left" left-text="返回"
  4. @clickLeft="toBack">
  5. <view class="nav-title">
  6. <text>{{title}}</text>
  7. </view>
  8. </uni-nav-bar>
  9. <view style="width: 100%;height: 160px;">
  10. <uni-forms ref="baseForm" :model="formData">
  11. <uni-card title="当前定位信息" :is-shadow="false">
  12. <view class="view-flex-inline">
  13. <uni-icons class="input-uni-icon" type="location" size="18" color="coral" />
  14. <text style="color: black;font-size: 1rem;margin-left: 4px;">经纬度:</text>
  15. <text
  16. style="color: blue;font-size: 0.8rem;padding-left: 5px">{{formData.longitude}},{{formData.latitude}}</text>
  17. </view>
  18. <view class="view-flex-inline" style="margin-top: 5px;">
  19. <uni-icons class="input-uni-icon" type="map-pin-ellipse" size="18" color="coral" />
  20. <text style="color: black;font-size: 1rem;margin-left: 4px;">地址:</text>
  21. <text style="color: blue;font-size: 0.8rem;padding-left: 5px">{{formData.address}}</text>
  22. </view>
  23. <view class="item-button-group "
  24. style="margin-top: 10px;padding-right: 0px; justify-content: center;">
  25. <!-- 定位返回 -->
  26. <view v-if="type==1" class="item-button mid-size" style="background-color: coral;"
  27. @click="onGetLngLat()">
  28. <view class="items-line">
  29. <uni-icons class="input-uni-icon" type="checkmarkempty" size="18" color="black" />
  30. <text class="button-text">确定</text>
  31. </view>
  32. </view>
  33. <!-- 提交补录定位 -->
  34. <view v-else-if="type==2" class="item-button mid-size" style="background-color: coral;"
  35. @click="toSubmit()">
  36. <view class="items-line">
  37. <uni-icons class="input-uni-icon" type="checkmarkempty" size="18" color="black" />
  38. <text class="button-text">立即提交</text>
  39. </view>
  40. </view>
  41. </view>
  42. </uni-card>
  43. </uni-forms>
  44. </view>
  45. <view style="width: 100%;height: 30px;">
  46. <view class="view-flex-inline-center">
  47. <text style="font-size: 0.8rem;color: orange;">点击下方地图重新拾取坐标信息</text>
  48. </view>
  49. </view>
  50. <view class="page-section page-section-gap">
  51. <map id="myMap" ref="myMap" :style="mapStyles" enable-zoom="true" :circles="circles" :scale="scale"
  52. :latitude="formData.latitude" :longitude="formData.longitude" @tap="tap">
  53. </map>
  54. </view>
  55. </view>
  56. </template>
  57. <script>
  58. import {
  59. gcoord
  60. } from '@/static/js/gcoord.global.prod.js'
  61. import http from '@/http/api.js';
  62. import {
  63. devUrl,
  64. prodUrl
  65. } from '@/common/setting';
  66. export default {
  67. data() {
  68. return {
  69. title: '地图拾取坐标',
  70. baseURL: '',
  71. type: 1,
  72. scale: 13,
  73. circles: [],
  74. formData: {
  75. id: '',
  76. latitude: '',
  77. longitude: '',
  78. address: '',
  79. },
  80. rules: {
  81. latitude: {
  82. rules: [{
  83. required: true,
  84. errorMessage: '必填项',
  85. }]
  86. },
  87. longitude: {
  88. rules: [{
  89. required: true,
  90. errorMessage: '必填项',
  91. }, ]
  92. },
  93. },
  94. mapStyles: {
  95. width: '100%',
  96. height: '600px'
  97. },
  98. }
  99. },
  100. onLoad(option) {
  101. this.baseURL = process.env.NODE_ENV === 'development' ? devUrl : prodUrl;
  102. this.type = option.type;
  103. if (this.type == 1) {
  104. this.restartLocation();
  105. } else if (this.type == 2) {
  106. this.formData.id = option.id;
  107. // let lng = option.lng;
  108. // let lat = option.lat;
  109. // if (lng != undefined && lng != null && lng != '' && lat != undefined && lat != null && lat != '') {
  110. // this.formData.longitude = lng;
  111. // this.formData.latitude = lat;
  112. // if (option.add != undefined && option.add != null) {
  113. // this.formData.address = option.add;
  114. // }
  115. // this.setMapPoint();
  116. // } else {
  117. this.restartLocation();
  118. //}
  119. }
  120. this.mapStyles.height = (uni.getWindowInfo().windowHeight - 50 - 160 - 30 - 10) + "px";
  121. },
  122. computed: {},
  123. onShow() {},
  124. onReady() {
  125. // 需要在onReady中设置规则
  126. // this.$refs.baseForm.setRules(this.rules)
  127. this.mapContext = uni.createMapContext('myMap', this);
  128. },
  129. mounted() {
  130. // #ifdef APP-PLUS
  131. this.addMapEvent();
  132. // #endif
  133. },
  134. methods: {
  135. addMapEvent() {
  136. let that = this;
  137. var maps = uni.createMapContext('myMap', this).$getAppMap();
  138. maps.onclick = function(point) {
  139. that.formData.longitude = new Number(point.longitude).toFixed(6);
  140. that.formData.latitude = new Number(point.latitude).toFixed(6);
  141. that.getAddress(point.longitude, point.latitude);
  142. that.setMapPoint();
  143. }
  144. },
  145. tap(e) {
  146. // #ifdef MP-WEIXIN
  147. let lat = e.target.latitude;
  148. let lng = e.target.longitude;
  149. this.formData.longitude = new Number(lng).toFixed(6);
  150. this.formData.latitude = new Number(lat).toFixed(6);
  151. this.getAddress(lng, lat);
  152. this.setMapPoint();
  153. // #endif
  154. },
  155. //返回上一页
  156. toBack() {
  157. uni.navigateBack({
  158. delta: 1
  159. })
  160. },
  161. onBackPress() {
  162. // #ifdef APP-PLUS
  163. plus.key.hideSoftKeybord();
  164. // #endif
  165. },
  166. restartLocation() {
  167. let that = this;
  168. uni.getLocation({
  169. type: 'wgs84',
  170. success: function(res) {
  171. console.log('当前位置:' + JSON.stringify(res));
  172. let p = gcoord.transform(
  173. [res.longitude, res.latitude],
  174. gcoord.WGS84,
  175. gcoord.GCJ02
  176. );
  177. that.formData.latitude = new Number(p[1]).toFixed(6);
  178. that.formData.longitude = new Number(p[0]).toFixed(6);
  179. that.getAddress(res.longitude, res.latitude);
  180. that.setMapPoint();
  181. }
  182. });
  183. },
  184. setMapPoint() {
  185. let circles = [];
  186. let rtu = {};
  187. rtu['longitude'] = parseFloat(this.formData.longitude);
  188. rtu['latitude'] = parseFloat(this.formData.latitude);
  189. rtu['color'] = '#FF0000';
  190. rtu['fillColor'] = '#FFFF00';
  191. rtu['radius'] = 100;
  192. circles.push(rtu);
  193. this.circles = circles;
  194. },
  195. getAddress(lng, lat) {
  196. let that = this;
  197. let url = 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + lat + ',' + lng +
  198. '&key=4HYBZ-EB23D-SLC42-HQ5R3-LP3LQ-OZFU5';
  199. http.request({
  200. url: url,
  201. method: 'GET',
  202. }).then(res => {}).catch(err => {
  203. that.formData.address = err.data.result.address;
  204. })
  205. },
  206. onGetLngLat() {
  207. let p = {
  208. 'lng': this.formData.longitude,
  209. 'lat': this.formData.latitude,
  210. 'add': this.formData.address,
  211. };
  212. uni.$emit('lngLat', JSON.stringify(p))
  213. this.toBack();
  214. },
  215. toSubmit() {
  216. let that = this;
  217. let lng = this.formData.longitude;
  218. let lat = this.formData.latitude;
  219. if (lng != undefined && lng != null && lng != '' && lat != undefined && lat != null && lat != '') {
  220. this.submit();
  221. } else {
  222. uni.showModal({
  223. content: "经纬度不能为空,请重新再试!",
  224. showCancel: false,
  225. success(res) {
  226. if (res.confirm) {
  227. }
  228. }
  229. });
  230. }
  231. },
  232. submit() {
  233. let that = this;
  234. console.log('toSubmit事件:', JSON.stringify(this.formData));
  235. http.request({
  236. url: '/galaxy-business/yj/resident/location/add',
  237. method: 'POST',
  238. data: this.formData
  239. }).then(res => {
  240. if (res.success) {
  241. if (res.code == 200) {
  242. uni.showModal({
  243. content: '操作已成功',
  244. showCancel: false,
  245. success(res) {
  246. if (res.confirm) {
  247. that.toBack();
  248. }
  249. }
  250. });
  251. } else {
  252. uni.showModal({
  253. content: '提交失败',
  254. showCancel: false
  255. });
  256. }
  257. } else {
  258. uni.showModal({
  259. content: '提交失败',
  260. showCancel: false
  261. });
  262. }
  263. }).catch(err => {
  264. uni.showModal({
  265. content: '' + err.data.msg,
  266. showCancel: false
  267. });
  268. })
  269. },
  270. }
  271. }
  272. </script>
  273. <style lang="scss" scoped>
  274. .container {
  275. padding: 0 0 100rpx;
  276. }
  277. </style>