maps.nvue 12 KB

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