setwoek.nvue 12 KB

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