mapN.nvue 17 KB

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