Chargermap.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. getLocation () {
  72. const self = this;
  73. AMap.plugin('AMap.Geolocation', function () {
  74. var geolocation = new AMap.Geolocation({
  75. // 是否使用高精度定位,默认:true
  76. enableHighAccuracy: true,
  77. // 设置定位超时时间,默认:无穷大
  78. timeout: 10000
  79. });
  80. geolocation.getCurrentPosition();
  81. AMap.event.addListener(geolocation, 'complete', onComplete);
  82. AMap.event.addListener(geolocation, 'error', onError);
  83. function onComplete (data) {
  84. // data是具体的定位信息
  85. console.log('定位成功信息:', data.addressComponent.city);
  86. self.city = data.addressComponent.city;
  87. }
  88. function onError (data) {
  89. // 定位出错
  90. console.log('定位失败错误:', data);
  91. // 调用IP定位
  92. self.getLngLatLocation();
  93. }
  94. });
  95. },
  96. // 通过IP获取当前位置
  97. getLngLatLocation () {
  98. AMap.plugin('AMap.CitySearch', function () {
  99. var citySearch = new AMap.CitySearch();
  100. citySearch.getLocalCity(function (status, result) {
  101. if (status === 'complete' && result.info === 'OK') {
  102. // 查询成功,result即为当前所在城市信息
  103. console.log('通过ip获取当前城市:', result);
  104. // 逆向地理编码
  105. AMap.plugin('AMap.Geocoder', function () {
  106. var geocoder = new AMap.Geocoder({
  107. // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
  108. city: result.adcode
  109. });
  110. var lnglat = result.rectangle.split(';')[0].split(',');
  111. geocoder.getAddress(lnglat, function (status, data) {
  112. if (status === 'complete' && data.info === 'OK') {
  113. // result为对应的地理位置详细信息
  114. console.log(data);
  115. }
  116. });
  117. });
  118. }
  119. });
  120. });
  121. },
  122. updateCharger(pos){
  123. // console.log('更新充电桩1'+JSON.stringify(pos))
  124. // console.log('更新充电桩2'+JSON.stringify(this.chargerList[0].info))
  125. let index = this.chargerList.findIndex(item => item.info.latitude === pos.latitude && item.info.longitude === pos.longitude);
  126. //let index = this.chargerList.findIndex(item => (Math.abs(item.info.longitude-pos.longitude)<0.000001 && Math.abs(item.info.longitude - pos.latitude)<0.000001));
  127. if(index>=0){
  128. 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;";
  129. 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 ;";
  130. let content = "<div style='" + station_icon2 + "'>";
  131. 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>";
  132. content+="<div style='color: #ffffff;line-height: 14px;padding: 4px;'>";
  133. content+="<div style='"+corner2+"'></div>";
  134. content+="<div class=''>";
  135. content+= "¥" + pos.price.toString();
  136. content+="</div>";
  137. content+="<div class=''>";
  138. content+="空闲" + pos.idleNum.toString();
  139. content+="</div>"
  140. content+="</div>";
  141. content+="</div>";
  142. console.log('找到充电桩[]'+content)
  143. //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.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>";
  144. this.chargerList[index].info.selected = true;
  145. this.chargerList[index].marker.setContent(content);
  146. }
  147. for(let i = 0;i < this.chargerList.length;i++){
  148. if(index == i)
  149. continue;
  150. if(this.chargerList[i].info.selected != null &&
  151. this.chargerList[i].info.selected)
  152. {
  153. 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;";
  154. 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 ;";
  155. let content = "<div style='"+station_icon+"'><text class='iconfont'>&#xe606;</text><div style='" + corner+"'></div></div>"
  156. //let content = "<img src='"+this.chargerIcon+"' style='height: 40px;width: 40px' />";
  157. this.chargerList[i].marker.setContent(content);
  158. this.chargerList[i].info.selected = false;
  159. console.log('restore'+JSON.stringify(this.chargerList[i].info))
  160. }
  161. }
  162. },
  163. addCharger(pos) {
  164. // console.log('添加充电桩'+JSON.stringify(pos))
  165. //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>";
  166. 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;";
  167. 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 ;";
  168. let content = "<div style='"+station_icon+"'><text class='iconfont'>&#xe606;</text><div style='" + corner+"'></div></div>"
  169. let marker = new AMap.Marker({
  170. content: content,//"<img src='"+this.chargerIcon+"' style='height: 40px;width: 40px' />",
  171. //icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
  172. position: [pos.longitude,pos.latitude],
  173. offset: new AMap.Pixel(-13, -30),
  174. zIndex:900,
  175. autoRotation: true,
  176. });
  177. marker.setMap(this.mapcharger);
  178. this.myEmit("charger",marker,pos)
  179. this.chargerList.push({marker:marker,info:pos});
  180. },
  181. addPerson(pos) {
  182. console.log('addPerson'+JSON.stringify(pos))
  183. let marker = new AMap.Marker({
  184. content: "<img src='"+this.personIcon+"' style='height: 40px;width: 40px' />",
  185. //icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
  186. position: [pos.longitude,pos.latitude],
  187. offset: new AMap.Pixel(-13, -30),
  188. zIndex:900,
  189. autoRotation: true,
  190. });
  191. marker.setMap(this.mapcharger);
  192. this.myEmit("person",marker,pos)
  193. //this.chargerList.push(marker)
  194. },
  195. setChargerList(sz){
  196. if(this.AMap==null){
  197. return
  198. }
  199. //this.getLocation();
  200. for(var i in this.chargerList){
  201. this.mapcharger.remove(this.chargerList[i]);
  202. }
  203. this.chargerList=[]
  204. for(var i in sz){
  205. var ob=sz[i];
  206. this.addCharger(ob);
  207. /*var ob1 = new AMap.Marker({
  208. position: new AMap.LngLat(ob.longitude,ob.latitude), // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
  209. title: '北京'
  210. });
  211. this.mapcharger.add(ob1);*/
  212. /*
  213. var ob1=new AMap.Marker({
  214. map: this.mapcharger,
  215. position: [ob.longitude,ob.latitude],
  216. //offset: new AMap.Pixel(-5, -5),
  217. //offset: new AMap.Pixel(-18, -40),
  218. content:"<img src='"+icon0+"' style='height: 40px;width: 40px' />",
  219. title:'test111111111111111',
  220. zIndex:900,
  221. autoRotation: true,
  222. });
  223. this.myEmit("car",ob1,ob)
  224. */
  225. //this.mapcharger.add(ob1);
  226. }
  227. },
  228. myEmit(type,ob1,obj){
  229. var _this=this;
  230. AMap.event.addListener(ob1, 'click', function(e) {
  231. _this.$emit('clickMap',{
  232. type:type,
  233. obj:obj
  234. })
  235. })
  236. },
  237. setMyStyle(s){
  238. this.myStyle=s;
  239. },
  240. setCenter(item){
  241. if(this.mapcharger){
  242. this.mapcharger.setCenter([item.longitude,item.latitude]);
  243. return true
  244. }else{
  245. return false
  246. }
  247. },
  248. setPerson(ob,bl){
  249. // console.log('setPerson1'+JSON.stringify(ob))
  250. if(this.AMap==null){
  251. return
  252. }
  253. // console.log('setPerson2')
  254. this.info.longitude=ob.longitude
  255. this.info.latitude=ob.latitude
  256. if(!this.info.ob){
  257. //this.addPerson(ob)
  258. //this.info.ob.setMap(this.mapcharger);
  259. //console.log('setMap xxx ')
  260. //this.mapbus.setCenter([this.info.longitude,this.info.latitude]); //设置地图中心点
  261. if(!bl){
  262. let posCenter= [ this.info.longitude,this.info.latitude];
  263. // console.log('center'+JSON.stringify(posCenter))
  264. //this.mapcharger.setZoom(9)
  265. //this.mapcharger.setCenter([112.28541,30.308354])
  266. //this.mapcharger.setCenter([121.504919,31.351083])
  267. this.mapcharger.setCenter(posCenter); //设置地图中心点
  268. }
  269. }else{
  270. // this.info.ob.setPosition( [this.info.longitude,this.info.latitude]);
  271. }
  272. },
  273. init(){
  274. console.log('init')
  275. var _this = this;
  276. // this.personIcon = require("@/static/img/charger_selected.png")
  277. // this.chargerIcon = require("@/static/img/charger.png");
  278. // this.chargerSelectedIcon = require("@/static/img/charger_selected_circle.png");
  279. MapLoader().then(AMap => {
  280. this.AMap=AMap;
  281. _this.mapcharger && _this.mapcharger.destroy();
  282. //使用地图容器ID创建
  283. _this.mapcharger=new AMap.Map("container", {
  284. resizeEnable: true,
  285. center: [this.longitude, this.latitude],
  286. zoom: 14
  287. });
  288. var clickHandler = function(e) {
  289. alert('您在[ '+e.lnglat.getLng()+','+e.lnglat.getLat()+' ]的位置点击了地图!');
  290. };
  291. _this.mapcharger.on('click', clickHandler);
  292. _this.$emit('onload')
  293. }, e => {
  294. //_this.mui.toast('地图加载失败');
  295. console.log('地图加载失败', e)
  296. })
  297. },
  298. initMap(point,stationList,startend){
  299. console.log('初始化地图'+this.longitude+this.latitude)
  300. var _this = this;
  301. this.covers=point;
  302. this.startend=startend;
  303. MapLoader().then(AMap => {
  304. this.AMap=AMap;
  305. _this.mapcharger && _this.mapcharger.destroy();
  306. _this.mapcharger=new AMap.Map("container", {
  307. resizeEnable: true,
  308. center: [this.longitude, this.latitude],
  309. //center: [116.397428, 39.90923],
  310. zoom: 14
  311. });
  312. var lineArr=[]
  313. //var icon0 = require("@/static/img/map_0.png");
  314. //var icon1 = require("@/static/img/map_1.png");
  315. //var icon2 = require("@/static/img/map_2.png");
  316. /*this.start=new AMap.Marker({
  317. map: _this.mapcharger,
  318. position: [_this.startend[0].longitude,_this.startend[0].latitude],
  319. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  320. offset: new AMap.Pixel(-15, -30),
  321. zIndex:99,
  322. autoRotation: true,
  323. });
  324. this.end=new AMap.Marker({
  325. map: _this.mapcharger,
  326. position: [_this.startend[1].longitude,_this.startend[1].latitude],
  327. content:"<img src='"+icon2+"' style='height: 30px;width: 30px' />",
  328. offset: new AMap.Pixel(-15, -30),
  329. zIndex:99,
  330. autoRotation: true,
  331. });
  332. for(var i in _this.covers){
  333. var point=_this.covers[i]
  334. lineArr.push([point.longitude,point.latitude])
  335. }
  336. for(var i in stationList){
  337. var point=stationList[i]
  338. var ob_point =new AMap.Marker({
  339. map: _this.mapcharger,
  340. position: [point.longitude,point.latitude],
  341. offset: new AMap.Pixel(-5, -7),
  342. content:"<img src='"+icon1+"' style='height: 10px;width: 10px' />",
  343. zIndex:99,
  344. autoRotation: true,
  345. });
  346. this.myEmit("point",ob_point,point)
  347. }
  348. var polyline = new AMap.Polyline({
  349. map: _this.mapcharger,
  350. path: lineArr,
  351. showDir: true,
  352. strokeColor: "#28F", //线颜色
  353. // strokeOpacity: 1, //线透明度
  354. strokeWeight: 3, //线宽
  355. //strokeStyle: "solid" //线样式
  356. });*/
  357. this.$emit('onload')
  358. }, e => {
  359. //_this.mui.toast('地图加载失败');
  360. console.log('地图加载失败', e)
  361. })
  362. },
  363. }
  364. }
  365. </script>
  366. <style scoped>
  367. /*去除下标*/
  368. .amap-logo{
  369. display: none;
  370. }
  371. .amap-copyright{
  372. opacity:0;
  373. font-size: 1px;
  374. }
  375. .input-card {
  376. display: flex;
  377. flex-direction: column;
  378. min-width: 0;
  379. word-wrap: break-word;
  380. background-color: #fff;
  381. background-clip: border-box;
  382. border-radius: .25rem;
  383. width: 22rem;
  384. border-width: 0;
  385. border-radius: 0.4rem;
  386. box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
  387. position: fixed;
  388. bottom: 1rem;
  389. right: 1rem;
  390. -ms-flex: 1 1 auto;
  391. flex: 1 1 auto;
  392. padding: 0.75rem 0.75rem;
  393. }
  394. .station-icon {
  395. width: 36px;
  396. height: 36px;
  397. line-height: 36px;
  398. text-align: center;
  399. border: 1px solid rgba(133, 140, 255, 100);
  400. color: #b58cff;
  401. border-radius: 999px;
  402. position: absolute;
  403. top: 89px;
  404. left: 38px;
  405. .corner{
  406. width: 0;
  407. height: 0;
  408. position: absolute;
  409. top: 34px;
  410. left: 0;
  411. right: 0;
  412. margin: auto;
  413. border-bottom: 4px solid transparent;
  414. border-left: 4px solid transparent;
  415. border-right: 4px solid transparent;
  416. border-top: 6px solid #b58cff ;
  417. }
  418. }
  419. .station-icon2 {
  420. width: 100px;
  421. height: 36px;
  422. line-height: 20px;
  423. border-radius: 50px;
  424. background-color: rgba(0, 185, 98, 100);
  425. text-align: center;
  426. display: flex;
  427. position: absolute;
  428. top: 191px;
  429. left: 168px;
  430. .icon2-left{
  431. width: 36px;
  432. height: 36px;
  433. line-height: 36px;
  434. text-align: center;
  435. border: 1px solid rgba(0, 185, 98, 100);
  436. border-radius: 999px;
  437. background-color: #fff;
  438. color: #00b962;
  439. }
  440. .icon2-right{
  441. color: #ffffff;
  442. line-height: 14px;
  443. padding: 4px;
  444. }
  445. }
  446. .corner2{
  447. width: 0;
  448. height: 0;
  449. position: absolute;
  450. top: 36px;
  451. left: 0;
  452. right: 0;
  453. margin: auto;
  454. border-bottom: 6px solid transparent;
  455. border-left: 6px solid transparent;
  456. border-right: 6px solid transparent;
  457. border-top: 8px solid #00b962 ;
  458. }
  459. </style>