maplocation.vue 8.1 KB

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