setwoek.nvue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <template>
  2. <view class="setbox">
  3. <map
  4. id="container"
  5. class="map"
  6. :latitude="center.lat"
  7. :longitude="center.lng"
  8. @poitap="onClcikImgMap"
  9. @tap="onClcikImgMap"
  10. :markers="covers"
  11. scale="15"
  12. :style="'width: 750rpx;height:' + mapheight + 'px;'"
  13. ></map>
  14. <view class="maptop">
  15. <view class="maptopflex">
  16. <image :src="serimg" class="maptopimg"></image>
  17. <input ref='input' class="maptopint" id="tipInputs" v-model="searchtxt" @input="searchKeyword" placeholder="请输入地点进行查找" />
  18. </view>
  19. <!-- 地图联想 -->
  20. <view class="addrSearch_list" >
  21. <view class="addrSearch_item" v-for="(item, index) in searchList" :key="index" @click="handleAddrClick(item)" @blur='getblur'>
  22. <view class="wrap_name">
  23. <!-- #ifdef H5 -->
  24. <text class="wrap_text">{{item.name.substring(0, item.name.indexOf(searchtxt))}}</text>
  25. <text class="wrap_text" style="color: #2797FF;">{{searchtxt}}</text>
  26. <text class="wrap_text">{{item.name.substring(item.name.indexOf(searchtxt) + searchtxt.length)}}</text>
  27. <!-- #endif -->
  28. <!-- #ifndef H5 -->
  29. <rich-text :nodes="richNodes(item)"></rich-text>
  30. <!-- #endif -->
  31. </view>
  32. <view class="wrap_addr">
  33. <text class="wrap_addrs">{{ item.addr }}</text>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. <view class="mapfot">
  39. <view class="mapfott"><text class="mapfottit">标点位置</text></view>
  40. <view class="mapfotx">
  41. <image :src="adrimg" class="mapfotimg"></image>
  42. <text class="mapfottxt" v-if="address">{{ address }}</text>
  43. </view>
  44. <view class="mapfotn" @click="getNextFn"><text class="mapfotntit">下一步</text></view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import amapFile from '@/plugins/amap-wx.js';
  50. import self from "@/utils/location.js"
  51. // #ifdef APP
  52. var myAmapFun = new amapFile.AMapWX({
  53. key: '55d039b2d1dbcb54e792bca4de9c3500',
  54. batch: true
  55. });
  56. // #endif
  57. export default{
  58. data(){
  59. return{
  60. serimg:require("@/static/images/serch.png"),
  61. adrimg:require("@/static/images/adress.png"),
  62. mapimg:require("@/static/images/map.png"),
  63. center:{
  64. lng:'117.211954',
  65. lat:'31.839676',
  66. },
  67. mapflag:false,
  68. // userId: this.$store.state.user.userId,
  69. // deptId: this.$store.state.user.deptId,
  70. address:'',
  71. searchtxt:'',
  72. type:'add',//新增还是修改,
  73. placeSearchComponent:'',
  74. autoCompleteComponent:'',
  75. map:'',
  76. covers: [{
  77. latitude: 31.839676,
  78. longitude: 117.211954,
  79. iconPath: require("@/static/images/map.png"),
  80. anchor:{x: 0.5, y: 0.5}
  81. }],
  82. mapheight:0,
  83. mapContext:"",
  84. searchList:[],
  85. focus:false,
  86. location:'',
  87. }
  88. },
  89. onReady() {
  90. this.getcreateMap()
  91. },
  92. onLoad(e) {
  93. if(e.type=='update'){
  94. this.type='update'
  95. this.center.lng=e.lng;
  96. this.center.lat=e.lat;
  97. this.address=e.address;
  98. }else{
  99. if(e.address){
  100. this.type='update'
  101. this.center.lng=e.lng;
  102. this.center.lat=e.lat;
  103. this.address=e.address;
  104. }else{
  105. this.type='add';
  106. }
  107. }
  108. if(e.fromurl){
  109. this.fromurl=e.fromurl
  110. }
  111. },
  112. mounted() {
  113. var that=this;
  114. this.getinfo();
  115. if(this.type=='add'){
  116. this.getlocation()
  117. }else{
  118. var lng=this.center.lng;
  119. var lat=this.center.lat;
  120. that.covers = [];
  121. that.covers = [{
  122. latitude: lat,
  123. longitude: lng,
  124. iconPath: that.mapimg,
  125. anchor:{x: 0.5, y: 0.5}
  126. }]
  127. // #ifdef APP
  128. that.getCity(lat, lng)
  129. // #endif
  130. }
  131. },
  132. methods:{
  133. getNextFn(){
  134. if(this.type=='add'){
  135. if(this.fromurl&&this.fromurl=='rule'){
  136. var address={
  137. address:this.address,
  138. lng:this.center.lng,
  139. lat:this.center.lat,
  140. }
  141. uni.setStorageSync('location', address);
  142. uni.$emit('refreshaddr');
  143. uni.navigateBack({delta: 1});
  144. }else{
  145. uni.redirectTo({
  146. url: '/work/pages/setswork?type=add&address='+this.address+'&lng='+this.center.lng+'&lat='+this.center.lat
  147. });
  148. }
  149. }else{
  150. var address={
  151. address:this.address,
  152. lng:this.center.lng,
  153. lat:this.center.lat,
  154. }
  155. uni.setStorageSync('location', address);
  156. uni.$emit('refreshaddr');
  157. uni.navigateBack({delta: 1});
  158. }
  159. },
  160. searchKeyword(){
  161. var that=this;
  162. if(this.searchtxt){
  163. // #ifdef H5
  164. AMap.plugin('AMap.PlaceSearch', function(){
  165. let autoOptions = {
  166. city:'全国',
  167. citylimit: true,
  168. pageIndex: 1,
  169. pageSize: 20,
  170. }
  171. let placeSearch = new AMap.PlaceSearch(autoOptions);
  172. placeSearch.search(that.searchtxt, function(status, result) {
  173. // 搜索成功时,result即是对应的匹配数据
  174. if (status === 'complete' && result.info === 'OK') {
  175. let items = result.poiList.pois;
  176. console.log(items,122)
  177. if(items.length > 0){
  178. var pois = [];
  179. for (let i in items) {
  180. var obj = {
  181. "name": items[i].name,
  182. "addr": items[i].address,
  183. "lng": items[i].location.lng,
  184. "lat": items[i].location.lat
  185. };
  186. pois.push(obj);
  187. }
  188. that.searchList = pois;
  189. }
  190. }else{
  191. that.searchList = [];
  192. }
  193. })
  194. })
  195. // #endif
  196. // #ifndef H5
  197. myAmapFun.getInputtips({
  198. keywords: that.searchtxt,
  199. city: '全国',
  200. citylimit: true,
  201. success: function(result){
  202. if(result.tips.length > 0){
  203. let pois = [];
  204. let items = result.tips;
  205. for (let i in items) {
  206. var lnglat = items[i].location.split(',');
  207. var obj = {
  208. "name": items[i].name,
  209. "addr": items[i].district + items[i].address,
  210. "lng": lnglat[0],
  211. "lat": lnglat[1]
  212. };
  213. pois.push(obj);
  214. }
  215. that.searchList = pois;
  216. }else{
  217. that.searchList = [];
  218. }
  219. },
  220. fail: function(info){
  221. that.searchList = [];
  222. console.log('fail-search', info)
  223. }
  224. })
  225. // #endif
  226. }
  227. },
  228. handleAddrClick(addr){
  229. console.log(addr)
  230. var that=this;
  231. var lng=addr.lng;
  232. var lat=addr.lat;
  233. that.center.lng=lng;
  234. that.center.lat=lat;
  235. that.covers = [];
  236. that.covers = [{
  237. latitude: addr.lat,
  238. longitude: addr.lng,
  239. iconPath: that.mapimg,
  240. anchor:{x: 0.5, y: 0.5}
  241. }]
  242. that.address=addr.name || addr.addr;
  243. that.searchList=[];
  244. that.$refs.input.blur()
  245. // #ifdef APP
  246. that.getCity(lat, lng)
  247. // #endif
  248. },
  249. richNodes(item){
  250. return [{
  251. children: [
  252. {
  253. type: 'text',
  254. attrs: {
  255. class:'wrap_text'
  256. },
  257. text: item.name.substring(0, item.name.indexOf(this.searchtxt))
  258. },
  259. {
  260. type: 'text',
  261. attrs: {
  262. class:'wrap_texts'
  263. },
  264. text: this.searchtxt
  265. },
  266. {
  267. type: 'text',
  268. attrs: {
  269. class:'wrap_text'
  270. },
  271. text: item.name.substring(item.name.indexOf(this.searchtxt) + this.searchtxt.length)
  272. }
  273. ]
  274. }]
  275. },
  276. // 获取当前屏幕信息
  277. getinfo(){
  278. var that=this;
  279. uni.getSystemInfo({
  280. success(e) {
  281. that.mapheight=e.windowHeight;
  282. },
  283. })
  284. },
  285. getcreateMap(){
  286. //mapId 就是你在 map 标签中定义的 id
  287. this.mapContext = uni.createMapContext('container', this);
  288. },
  289. onClcikImgMap(e) {
  290. var that=this;
  291. var lng=e.detail.longitude;
  292. var lat=e.detail.latitude;
  293. that.center.lng=lng;
  294. that.center.lat=lat;
  295. that.covers = [];
  296. that.covers = [{
  297. latitude: e.detail.latitude,
  298. longitude: e.detail.longitude,
  299. iconPath: that.mapimg,
  300. anchor:{x: 0.5, y: 0.5}
  301. }]
  302. // #ifdef APP
  303. that.getCity(lat, lng)
  304. // #endif
  305. },
  306. //获取中心点
  307. getCenterLanLat() {
  308. var that = this;
  309. this.mapContext.getCenterLocation({
  310. type: 'gcj02',
  311. success: res => {
  312. that.center.lng=res.longitude;
  313. that.center.lat=res.latitude;
  314. that.covers = [];
  315. that.covers = [{
  316. latitude: e.detail.latitude,
  317. longitude: e.detail.longitude,
  318. iconPath: that.mapimg,
  319. anchor:{x: 0.5, y: 0.5}
  320. }]
  321. // #ifdef APP
  322. that.getCity(res.latitude, res.longitude)
  323. // #endif
  324. },
  325. fail: err => {
  326. console.log('获取当前地图中心的经纬度2', err);
  327. },
  328. });
  329. },
  330. getCity(latitude, longitude) {
  331. var that = this;
  332. that.location = `${longitude},${latitude}`
  333. myAmapFun.getRegeo({
  334. //如果经纬度有问题会导致不进入回调方法,从而报错
  335. location: that.location,
  336. success: function(e) {
  337. //成功回调
  338. that.address='';
  339. that.address = e[0].regeocodeData.formatted_address; //详细地址
  340. },
  341. fail: function(info) {
  342. //失败回调
  343. console.log(info)
  344. }
  345. })
  346. },
  347. // 获取定位
  348. getlocation(){
  349. var that = this;
  350. uni.getLocation({
  351. type: 'gcj02',
  352. geocode: true,
  353. success: function(res) {
  354. that.center.lng=res.longitude;
  355. that.center.lat=res.latitude;
  356. // #ifdef H5
  357. var adr= address.province+address.city+address.district+address.street+address.streetNum+address.poiName
  358. // that.address=res.longitude
  359. that.address=adr
  360. // #endif
  361. // #ifdef APP
  362. that.getCity(res.latitude, res.longitude)
  363. // #endif
  364. },
  365. fail: function(red) {
  366. uni.showToast({
  367. title:"获取定位失败"
  368. })
  369. },
  370. })
  371. },
  372. // 点击刷新方法
  373. refresh(e){
  374. this.getlocation();
  375. },
  376. }
  377. }
  378. </script>
  379. <style lang="scss" scoped>
  380. .maptop {
  381. position: fixed;
  382. left: 34rpx;
  383. right: 34rpx;
  384. top: 24rpx;
  385. }
  386. .maptopflex {
  387. display: flex;
  388. align-items: center;
  389. padding: 0 32rpx;
  390. box-sizing: border-box;
  391. flex-direction: row;
  392. width: 684rpx;
  393. height: 84rpx;
  394. background: #ffffff;
  395. box-shadow: 0px 0px 12rpx 0px rgba(75, 75, 75, 0.35);
  396. border-radius: 6rpx;
  397. margin-bottom: 10rpx;
  398. }
  399. .maptopimg {
  400. width: 36rpx;
  401. height: 38rpx;
  402. margin-right: 16rpx;
  403. }
  404. .maptopint {
  405. font-size: 32rpx;
  406. color: #666666;
  407. flex: 1;
  408. height: 84rpx;
  409. }
  410. .mapfot {
  411. position: fixed;
  412. left: 34rpx;
  413. right: 34rpx;
  414. bottom: 24rpx;
  415. background: #fff;
  416. box-shadow: 0px 0px 12rpx 0px rgba(75, 75, 75, 0.35);
  417. border-radius: 6rpx;
  418. padding: 40rpx 32rpx 32rpx;
  419. height: 300rpx;
  420. }
  421. .mapfott {
  422. margin-bottom: 28rpx;
  423. }
  424. .mapfottit {
  425. font-size: 33rpx;
  426. font-weight: bold;
  427. color: #343434;
  428. }
  429. .mapfotx {
  430. display: flex;
  431. margin-bottom: 32rpx;
  432. min-height: 70rpx;
  433. flex-direction: row;
  434. }
  435. .mapfotimg {
  436. width: 26rpx;
  437. height: 28rpx;
  438. flex: 0 0 auto;
  439. margin-right: 12rpx;
  440. }
  441. .mapfottxt {
  442. flex: 1;
  443. font-size: 32rpx;
  444. color: #343434;
  445. line-height: 32rpx;
  446. }
  447. .mapfotn {
  448. width: 618rpx;
  449. height: 76rpx;
  450. background: #1678ff;
  451. border-radius: 6rpx;
  452. display: flex;
  453. align-items: center;
  454. justify-content: center;
  455. }
  456. .mapfotntit {
  457. font-size: 34rpx;
  458. color: #fff;
  459. }
  460. .addrSearch_item {
  461. border-bottom: 2upx solid #f5f5f5;
  462. padding: 4upx;
  463. background: #ffffff;
  464. }
  465. .addrSearch_item .wrap_name {
  466. display: flex;
  467. flex-direction: row !important;
  468. }
  469. .wrap_text {
  470. font-size: 34upx;
  471. color: #151515;
  472. line-height: 40upx;
  473. }
  474. .wrap_texts{
  475. font-size: 34upx;
  476. color: #2797FF;
  477. line-height: 40upx;
  478. }
  479. .addrSearch_item .wrap_addr {
  480. margin-top: 2upx;
  481. }
  482. .wrap_addrs {
  483. font-size: 28upx;
  484. color: #999;
  485. line-height: 32upx;
  486. }
  487. </style>