map.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <template>
  2. <view>
  3. <customNav :shownav="state" :title="$t('index.dizhixuanz')" titledetl=""></customNav>
  4. <view class="mapcontentV" :style="'height:'+mapHeight+'rpx;'">
  5. <view class="">
  6. <map id="map" class="mapbody" scale="21" :style="'height:'+mapHeight+'rpx;'" :latitude="latitude" :longitude="longitude" :markers="covers" :controls="controls" @controltap="controlstp" @regionchange="regionChange">
  7. </map>
  8. </view>
  9. </view>
  10. <view class="contensb">
  11. <view style="height: 8rpx;background-color:aliceblue;"></view>
  12. <scroll-view :scroll-top="scrollTop" scroll-y="true" :style="'height:'+listHight+'rpx;'">
  13. <view v-if="type==0">
  14. <view class="itemContent contentInRowS" v-for="(item,index) in pois" @click="querendizhi(index)">
  15. <uni-icons v-if="chakanzb" custom-prefix="custom-icon" color="#E02F73" type="location-filled" size="28" @click="markertap(item)"></uni-icons>
  16. <view class="contentColumn" style="width: 80%;align-items: center;">
  17. <text class="addrName">{{item.name}}</text>
  18. <text class="addrNamedetl">{{item.addr}}</text>
  19. </view>
  20. <uni-icons custom-prefix="custom-icon" color="slateblue" type="compose" size="28" @click="addrselect(item)"></uni-icons>
  21. </view>
  22. </view>
  23. <view 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="#E02F73" type="location-filled" size="28" @click="markertap(item)"></uni-icons>
  26. <view class="contentColumn" style="width: 80%;align-items: center;">
  27. <text class="addrName">{{item.name}}</text>
  28. <text class="addrNamedetl">{{item.vicinity}}</text>
  29. </view>
  30. <uni-icons custom-prefix="custom-icon" color="slateblue" type="compose" size="28" @click="addrselect(item)"></uni-icons>
  31. </view>
  32. </view>
  33. </scroll-view>
  34. </view>
  35. <!-- 提示窗示例 -->
  36. <popViewGG :show="showpop" :maskClosable="true" :isCancel="false" @chooseCancel="chooseCancel">
  37. <view class="contentColumnC popocontenv">
  38. <view class="itemViewTop">
  39. <text class="itemTextTop">{{$t('index.weizhiyongyu')}}</text>
  40. </view>
  41. <view v-if="chakanzb" class="contentColumnC itemView" @click="itemSelect(1)">
  42. <text class="itemTextA">{{$t('index.zhoubianmd')}}</text>
  43. </view>
  44. <view class="contentColumnC itemView" @click="itemSelect(2)">
  45. <text class="itemTextB">+{{$t('index.xinzengshdz')}}</text>
  46. </view>
  47. </view>
  48. </popViewGG>
  49. </view>
  50. </template>
  51. <script>
  52. import api from "@/pages/api/api.js"
  53. export default {
  54. data() {
  55. return {
  56. controls:[],
  57. covers:[],
  58. latitude: 0,
  59. longitude:0,
  60. pois:[],
  61. poi:[],
  62. markAddr:'',
  63. scrollTop:0,
  64. mapHeight:0,
  65. listHight:0,
  66. left:0,
  67. top:1,
  68. mapContext:Object,
  69. mapready:false,
  70. state:true,
  71. chakanzb:true,
  72. type:1,
  73. showLb:false,
  74. showpop:false,
  75. juli:0
  76. }
  77. },
  78. onReady() {
  79. this.mapContext = uni.createMapContext("map",this);
  80. //console.log('---------1')
  81. //this.showLb=true;
  82. this.setCenter();
  83. this.gotoLocat();
  84. var that = this;
  85. setTimeout(()=>{
  86. that.mapready = true;
  87. },6000)
  88. },
  89. onBackPress(options) {
  90. //console.log('options', options);
  91. if (options.from == 'backbutton') {
  92. return !this.state;
  93. } else if (options.from == 'navigateBack') {
  94. return false;
  95. }
  96. },
  97. onLoad(option) {
  98. var temp = option.state;
  99. if(temp==1){
  100. this.state=false;
  101. }
  102. if(temp==2){
  103. this.chakanzb=false;
  104. }
  105. var info = uni.getSystemInfoSync();
  106. this.mapHeight=600;
  107. this.listHight=info.windowHeight*2-640-info.statusBarHeight*2;
  108. this.left = 750;
  109. this.top = 250;
  110. },
  111. methods: {
  112. gotoLocat(){
  113. var that = this;
  114. uni.getLocation({
  115. type: "wgs84", //默认为 wgs84 返回 gps 坐标//谷歌地图使用 wgs84 坐标,其他地图使用 gcj02 坐标
  116. geocode: "true",
  117. isHighAccuracy: "true",
  118. accuracy: "best", // 精度值为20m
  119. success: function (res) {
  120. that.markAddr=that.$t('index.xianzaiwez');
  121. that.setLocat(res.latitude,res.longitude,1,5);//wgs84转为gcj02显示在百度地图上
  122. var latlon= res.latitude+','+res.longitude;
  123. that.getAreaCode(latlon);
  124. //that.gcj02towgs(res.latitude,res.longitude);//gcj02定位查询
  125. },
  126. fail(err) {
  127. }
  128. });
  129. },
  130. movetoCenter(latitude,longitude,sLb){
  131. console.log(longitude)
  132. this.mapready=false;
  133. this.latitude=latitude;
  134. this.longitude=longitude;
  135. setTimeout(()=>{
  136. this.mapready = true;
  137. },500);
  138. // return;
  139. // var label=''
  140. // if(sLb){
  141. // label={//自定义标记点上方的气泡窗口
  142. // content: latitude+','+longitude,//文本
  143. // color:"#ffffff",//文本颜色
  144. // fontSize:12,//文字大小
  145. // padding:3,//文本边缘留白
  146. // textAlign:"center",//文本对齐方式。有效值: left, right, center
  147. // };
  148. // }
  149. // else{
  150. // label={//自定义标记点上方的气泡窗口
  151. // content: latitude,//文本
  152. // color:"#ffffff",//文本颜色
  153. // fontSize:12,//文字大小
  154. // padding:3,//文本边缘留白
  155. // textAlign:"center",//文本对齐方式。有效值: left, right, center
  156. // };
  157. // }
  158. // // if(!this.showLb){
  159. // // label='';
  160. // // }
  161. // this.covers=[{
  162. // id:1,//控件id
  163. // iconPath:'/static/images/Rdizhil60@3x.png',//显示的图标
  164. // latitude:latitude,
  165. // longitude:longitude,
  166. // label:label,
  167. // }];
  168. },
  169. getAreaCode(latlng){
  170. var userId = uni.getStorageSync('userId');
  171. var that = this;
  172. api('getLocationAddr',{
  173. latlng:latlng,
  174. type:this.type,
  175. id:userId
  176. },r=>{
  177. if(r.data.code!=200){
  178. return;
  179. }
  180. that.dowithAddr(r.data.data);
  181. that.scrollTop=0;
  182. },failc=>{
  183. //console.log('getadvertis----',failc)
  184. })
  185. },
  186. dowithAddr(r){
  187. console.log(r);
  188. if(this.type==0){
  189. if(r.result.pois.length>0){
  190. this.pois=r.result.pois;
  191. this.markAddr=r.result.formatted_address;
  192. }
  193. }
  194. else if(this.type==1){
  195. if(r.results.length>0){
  196. this.pois=r.results;
  197. this.markAddr=this.pois[1].vicinity;
  198. }
  199. }
  200. },
  201. regionChange(){
  202. var that = this;
  203. if(!this.mapready){
  204. return;
  205. }
  206. this.mapContext.getCenterLocation({
  207. success: res => {//经纬度格式是gcj02
  208. console.log('getCenterLocation',res)
  209. that.movetoCenter(res.latitude,res.longitude,false);
  210. var tap={
  211. point:{
  212. x:res.latitude,
  213. y:res.longitude
  214. },
  215. addr:'',
  216. name:''
  217. }
  218. that.bdtogps(tap,2)
  219. //this.gcj02towgs(res.latitude,res.longitude);
  220. },
  221. fail: res => {
  222. }
  223. })
  224. },
  225. controlstp(tap){
  226. if(tap.detail.controlId==2){
  227. this.gotoLocat();
  228. }
  229. },
  230. markertap(tap){
  231. var addr;
  232. if(this.type==0){
  233. this.bdtogps(tap,1);
  234. }
  235. else if(this.type==1){
  236. addr={
  237. address: tap.vicinity,
  238. name:tap.name,
  239. longitude:tap.geometry.location.lng,
  240. latitude: tap.geometry.location.lat,
  241. }
  242. uni.setStorageSync('tempLocation',addr);
  243. uni.setStorageSync('userAddrLocation',addr);
  244. uni.navigateBack();
  245. }
  246. },
  247. chooseCancel(){
  248. this.showpop=false;
  249. },
  250. addrselect(tap){
  251. var addr;
  252. if(this.type==0){
  253. this.bdtogps(tap,0);
  254. }
  255. else if(this.type==1){
  256. addr={
  257. address: tap.vicinity,
  258. name:tap.name,
  259. longitude:tap.geometry.location.lng,
  260. latitude: tap.geometry.location.lat,
  261. }
  262. uni.setStorageSync('tempLocation',addr);
  263. uni.setStorageSync('userAddrLocation',addr);
  264. if(this.chakanzb){
  265. uni.redirectTo({
  266. url:'/pages/UserCenter/dizhi/newdizhi?id=-1'
  267. });
  268. }
  269. else{
  270. uni.navigateBack();
  271. }
  272. }
  273. },
  274. setCenter(){
  275. //console.log('setCenter',this.left,this.top)
  276. this.controls=[
  277. {//在地图上显示控件,控件不随着地图移动
  278. id:1,//控件id
  279. iconPath:'/static/images/Rdizhil@3x.png',//显示的图标
  280. position:{//控件在地图的位置
  281. left:this.left/4-20,
  282. top:150-36,
  283. width:36,
  284. height:36
  285. },
  286. },
  287. {//在地图上显示控件,控件不随着地图移动
  288. id:2,//控件id
  289. iconPath:'/static/images/dingwei.png',//显示的图标
  290. clickable:true,
  291. position:{//控件在地图的位置
  292. left:320,
  293. top:260,
  294. width:28,
  295. height:28
  296. },
  297. }];
  298. },
  299. gcj02towgs(latitude,longitude){
  300. api('gcj02towgs',{
  301. lng:longitude,
  302. lat:latitude
  303. },r=>{
  304. let str = r.data.data;
  305. let s = str.split(',');
  306. if(s.length>1){
  307. var latlon= s[1]+','+s[0];
  308. //console.log('----1',latlon);
  309. this.getAreaCode(latlon);
  310. }
  311. },failc=>{
  312. //console.log('getadvertis----',failc)
  313. })
  314. },
  315. querendizhi(index){
  316. var addr;
  317. var poi=this.pois[index];
  318. if(this.type==0){
  319. this.movetoCenter(poi.point.y,poi.point.x,false);
  320. //this.setLocat(poi.point.y,poi.point.x,5,3);//百度API查询结果bd09ll转为gcj02显示在百度地图上
  321. }
  322. else if(this.type==1){
  323. this.setLocat(poi.geometry.location.lat,poi.geometry.location.lng,1,5);//谷歌返回GPS转为转为gcj02显示在百度地图上
  324. }
  325. return;
  326. },
  327. setLocat(latitude,longitude,f,t){
  328. var latlng = longitude+','+latitude;
  329. api('setLocat',{
  330. latlng:latlng,
  331. from:f,
  332. to:t
  333. },r=>{
  334. this.movetoCenter(r.data.data.result[0].y,r.data.data.result[0].x,false);
  335. },failc=>{
  336. //console.log('getadvertis----',failc)
  337. })
  338. },
  339. //bd09ll 转 GPS
  340. bdtogps(tap,type){
  341. var latlng = tap.point.x+','+tap.point.y;
  342. api('setLocat',{
  343. latlng:latlng,
  344. from:5,
  345. to:3
  346. },r=>{
  347. console.log('bdtogps1:',r);
  348. //this.movetoCenter(r.data.data.result[0].y,r.data.data.result[0].x,false);
  349. api('gcj02towgs',{
  350. lng:r.data.data.result[0].x,
  351. lat:r.data.data.result[0].y
  352. },res=>{
  353. console.log('bdtogps2:',res);
  354. var s = res.data.data.split(',');
  355. if(type==2){
  356. var latlon= s[1]+','+s[0];
  357. //console.log('----1',latlon);
  358. this.getAreaCode(latlon);
  359. return;
  360. }
  361. var addr={//bd09ll 转 GPS
  362. address:tap.addr,
  363. name:tap.name,
  364. longitude:s[0],
  365. latitude:s[1],
  366. }
  367. uni.setStorageSync('tempLocation',addr);
  368. uni.setStorageSync('userAddrLocation',addr);
  369. if(type==0){
  370. if(this.chakanzb){
  371. uni.redirectTo({
  372. url:'/pages/UserCenter/dizhi/newdizhi?id=-1'
  373. });
  374. }
  375. else{
  376. uni.navigateBack();
  377. }
  378. }
  379. else if(type==1){
  380. uni.navigateBack();
  381. }
  382. },failc=>{
  383. //console.log('getadvertis----',failc)
  384. })
  385. },failc=>{
  386. //console.log('getadvertis----',failc)
  387. })
  388. },
  389. }
  390. }
  391. </script>
  392. <style>
  393. .mapbody{
  394. width: 100%;
  395. }
  396. .mapcontentV{
  397. margin-top: 20rpx;
  398. }
  399. .contensb{
  400. display: flex;
  401. flex-direction: column;
  402. justify-content:flex-start;
  403. }
  404. .itemContent{
  405. width: 90%;
  406. margin-left: 5%;
  407. padding: 10rpx;
  408. border-bottom-width: 2rpx;
  409. border-bottom-style:outset;
  410. border-bottom-color:lightgray;
  411. align-items: center;
  412. }
  413. .addrName{
  414. width: 100%;
  415. margin-left:10rpx;
  416. font-size: 32rpx;
  417. color: black;
  418. height: 36rpx;
  419. line-height: 36rpx;
  420. overflow: hidden;
  421. text-overflow: ellipsis;
  422. display: -webkit-box;
  423. -webkit-line-clamp: 1;
  424. -webkit-box-orient: vertical;
  425. }
  426. .addrNamedetlV{
  427. display: flex;
  428. width: 100%;
  429. margin-left: 10rpx;
  430. height: 100rpx;
  431. align-items: center;
  432. }
  433. .addrNamedetl{
  434. text-align: left;
  435. width: 100%;
  436. margin-left: 10rpx;
  437. font-size: 28rpx;
  438. color:darkslategrey;
  439. padding: 10rpx;
  440. }
  441. .popocontenv{
  442. background-color: cornflowerblue;
  443. border-radius: 16rpx 16rpx 0rpx 0rpx;
  444. }
  445. .itemViewTop{
  446. display: flex;
  447. background-color:whitesmoke;
  448. width: 100%;
  449. padding: 10rpx;
  450. height: 70rpx;
  451. justify-content: center;
  452. align-items: center;
  453. text-align: center;
  454. box-shadow: 0rpx 0rpx -10rpx 0rpx #E02F73;
  455. }
  456. .itemTextTop{
  457. text-align: center;
  458. font-size: 32rpx;
  459. color: black;
  460. }
  461. .itemView{
  462. width: 100%;
  463. padding: 10rpx;
  464. height: 100rpx;
  465. background-color: white;
  466. text-align: center;
  467. }
  468. .itemTextA{
  469. width: 80%;
  470. height: 70rpx;
  471. border-radius: 20rpx;
  472. line-height: 70rpx;
  473. text-align: center;
  474. font-size: 32rpx;
  475. color: white;
  476. background-color: #E02F73;
  477. box-shadow: 0rpx 0rpx 10rpx 0rpx #E02F73;
  478. }
  479. .itemTextB{
  480. width: 80%;
  481. height: 70rpx;
  482. border-radius: 20rpx;
  483. line-height: 70rpx;
  484. text-align: center;
  485. font-size: 32rpx;
  486. color: white;
  487. background-color: sandybrown;
  488. box-shadow: 0rpx 0rpx 10rpx 0rpx sandybrown;
  489. }
  490. </style>