map.vue 13 KB

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