map.nvue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. <template>
  2. <view>
  3. <customNav :shownav="state" title="map" titledetl=""></customNav>
  4. <view class="mapcontentV" :style="'height:'+mapHeight+'rpx;'">
  5. <view class="">
  6. <map id="map" class="mapbody" scale="18" show-location="true" :style="'height:'+mapHeight+'rpx;'" :latitude="latitude" :longitude="longitude" :markers="covers" :controls="controls" @markertap="markertap" @regionchange="regionChange" @controltap="controlstp" @anchorpointtap="anchorpointtap">
  7. </map>
  8. </view>
  9. </view>
  10. <view class="contensb">
  11. <view style="height: 8rpx;background-color:lavender;width: 100vw;"></view>
  12. <!-- <text>{{latitude}},{{longitude}}</text> -->
  13. <scroll-view class="scrollVct" :scroll-top="scrollTop" scroll-y="true" :style="'height:'+listHight+'rpx;'">
  14. <view class="contentColumn" v-if="type==0">
  15. <view class="itemContent contentInRowS" v-for="(item,index) in pois" @click="querendizhi(index)">
  16. <uni-icons v-if="chakanzb" custom-prefix="custom-icon" color="#006AFF" type="location-filled" size="28" @click="listMarkertap(item)"></uni-icons>
  17. <view class="contentColumn" style="width: 580upx;align-items: center;">
  18. <text class="addrName">{{item.name}}</text>
  19. <text class="addrNamedetl">{{item.addr}}</text>
  20. </view>
  21. </view>
  22. </view>
  23. <view class="contentColumn" v-if="type==1">
  24. <view class="itemContent contentInRowL" v-for="(item,index) in pois" @click="querendizhi(index)">
  25. <uni-icons v-if="chakanzb" custom-prefix="custom-icon" color="#006AFF" type="location-filled" size="28" @click="listMarkertap(item)"></uni-icons>
  26. <view class="contentColumn" style="width: 580upx;align-items: center;">
  27. <text class="addrName">{{item.name}}</text>
  28. <text class="addrNamedetl">{{item.vicinity}}</text>
  29. </view>
  30. </view>
  31. <view v-if="pois.length==0" class="itemContent contentInRowL">
  32. <image style="width: 50rpx;height: 50rpx;" src="/static/images/Rdizhil100@3x.png" mode="scaleToFill"></image>
  33. <view class="contentColumn" style="width: 580upx;align-items: center;">
  34. <text class="addrName">{{centerpoi.name}}</text>
  35. <text class="addrNamedetl">{{centerpoi.address}}</text>
  36. </view>
  37. </view>
  38. </view>
  39. </scroll-view>
  40. </view>
  41. <uni-popup ref="alertDialog" type="dialog">
  42. <uni-popup-dialog type="NOMLE" :cancelText="i18n('order.quxiao')" :confirmText="i18n('comp.queren')" :title="noteMsg.title" :content="noteMsg.msg" @confirm="dialogConfirm"
  43. @close="dialogClose"></uni-popup-dialog>
  44. </uni-popup>
  45. </view>
  46. </template>
  47. <script>
  48. import {http} from '@/utils/request';
  49. import {
  50. getLocationAddr,
  51. gcj02towgs,
  52. setLocat
  53. } from '@/pages/api/basic.js';
  54. export default {
  55. data() {
  56. return {
  57. controls:[],
  58. covers:[],
  59. latitude: 22.881143,
  60. longitude: 108.41943,
  61. mapCenter:{
  62. lat:22.881143,
  63. lon: 108.41943
  64. },
  65. noteMsg:{
  66. title:'',
  67. msg:'',
  68. markAddr:'',
  69. lat:0,
  70. lon:0
  71. },
  72. pois:[],
  73. centerpoi:'',
  74. markAddr:'',
  75. scrollTop:0,
  76. mapHeight:0,
  77. listHight:0,
  78. left:0,
  79. top:0,
  80. mapContext:Object,
  81. state:true,
  82. isAndroid:true,
  83. chakanzb:true,
  84. type:1,
  85. previous:0,
  86. timer:''
  87. }
  88. },
  89. onReady() {
  90. this.mapContext = uni.createMapContext("map",this);
  91. console.log('---------1')
  92. if(!this.isAndroid){
  93. this.setCenter();
  94. }
  95. this.gotoLocat();
  96. //this.mapContext.showUserLocation(true);
  97. },
  98. onBackPress(options) {
  99. //console.log('options', options);
  100. if (options.from == 'backbutton') {
  101. return !this.state;
  102. } else if (options.from == 'navigateBack') {
  103. return false;
  104. }
  105. },
  106. onLoad(option) {
  107. let systemInfo = uni.getSystemInfoSync();
  108. this.isAndroid = systemInfo.platform.toLowerCase() === 'android';
  109. var temp = option.state;
  110. if(temp==1){
  111. this.state=false;
  112. }
  113. if(temp==2){
  114. this.chakanzb=false;
  115. }
  116. var info = uni.getSystemInfoSync();
  117. this.mapHeight=600;
  118. this.listHight=info.windowHeight*2-740-info.statusBarHeight*2;
  119. this.left = 750;
  120. this.top = 250;
  121. },
  122. methods: {
  123. i18n(str){
  124. return getApp().globalData.$t(str);
  125. },
  126. gotoLocat(){
  127. var that = this;
  128. uni.getLocation({
  129. type: "wgs84", //默认为 wgs84 返回 gps 坐标//谷歌地图使用 wgs84 坐标,其他地图使用 gcj02 坐标
  130. geocode: "true",
  131. isHighAccuracy: "true",
  132. accuracy: "best", // 精度值为20m
  133. success: function (res) {
  134. console.log(res);
  135. that.markAddr=that.i18n('index.xianzaiwez');
  136. if(res.type=='gcj02'){
  137. that.movetoCenter(res.latitude,res.longitude,false);
  138. that.gcj02towgs(res.latitude,res.longitude);//gcj02定位查询
  139. }
  140. else if(res.type=='wgs84'){
  141. //that.setLocat(res.latitude,res.longitude,1,5);//wgs84转为gcj02显示在地图上
  142. that.movetoCenter(res.latitude,res.longitude,false);
  143. }
  144. },
  145. fail(err) {
  146. }
  147. });
  148. },
  149. movetoCenter(latitude,longitude,sLb){
  150. if(this.latitude==latitude){
  151. this.latitude=latitude+0.0000000001;
  152. }
  153. else{
  154. this.latitude=latitude;
  155. }
  156. this.longitude=longitude;
  157. this.mapCenter.lat = this.latitude;
  158. this.mapCenter.lon = this.longitude;
  159. this.mapContext.moveToLocation();
  160. },
  161. setCenterMark(){
  162. this.covers=[{
  163. id:1,//控件id
  164. iconPath:'/static/images/Rdizhil100@3x.png',//显示的图标
  165. latitude:this.mapCenter.lat,
  166. longitude:this.mapCenter.lon,
  167. }];
  168. },
  169. getAreaCode(latlng){
  170. var userId = uni.getStorageSync('userId');
  171. var that = this;
  172. http.get(`${getLocationAddr}`, {
  173. latlng:latlng,
  174. type:this.type,
  175. id:userId
  176. },false)
  177. .then(r => {
  178. //console.log('getAreaCode',r);
  179. if(r.data.code!=200){
  180. return;
  181. }
  182. that.dowithAddr(r.data.data,latlng);
  183. that.scrollTop=0;
  184. })
  185. .catch(err => {
  186. })
  187. },
  188. dowithAddr(r,latlng){
  189. console.log(this.type,r);
  190. if(this.type==0){
  191. if(r.result.pois.length>0){
  192. this.pois=r.result.pois;
  193. }
  194. }
  195. else if(this.type==1){
  196. if(r.results.length>0){
  197. r.results[0].name=r.address.name;
  198. r.results[0].vicinity=r.address.address;
  199. var s = latlng.split(',');
  200. r.results[0].geometry.location.lng=s[1];
  201. r.results[0].geometry.location.lat=s[0];
  202. this.pois=r.results;
  203. this.markAddr=r.address.address;
  204. }
  205. else{
  206. var s = latlng.split(',');
  207. this.centerpoi={
  208. "address":r.address.address,
  209. "name":r.address.name,
  210. "lng":s[1],
  211. "lat":s[0]
  212. }
  213. this.markAddr=r.address.address;
  214. }
  215. }
  216. },
  217. regionChange(val){
  218. console.log('regionChange',val)
  219. var now = new Date().valueOf();
  220. if(this.isAndroid){
  221. if(val.type!='end'){
  222. return;
  223. }
  224. }
  225. else{
  226. if(val.detail.type!='end'){
  227. console.log('ios begin')
  228. return;
  229. }
  230. else{
  231. console.log('ios end')
  232. }
  233. }
  234. var that = this;
  235. if (now - that.previous < 300) {
  236. if (that.timer) clearTimeout(that.timer)
  237. that.timer = setTimeout(() => {
  238. that.previous = now
  239. that.CenterLocation();
  240. }, 300)
  241. }
  242. else{
  243. that.previous=now;
  244. if (that.timer) clearTimeout(that.timer)
  245. that.timer = setTimeout(() => {
  246. that.CenterLocation();
  247. }, 300)
  248. }
  249. },
  250. CenterLocation(){
  251. var that = this;
  252. this.mapContext.getCenterLocation({
  253. success: res => {//经纬度格式是gcj02
  254. console.log('getCenterLocation',res)
  255. this.mapCenter.lat = res.latitude;
  256. this.mapCenter.lon = res.longitude;
  257. //that.bdtogps(tap,2)
  258. //this.gcj02towgs(res.latitude,res.longitude);
  259. var latlon= res.latitude+','+res.longitude;
  260. console.log('----1',latlon);
  261. this.setCenterMark();
  262. this.getAreaCode(latlon);
  263. this.tempmark=false;
  264. },
  265. fail: res => {
  266. console.log(res);
  267. }
  268. })
  269. },
  270. controlstp(tap){
  271. console.log(tap);
  272. if(tap.detail.controlId==2){
  273. this.gotoLocat();
  274. }
  275. if(tap.detail.controlId==1){
  276. console.log(tap);
  277. var addr={
  278. address:this.markAddr,
  279. name:'',
  280. longitude:this.mapCenter.lon,
  281. latitude: this.mapCenter.lat,
  282. }
  283. var that = this;
  284. uni.showModal({
  285. title: that.i18n('api.message'),
  286. content:that.i18n('index.querenxzwz'),
  287. cancelText: that.i18n('order.quxiao'), // 取消按钮的文字
  288. confirmText: that.i18n('comp.queren'), // 确认按钮的文字
  289. success: function (res) {
  290. if (res.confirm) { //这里是点击了确定以后
  291. uni.setStorageSync('tempLocation',addr);
  292. uni.setStorageSync('userAddrLocation',addr);
  293. uni.navigateBack();
  294. } else { //这里是点击了取消以后
  295. }
  296. }
  297. })
  298. }
  299. },
  300. markertap(tap){
  301. console.log('markertap---',tap);
  302. this.noteMsg.title=this.i18n('api.message');
  303. this.noteMsg.msg=this.i18n('index.querenxzwz');
  304. this.noteMsg.lon=this.mapCenter.lon;
  305. this.noteMsg.lat=this.mapCenter.lat;
  306. this.noteMsg.markAddr=this.markAddr;
  307. this.$refs.alertDialog.open('center');
  308. },
  309. anchorpointtap(tap){
  310. console.log('anchorpointtap-----',tap);
  311. this.noteMsg.markAddr=this.i18n('index.xianzaiwez');
  312. this.noteMsg.lon=tap.detail.longitude;
  313. this.noteMsg.lat=tap.detail.latitude;
  314. this.noteMsg.title=this.i18n('api.message');
  315. this.noteMsg.msg=this.i18n('index.querenxzwz');
  316. this.$refs.alertDialog.open('center');
  317. },
  318. listMarkertap(tap){
  319. var addr;
  320. if(this.type==0){
  321. //this.bdtogps(tap,1);
  322. }
  323. else if(this.type==1){
  324. addr={
  325. address: tap.vicinity,
  326. name:tap.name,
  327. longitude:tap.geometry.location.lng,
  328. latitude: tap.geometry.location.lat,
  329. }
  330. uni.setStorageSync('tempLocation',addr);
  331. uni.setStorageSync('userAddrLocation',addr);
  332. uni.navigateBack();
  333. }
  334. },
  335. itemSelect(index){
  336. if(index==1){
  337. uni.navigateBack();
  338. }
  339. if(index==2){
  340. // uni.redirectTo({
  341. // url:'/pages/UserCenter/dizhi/newdizhi?id=-1'
  342. // })
  343. }
  344. },
  345. dialogConfirm() {
  346. console.log('点击确认')
  347. var addr={
  348. address:this.noteMsg.markAddr,
  349. name:'',
  350. longitude:this.noteMsg.lon,
  351. latitude: this.noteMsg.lat,
  352. }
  353. uni.setStorageSync('tempLocation',addr);
  354. uni.setStorageSync('userAddrLocation',addr);
  355. setTimeout(()=>{
  356. uni.navigateBack();
  357. },200);
  358. },
  359. dialogClose() {
  360. console.log('点击关闭')
  361. },
  362. setCenter(){
  363. //console.log('setCenter',this.left,this.top)
  364. this.controls=[
  365. // {//在地图上显示控件,控件不随着地图移动
  366. // id:1,//控件id
  367. // iconPath:'/static/images/Rdizhil@3x.png',//显示的图标
  368. // clickable:true,
  369. // position:{//控件在地图的位置
  370. // left:158,
  371. // top:150-36,
  372. // width:36,
  373. // height:36
  374. // },
  375. // },
  376. {//在地图上显示控件,控件不随着地图移动
  377. id:2,//控件id
  378. iconPath:'/static/images/dingwei.png',//显示的图标
  379. clickable:true,
  380. position:{//控件在地图的位置
  381. left:365,
  382. top:285,
  383. width:40,
  384. height:40
  385. },
  386. }
  387. ];
  388. },
  389. gcj02towgs(latitude,longitude){
  390. http.get(`${gcj02towgs}`, {
  391. lng:longitude,
  392. lat:latitude
  393. },false)
  394. .then(r => {
  395. //console.log('gcj02towgs:',r);
  396. let str = r.data.data;
  397. let s = str.split(',');
  398. if(s.length>1){
  399. var latlon= s[1]+','+s[0];
  400. //console.log('----1',latlon);
  401. this.getAreaCode(latlon);
  402. }
  403. else{
  404. this.getAreaCode(r.data.data);
  405. }
  406. })
  407. .catch(err => {
  408. console.log(err)
  409. })
  410. },
  411. querendizhi(index){
  412. var addr;
  413. var poi=this.pois[index];
  414. if(this.type==0){
  415. // this.markAddr=poi.addr;
  416. // this.movetoCenter(poi.point.y,poi.point.x,false);
  417. this.setLocat(poi.point.y,poi.point.x,5,3);//百度API查询结果bd09ll转为gcj02显示在百度地图上
  418. //this.bdtogps(tap,2);
  419. }
  420. else if(this.type==1){
  421. this.movetoCenter(poi.geometry.location.lat,poi.geometry.location.lng,false);
  422. //this.setLocat(poi.geometry.location.lat,poi.geometry.location.lng,1,5);//谷歌返回GPS转为转为gcj02显示在百度地图上
  423. //var latlon= poi.geometry.location.lat+','+poi.geometry.location.lng;
  424. //that.getAreaCode(latlon);
  425. }
  426. return;
  427. },
  428. addrSelect(){
  429. },
  430. setLocat(latitude,longitude,f,t){
  431. var latlng = longitude+','+latitude;
  432. http.get(`${setLocat}`, {
  433. latlng:latlng,
  434. from:f,
  435. to:t
  436. },false)
  437. .then(r => {
  438. console.log('setLocat:',r);
  439. this.movetoCenter(r.data.data.result[0].y,r.data.data.result[0].x,false);
  440. })
  441. .catch(err => {
  442. console.log(err)
  443. })
  444. },
  445. // 转 GPS
  446. bdtogps(addr,type){
  447. http.get(`${gcj02towgs}`, {
  448. lng:addr.longitude,
  449. lat:addr.latitude
  450. },false)
  451. .then(res => {
  452. console.log('bdtogps2:',res);
  453. let str = res.data.data;
  454. let s = str.split(',');
  455. if(type==0){
  456. var tpaddr={
  457. address:addr.address,
  458. name:'',
  459. longitude:s[0],
  460. latitude:s[1],
  461. }
  462. uni.setStorageSync('tempLocation',tpaddr);
  463. uni.setStorageSync('userAddrLocation',tpaddr);
  464. uni.navigateBack();
  465. }
  466. })
  467. .catch(err => {
  468. console.log(err)
  469. })
  470. },
  471. }
  472. }
  473. </script>
  474. <style>
  475. .mapbody{
  476. width: 750rpx;
  477. }
  478. .mapcontentV{
  479. margin-top: 20rpx;
  480. width: 750rpx;
  481. background-color: bisque;
  482. }
  483. .contensb{
  484. display: flex;
  485. flex-direction: column;
  486. justify-content:flex-start;
  487. width: 750rpx;
  488. }
  489. .scrollVct{
  490. width: 750rpx;
  491. }
  492. .itemContent{
  493. width: 740upx;
  494. margin-left: 5upx;
  495. padding: 10rpx;
  496. border-bottom-width: 2rpx;
  497. border-bottom-color:lightgray;
  498. align-items: center;
  499. }
  500. .addrName{
  501. width: 580upx;
  502. margin-left:10rpx;
  503. font-size: 32rpx;
  504. color: black;
  505. height: 36rpx;
  506. line-height: 36rpx;
  507. /* overflow: hidden;
  508. text-overflow: ellipsis;
  509. display: -webkit-box;
  510. -webkit-line-clamp: 1;
  511. -webkit-box-orient: vertical; */
  512. }
  513. .addrNamedetlV{
  514. display: flex;
  515. width: 100%;
  516. margin-left: 10rpx;
  517. height: 100rpx;
  518. align-items: center;
  519. }
  520. .addrNamedetl{
  521. text-align: left;
  522. width: 580upx;
  523. margin-left: 10rpx;
  524. font-size: 28rpx;
  525. color:darkslategrey;
  526. padding: 10rpx;
  527. }
  528. .popocontenv{
  529. background-color: cornflowerblue;
  530. border-radius: 16rpx 16rpx 0rpx 0rpx;
  531. }
  532. .itemViewTop{
  533. display: flex;
  534. background-color:whitesmoke;
  535. width: 100vw;
  536. padding: 10rpx;
  537. height: 70rpx;
  538. justify-content: center;
  539. align-items: center;
  540. text-align: center;
  541. box-shadow: 0rpx 0rpx -10rpx 0rpx #006AFF;
  542. }
  543. .itemTextTop{
  544. text-align: center;
  545. font-size: 32rpx;
  546. color: black;
  547. }
  548. .itemView{
  549. width: 100vw;
  550. padding: 10rpx;
  551. height: 100rpx;
  552. background-color: white;
  553. text-align: center;
  554. }
  555. .itemTextA{
  556. width: 80vw;
  557. height: 70rpx;
  558. border-radius: 20rpx;
  559. line-height: 70rpx;
  560. text-align: center;
  561. font-size: 32rpx;
  562. color: white;
  563. background-color: #006AFF;
  564. box-shadow: 0rpx 0rpx 10rpx 0rpx #006AFF;
  565. }
  566. .itemTextB{
  567. width: 80vw;
  568. height: 70rpx;
  569. border-radius: 20rpx;
  570. line-height: 70rpx;
  571. text-align: center;
  572. font-size: 32rpx;
  573. color: white;
  574. background-color: sandybrown;
  575. box-shadow: 0rpx 0rpx 10rpx 0rpx sandybrown;
  576. }
  577. </style>