Chargermap.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. <template>
  2. <view>
  3. <view id="container" :style="myStyle"></view>
  4. <u-icon name="map-fill" v-if="false" class="input-card" style="width: 24rem;top: 130px;width: 20px;height: 20px;" color="#999" size="40"></u-icon>
  5. </view>
  6. </template>
  7. <script>
  8. import MapLoader from '@/utils/AMap'
  9. export default {
  10. name:"Chargermap",
  11. data() {
  12. return {
  13. myStyle:"width: 100%; height: 1200rpx;",
  14. chargerIcon:'',
  15. chargerSelectedIcon:'',
  16. personIcon:'',
  17. longitude: 112.276527,
  18. latitude: 30.306427,
  19. AMap:null,
  20. //车的位置
  21. car: {
  22. obj:null,
  23. longitude: '',
  24. latitude: '',
  25. },
  26. down: {
  27. obj:null,
  28. longitude: '',
  29. latitude: '',
  30. },
  31. up: {
  32. obj:null,
  33. longitude: '',
  34. latitude: '',
  35. },
  36. //个人
  37. info: {
  38. obj:null,
  39. init: false,
  40. longitude: '',
  41. latitude: '',
  42. },
  43. mapcharger:null,
  44. startend: [{
  45. longitude:112.279274,
  46. latitude:30.303273,
  47. name:''
  48. }, {
  49. longitude:112.273867,
  50. latitude:30.309817,
  51. name:''
  52. }],
  53. start:{},
  54. end:{},
  55. chargerList:[],
  56. covers: [
  57. {
  58. longitude:112.273867,
  59. latitude:30.309817,
  60. },{
  61. longitude:112.280261,
  62. latitude:30.3041,
  63. },{
  64. longitude:112.279274,
  65. latitude:30.303273,
  66. }
  67. ],
  68. };
  69. },
  70. methods:{
  71. getLocation1() {
  72. // console.log('getLocation');
  73. const self = this;
  74. AMap.plugin('AMap.Geolocation', function () {
  75. var geolocation = new AMap.Geolocation({
  76. // 是否使用高精度定位,默认:true
  77. enableHighAccuracy: true,
  78. // 设置定位超时时间,默认:无穷大
  79. timeout: 10000
  80. });
  81. geolocation.getCurrentPosition();
  82. AMap.event.addListener(geolocation, 'complete', onComplete);
  83. AMap.event.addListener(geolocation, 'error', onError);
  84. function onComplete (data) {
  85. // data是具体的定位信息
  86. self.$emit('location',data);
  87. console.log('定位成功信息:', data.addressComponent.city);
  88. self.city = data.addressComponent.city;
  89. }
  90. function onError (data) {
  91. // 定位出错
  92. // console.log('定位失败错误:', data);
  93. // 调用IP定位
  94. let ret = { position:{longitude: 112.28468,latitude: 30.307094}};
  95. self.$emit('location',ret);
  96. //self.getLngLatLocation();
  97. }
  98. });
  99. },
  100. // 通过IP获取当前位置
  101. getLngLatLocation () {
  102. const self = this;
  103. AMap.plugin('AMap.CitySearch', function () {
  104. var citySearch = new AMap.CitySearch();
  105. citySearch.getLocalCity(function (status, result) {
  106. if (status === 'complete' && result.info === 'OK') {
  107. // 查询成功,result即为当前所在城市信息
  108. //console.log('通过ip获取当前城市:', result);
  109. // 逆向地理编码
  110. AMap.plugin('AMap.Geocoder', function () {
  111. var geocoder = new AMap.Geocoder({
  112. // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
  113. city: result.adcode
  114. });
  115. var lnglat = result.rectangle.split(';')[0].split(',');
  116. geocoder.getAddress(lnglat, function (status, data) {
  117. if (status === 'complete' && data.info === 'OK') {
  118. // result为对应的地理位置详细信息
  119. //self.$emit('location',data);
  120. console.log('location'+JSON.stringify(data));
  121. }
  122. });
  123. });
  124. }
  125. });
  126. });
  127. },
  128. searchBtn(startLngLat,endLst){
  129. var _this = this;
  130. AMap.plugin('AMap.Driving', function() {
  131. var driving = new AMap.Driving({
  132. // 驾车路线规划策略,AMap.DrivingPolicy.LEAST_TIME是最快捷模式
  133. policy: AMap.DrivingPolicy.LEAST_TIME
  134. })
  135. //var startLngLat = [_this.longitude, _this.latitude]
  136. //var endLngLat = [116.427281, 39.903719]
  137. for(let i=0;i< endLst.length;i++)
  138. {
  139. let item = endLst[i];
  140. let endLngLat = [item.longitude,item.latitude]
  141. console.log('search start['+JSON.stringify(startLngLat))
  142. console.log('search end]'+JSON.stringify(endLngLat))
  143. driving.search(startLngLat, endLngLat, function (status, result) {
  144. console.log(result)
  145. console.log('秒'+result.routes[0].time)//秒
  146. console.log('距离'+result.routes[0].distance)//米
  147. //uni.showToast({
  148. // title:result.routes[0].time+"秒,"+result.routes[0].distance+"米"
  149. //})
  150. endLst[i].distance = result.routes[0].distance/1000.0;
  151. endLst[i].time = result.routes[0].time/60.0;
  152. })
  153. //var startLngLat2 = [_this.longitude, _this.latitude]
  154. //var endLngLat2 = [_this.longitude2, _this.latitude2]
  155. /*
  156. driving.search(startLngLat2, endLngLat2, function (status, result) {
  157. console.log(result)
  158. console.log(result.routes[0].time)//秒
  159. console.log(result.routes[0].distance)//米
  160. uni.showToast({
  161. title:result.routes[0].time+"秒,"+result.routes[0].distance+"米"
  162. })
  163. })*/
  164. }
  165. })
  166. },
  167. updateCharger(pos){
  168. // console.log('更新充电桩1'+JSON.stringify(pos))
  169. // console.log('更新充电桩2'+JSON.stringify(this.chargerList[0].info))
  170. // let index = this.chargerList.findIndex(item => item.info.latitude === pos.latitude && item.info.longitude === pos.longitude);
  171. //let index = this.chargerList.findIndex(item => (Math.abs(item.info.longitude-pos.longitude)<0.000001 && Math.abs(item.info.longitude - pos.latitude)<0.000001));
  172. let index = this.chargerList.findIndex(item => item.info.id === pos.id);
  173. console.log('更新充电桩['+ index + ']'+JSON.stringify(pos))
  174. if(index>=0){
  175. let station_icon2 = "width: 100px;height: 36px;line-height: 20px;border-radius: 50px;background-color: rgba(0, 185, 98, 100);text-align: center;display: flex;";
  176. let corner2=" width: 0;height: 0;position: absolute;top: 36px;left: 0;right: 0;margin: auto;border-bottom: 6px solid transparent;border-left: 6px solid transparent;border-right: 6px solid transparent;border-top: 8px solid #00b962 ;";
  177. let content = "<div style='" + station_icon2 + "'>";
  178. content+="<div style='width: 36px;height: 36px;line-height: 36px;text-align: center;border: 1px solid rgba(0, 185, 98, 100);border-radius: 999px;background-color: #fff;color: #00b962;' class='iconfont'>&#xe606;</div>";
  179. content+="<div style='color: #ffffff;line-height: 14px;padding: 4px;'>";
  180. content+="<div style='"+corner2+"'></div>";
  181. content+="<div class=''>";
  182. content+= "¥" + pos.price.toString();
  183. content+="</div>";
  184. content+="<div class=''>";
  185. content+="空闲" + pos.idleNum.toString();
  186. content+="</div>"
  187. content+="</div>";
  188. content+="</div>";
  189. // console.log('找到充电桩[]'+content)
  190. //content = "<div style='height: 40px;width: 100px;display: flex;flex-direction: row; flex-wrap: wrap; background-color: #00B962!important;border-radius: 20px;'> <div><img src='"+this.chargerSelectedIcon+"' style='idth: 40px;height: 40px'></div><div style='display: flex;flex-direction:column;color:#FFFFFF;'><div style='margin-left:10px'>¥"+ pos.price +"</div><div style='margin-left:10px'>空闲"+pos.idleNum + "</div> </div></div>";
  191. this.chargerList[index].info.selected = true;
  192. this.chargerList[index].marker.setContent(content);
  193. }
  194. for(let i = 0;i < this.chargerList.length;i++){
  195. if(index == i)
  196. continue;
  197. if(this.chargerList[i].info.selected != null &&
  198. this.chargerList[i].info.selected)
  199. {
  200. let station_icon = "width: 36px;height: 36px;line-height: 36px;text-align: center;border: 1px solid rgba(133, 140, 255, 100); color: #b58cff; border-radius: 999px;";
  201. let corner = "width: 0;height: 0;position: absolute;top: 34px;left: 0;right: 0;margin: auto;border-bottom: 4px solid transparent;border-left: 4px solid transparent;border-right: 4px solid transparent;border-top: 6px solid #b58cff ;";
  202. let content = "<div style='"+station_icon+"'><text class='iconfont'>&#xe606;</text><div style='" + corner+"'></div></div>"
  203. //let content = "<img src='"+this.chargerIcon+"' style='height: 40px;width: 40px' />";
  204. this.chargerList[i].marker.setContent(content);
  205. this.chargerList[i].info.selected = false;
  206. console.log('restore'+JSON.stringify(this.chargerList[i].info))
  207. }
  208. }
  209. },
  210. addCharger(pos) {
  211. // console.log('添加充电桩'+JSON.stringify(pos))
  212. //let content = "<div style='height: 40px;width: 100px;display: flex;flex-direction: row; flex-wrap: wrap; background-color: #00B962!important;border-radius: 20px;'> <div><img src='"+this.chargerIcon+"' style='idth: 40px;height: 40px'></div><div style='display: flex;flex-direction:column;color:#FFFFFF'><div>¥1.5</div><div>空闲10</div> </div></div>";
  213. let station_icon = "width: 36px;height: 36px;line-height: 36px;text-align: center;border: 1px solid rgba(133, 140, 255, 100); color: #b58cff; border-radius: 999px;";
  214. let corner = "width: 0;height: 0;position: absolute;top: 34px;left: 0;right: 0;margin: auto;border-bottom: 4px solid transparent;border-left: 4px solid transparent;border-right: 4px solid transparent;border-top: 6px solid #b58cff ;";
  215. let content = "<div style='"+station_icon+"'><text class='iconfont'>&#xe606;</text><div style='" + corner+"'></div></div>"
  216. let marker = new AMap.Marker({
  217. content: content,//"<img src='"+this.chargerIcon+"' style='height: 40px;width: 40px' />",
  218. //icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
  219. position: [pos.longitude,pos.latitude],
  220. offset: new AMap.Pixel(-13, -30),
  221. zIndex:900,
  222. autoRotation: true,
  223. });
  224. marker.setMap(this.mapcharger);
  225. this.myEmit("charger",marker,pos)
  226. this.chargerList.push({marker:marker,info:pos});
  227. },
  228. addPerson(pos) {
  229. console.log('addPerson'+JSON.stringify(pos))
  230. let marker = new AMap.Marker({
  231. content: "<img src='"+this.personIcon+"' style='height: 40px;width: 40px' />",
  232. //icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
  233. position: [pos.longitude,pos.latitude],
  234. offset: new AMap.Pixel(-13, -30),
  235. zIndex:900,
  236. autoRotation: true,
  237. });
  238. marker.setMap(this.mapcharger);
  239. this.myEmit("person",marker,pos)
  240. //this.chargerList.push(marker)
  241. },
  242. setChargerList(sz){
  243. if(this.AMap==null){
  244. return
  245. }
  246. //this.getLocation();
  247. for(var i in this.chargerList){
  248. this.mapcharger.remove(this.chargerList[i]);
  249. }
  250. this.chargerList=[]
  251. for(var i in sz){
  252. var ob=sz[i];
  253. this.addCharger(ob);
  254. /*var ob1 = new AMap.Marker({
  255. position: new AMap.LngLat(ob.longitude,ob.latitude), // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
  256. title: '北京'
  257. });
  258. this.mapcharger.add(ob1);*/
  259. /*
  260. var ob1=new AMap.Marker({
  261. map: this.mapcharger,
  262. position: [ob.longitude,ob.latitude],
  263. //offset: new AMap.Pixel(-5, -5),
  264. //offset: new AMap.Pixel(-18, -40),
  265. content:"<img src='"+icon0+"' style='height: 40px;width: 40px' />",
  266. title:'test111111111111111',
  267. zIndex:900,
  268. autoRotation: true,
  269. });
  270. this.myEmit("car",ob1,ob)
  271. */
  272. //this.mapcharger.add(ob1);
  273. }
  274. },
  275. myEmit(type,ob1,obj){
  276. var _this=this;
  277. AMap.event.addListener(ob1, 'click', function(e) {
  278. _this.$emit('clickMap',{
  279. type:type,
  280. obj:obj
  281. })
  282. })
  283. },
  284. setMyStyle(s){
  285. this.myStyle=s;
  286. },
  287. setCenter(item){
  288. if(this.mapcharger){
  289. this.mapcharger.setCenter([item.longitude,item.latitude]);
  290. return true
  291. }else{
  292. return false
  293. }
  294. },
  295. setPerson(ob,bl){
  296. // console.log('setPerson1'+JSON.stringify(ob))
  297. if(this.AMap==null){
  298. return
  299. }
  300. // console.log('setPerson2')
  301. this.info.longitude=ob.longitude
  302. this.info.latitude=ob.latitude
  303. if(!this.info.ob){
  304. //this.addPerson(ob)
  305. //this.info.ob.setMap(this.mapcharger);
  306. //console.log('setMap xxx ')
  307. //this.mapbus.setCenter([this.info.longitude,this.info.latitude]); //设置地图中心点
  308. if(!bl){
  309. let posCenter= [ this.info.longitude,this.info.latitude];
  310. // console.log('center'+JSON.stringify(posCenter))
  311. //this.mapcharger.setZoom(9)
  312. //this.mapcharger.setCenter([112.28541,30.308354])
  313. //this.mapcharger.setCenter([121.504919,31.351083])
  314. this.mapcharger.setCenter(posCenter); //设置地图中心点
  315. }
  316. }else{
  317. // this.info.ob.setPosition( [this.info.longitude,this.info.latitude]);
  318. }
  319. },
  320. logMapInfo() {
  321. var posCenter = this.mapcharger.getCenter();
  322. //console.log('center'+JSON.stringify(posCenter));
  323. var limitBounds = this.mapcharger.getBounds();
  324. let pos = {center:posCenter,bounds:limitBounds};
  325. return pos;
  326. },
  327. init(){
  328. // console.log('init')
  329. var _this = this;
  330. // this.personIcon = require("@/static/img/charger_selected.png")
  331. // this.chargerIcon = require("@/static/img/charger.png");
  332. // this.chargerSelectedIcon = require("@/static/img/charger_selected_circle.png");
  333. MapLoader().then(AMap => {
  334. this.AMap=AMap;
  335. _this.mapcharger && _this.mapcharger.destroy();
  336. //使用地图容器ID创建
  337. _this.mapcharger=new AMap.Map("container", {
  338. resizeEnable: true,
  339. dragEnable : true,
  340. center: [this.longitude, this.latitude],
  341. zoom: 14
  342. });
  343. //var clickHandler = function(e) {
  344. // console.log('您在[ '+e.lnglat.getLng()+','+e.lnglat.getLat()+' ]的位置点击了地图!');
  345. //};
  346. //_this.mapcharger.on('click', clickHandler);
  347. _this.$emit('onload')
  348. var mapMovestart = function(){
  349. console.log('movestart')
  350. }
  351. var mapMove = function(){
  352. //console.log('mapMove')
  353. }
  354. var mapMoveend = function(){
  355. let pos = _this.logMapInfo();
  356. _this.$emit('onMoveEnd',pos);
  357. // console.log('mapMoveend')
  358. }
  359. _this.mapcharger.on('movestart', mapMovestart);
  360. _this.mapcharger.on('mapmove', mapMove);
  361. _this.mapcharger.on('moveend', mapMoveend);
  362. }, e => {
  363. //_this.mui.toast('地图加载失败');
  364. console.log('地图加载失败', e)
  365. })
  366. },
  367. initMap(point,stationList,startend){
  368. console.log('初始化地图'+this.longitude+this.latitude)
  369. var _this = this;
  370. this.covers=point;
  371. this.startend=startend;
  372. MapLoader().then(AMap => {
  373. this.AMap=AMap;
  374. _this.mapcharger && _this.mapcharger.destroy();
  375. _this.mapcharger=new AMap.Map("container", {
  376. resizeEnable: true,
  377. center: [this.longitude, this.latitude],
  378. //center: [116.397428, 39.90923],
  379. zoom: 14
  380. });
  381. var lineArr=[]
  382. //var icon0 = require("@/static/img/map_0.png");
  383. //var icon1 = require("@/static/img/map_1.png");
  384. //var icon2 = require("@/static/img/map_2.png");
  385. /*this.start=new AMap.Marker({
  386. map: _this.mapcharger,
  387. position: [_this.startend[0].longitude,_this.startend[0].latitude],
  388. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  389. offset: new AMap.Pixel(-15, -30),
  390. zIndex:99,
  391. autoRotation: true,
  392. });
  393. this.end=new AMap.Marker({
  394. map: _this.mapcharger,
  395. position: [_this.startend[1].longitude,_this.startend[1].latitude],
  396. content:"<img src='"+icon2+"' style='height: 30px;width: 30px' />",
  397. offset: new AMap.Pixel(-15, -30),
  398. zIndex:99,
  399. autoRotation: true,
  400. });
  401. for(var i in _this.covers){
  402. var point=_this.covers[i]
  403. lineArr.push([point.longitude,point.latitude])
  404. }
  405. for(var i in stationList){
  406. var point=stationList[i]
  407. var ob_point =new AMap.Marker({
  408. map: _this.mapcharger,
  409. position: [point.longitude,point.latitude],
  410. offset: new AMap.Pixel(-5, -7),
  411. content:"<img src='"+icon1+"' style='height: 10px;width: 10px' />",
  412. zIndex:99,
  413. autoRotation: true,
  414. });
  415. this.myEmit("point",ob_point,point)
  416. }
  417. var polyline = new AMap.Polyline({
  418. map: _this.mapcharger,
  419. path: lineArr,
  420. showDir: true,
  421. strokeColor: "#28F", //线颜色
  422. // strokeOpacity: 1, //线透明度
  423. strokeWeight: 3, //线宽
  424. //strokeStyle: "solid" //线样式
  425. });*/
  426. this.$emit('onload')
  427. }, e => {
  428. //_this.mui.toast('地图加载失败');
  429. console.log('地图加载失败', e)
  430. })
  431. },
  432. }
  433. }
  434. </script>
  435. <style scoped>
  436. /*去除下标*/
  437. .amap-logo{
  438. display: none;
  439. }
  440. .amap-copyright{
  441. opacity:0;
  442. font-size: 1px;
  443. }
  444. .input-card {
  445. display: flex;
  446. flex-direction: column;
  447. min-width: 0;
  448. word-wrap: break-word;
  449. background-color: #fff;
  450. background-clip: border-box;
  451. border-radius: .25rem;
  452. width: 22rem;
  453. border-width: 0;
  454. border-radius: 0.4rem;
  455. box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
  456. position: fixed;
  457. bottom: 1rem;
  458. right: 1rem;
  459. -ms-flex: 1 1 auto;
  460. flex: 1 1 auto;
  461. padding: 0.75rem 0.75rem;
  462. }
  463. .station-icon {
  464. width: 36px;
  465. height: 36px;
  466. line-height: 36px;
  467. text-align: center;
  468. border: 1px solid rgba(133, 140, 255, 100);
  469. color: #b58cff;
  470. border-radius: 999px;
  471. position: absolute;
  472. top: 89px;
  473. left: 38px;
  474. }
  475. .corner{
  476. width: 0;
  477. height: 0;
  478. position: absolute;
  479. top: 34px;
  480. left: 0;
  481. right: 0;
  482. margin: auto;
  483. border-bottom: 4px solid transparent;
  484. border-left: 4px solid transparent;
  485. border-right: 4px solid transparent;
  486. border-top: 6px solid #b58cff ;
  487. }
  488. .station-icon2 {
  489. width: 100px;
  490. height: 36px;
  491. line-height: 20px;
  492. border-radius: 50px;
  493. background-color: rgba(0, 185, 98, 100);
  494. text-align: center;
  495. display: flex;
  496. position: absolute;
  497. top: 191px;
  498. left: 168px;
  499. }
  500. .icon2-left{
  501. width: 36px;
  502. height: 36px;
  503. line-height: 36px;
  504. text-align: center;
  505. border: 1px solid rgba(0, 185, 98, 100);
  506. border-radius: 999px;
  507. background-color: #fff;
  508. color: #00b962;
  509. }
  510. .icon2-right{
  511. color: #ffffff;
  512. line-height: 14px;
  513. padding: 4px;
  514. }
  515. .corner2{
  516. width: 0;
  517. height: 0;
  518. position: absolute;
  519. top: 36px;
  520. left: 0;
  521. right: 0;
  522. margin: auto;
  523. border-bottom: 6px solid transparent;
  524. border-left: 6px solid transparent;
  525. border-right: 6px solid transparent;
  526. border-top: 8px solid #00b962 ;
  527. }
  528. </style>