Chargermap.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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. let _self;
  10. function mapMovestart(e) {
  11. console.log('movestart')
  12. }
  13. function mapMove(e){
  14. // console.log('mapMove')
  15. }
  16. function mapMoveend(e){
  17. //let _self = e;
  18. let pos = _self.logMapInfo();
  19. _self.$emit('onMoveEnd',pos);
  20. console.log('mapMoveend')
  21. }
  22. export default {
  23. name:"Chargermap",
  24. data() {
  25. return {
  26. enabledMove:true,
  27. myStyle:"width: 100%; height: 1200rpx;",
  28. chargerIcon:'',
  29. chargerSelectedIcon:'',
  30. positionIcon:'',
  31. personIcon:'',
  32. longitude: 112.276527,
  33. latitude: 30.306427,
  34. AMap:null,
  35. //车的位置
  36. car: {
  37. obj:null,
  38. longitude: '',
  39. latitude: '',
  40. },
  41. down: {
  42. obj:null,
  43. longitude: '',
  44. latitude: '',
  45. },
  46. up: {
  47. obj:null,
  48. longitude: '',
  49. latitude: '',
  50. },
  51. //个人
  52. info: {
  53. obj:null,
  54. init: false,
  55. longitude: '',
  56. latitude: '',
  57. },
  58. mapcharger:null,
  59. startend: [{
  60. longitude:112.279274,
  61. latitude:30.303273,
  62. name:''
  63. }, {
  64. longitude:112.273867,
  65. latitude:30.309817,
  66. name:''
  67. }],
  68. start:{},
  69. end:{},
  70. chargerList:[],
  71. covers: [
  72. {
  73. longitude:112.273867,
  74. latitude:30.309817,
  75. },{
  76. longitude:112.280261,
  77. latitude:30.3041,
  78. },{
  79. longitude:112.279274,
  80. latitude:30.303273,
  81. }
  82. ],
  83. };
  84. },
  85. methods:{
  86. /*
  87. getLocation1() {
  88. // console.log('getLocation');
  89. const self = this;
  90. AMap.plugin('AMap.Geolocation', function () {
  91. var geolocation = new AMap.Geolocation({
  92. // 是否使用高精度定位,默认:true
  93. enableHighAccuracy: true,
  94. // 设置定位超时时间,默认:无穷大
  95. timeout: 10000
  96. });
  97. geolocation.getCurrentPosition();
  98. AMap.event.addListener(geolocation, 'complete', onComplete);
  99. AMap.event.addListener(geolocation, 'error', onError);
  100. function onComplete (data) {
  101. // data是具体的定位信息
  102. self.$emit('location',data);
  103. console.log('定位成功信息:', data.addressComponent.city);
  104. self.city = data.addressComponent.city;
  105. }
  106. function onError (data) {
  107. // 定位出错
  108. // console.log('定位失败错误:', data);
  109. // 调用IP定位
  110. let ret = { position:{longitude: 112.28468,latitude: 30.307094}};
  111. self.$emit('location',ret);
  112. //self.getLngLatLocation();
  113. }
  114. });
  115. },
  116. // 通过IP获取当前位置
  117. getLngLatLocation () {
  118. const self = this;
  119. AMap.plugin('AMap.CitySearch', function () {
  120. var citySearch = new AMap.CitySearch();
  121. citySearch.getLocalCity(function (status, result) {
  122. if (status === 'complete' && result.info === 'OK') {
  123. // 查询成功,result即为当前所在城市信息
  124. //console.log('通过ip获取当前城市:', result);
  125. // 逆向地理编码
  126. AMap.plugin('AMap.Geocoder', function () {
  127. var geocoder = new AMap.Geocoder({
  128. // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
  129. city: result.adcode
  130. });
  131. var lnglat = result.rectangle.split(';')[0].split(',');
  132. geocoder.getAddress(lnglat, function (status, data) {
  133. if (status === 'complete' && data.info === 'OK') {
  134. // result为对应的地理位置详细信息
  135. //self.$emit('location',data);
  136. console.log('location'+JSON.stringify(data));
  137. }
  138. });
  139. });
  140. }
  141. });
  142. });
  143. }, */
  144. getPositionByLonLats(pos) {
  145. var _this = this;
  146. MapLoader().then(AMap => {
  147. var lnglatXY =//[_this.longitude, _this.latitude]
  148. [pos.longitude, pos.latitude];
  149. AMap.service('AMap.Geocoder', function() {
  150. let geocoder = new AMap.Geocoder({});
  151. geocoder.getAddress(lnglatXY, function(status, result) {
  152. console.log(lnglatXY);
  153. console.log(status, result);
  154. if (status === 'complete' && result.info === 'OK') {
  155. var address = result.regeocode.formattedAddress;
  156. console.log('当前位置'+JSON.stringify(lnglatXY)+address);
  157. //_this.message= address;
  158. } else {
  159. //_this.message= '无法获取定位';
  160. }
  161. });
  162. });
  163. }).catch(error => {
  164. console.log(error)
  165. })
  166. },
  167. calculateDistance(startLngLat,endLngLat){
  168. var _this = this;
  169. //var endLngLat = [item.longitude,item.latitude];
  170. return new Promise((resolve, reject) => {
  171. AMap.plugin('AMap.Driving', function() {
  172. var driving = new AMap.Driving({
  173. // 驾车路线规划策略,AMap.DrivingPolicy.LEAST_TIME是最快捷模式
  174. policy: AMap.DrivingPolicy.LEAST_TIME
  175. })
  176. driving.search(startLngLat, endLngLat, function (status, result) {
  177. resolve(result.routes[0])
  178. })
  179. /*
  180. var startLngLat2 = [_this.longitude, _this.latitude]
  181. var endLngLat2 = [_this.longitude2, _this.latitude2]
  182. driving.search(startLngLat2, endLngLat2, function (status, result) {
  183. console.log(result)
  184. console.log(result.routes[0].time)//秒
  185. console.log(result.routes[0].distance)//米
  186. uni.showToast({
  187. title:result.routes[0].time+"秒,"+result.routes[0].distance+"米"
  188. })
  189. })*/
  190. })
  191. })
  192. },
  193. calcDistances(startLngLat,endLst){
  194. var _this = this;
  195. // console.log('endLst'+JSON.stringify(endLst));
  196. for(let i=0;i< endLst.length;i++)
  197. {
  198. let item = endLst[i];
  199. let endLngLat = [item.longitude,item.latitude]
  200. //if(endLst[i].distance == null){
  201. var lnglat = new AMap.LngLat(startLngLat[0],startLngLat[1]);
  202. var myDistance = lnglat.distance(endLngLat);
  203. //_self.stationDetail.station.distance = myDistance/1000;
  204. //_self.stationDetail.station.time = myDistance/500;
  205. endLst[i].distance = myDistance/1000.0;
  206. endLst[i].time = myDistance/500.0;
  207. /*}else{
  208. endLst[i].time = endLst[i].distance/500.0;
  209. }*/
  210. }
  211. /*
  212. AMap.plugin('AMap.Driving', function() {
  213. var driving = new AMap.Driving({
  214. // 驾车路线规划策略,AMap.DrivingPolicy.LEAST_TIME是最快捷模式
  215. policy: AMap.DrivingPolicy.LEAST_TIME
  216. })
  217. for(let i=0;i< endLst.length;i++)
  218. {
  219. let item = endLst[i];
  220. let endLngLat = [item.longitude,item.latitude]
  221. driving.search(startLngLat, endLngLat, function (status, result) {
  222. endLst[i].distance = result.routes[0].distance/1000.0;
  223. endLst[i].time = result.routes[0].time/60.0;
  224. })
  225. }
  226. }) */
  227. },
  228. updateCharger(pos){
  229. console.log('更新充电桩1'+JSON.stringify(pos) )
  230. // let index = this.chargerList.findIndex(item => item.info.latitude === pos.latitude && item.info.longitude === pos.longitude);
  231. //let index = this.chargerList.findIndex(item => (Math.abs(item.info.longitude-pos.longitude)<0.000001 && Math.abs(item.info.longitude - pos.latitude)<0.000001));
  232. let index = this.chargerList.findIndex(item => item.info.id === pos.id);
  233. // console.log('更新充电桩['+ index + ']'+JSON.stringify(pos))
  234. let colorBg = '#00BAC8';//#00b962
  235. if(this.chargerList[index].info.type =='快充'){
  236. colorBg = '#8486FF'
  237. }else if(this.chargerList[index].info.type =='慢充'){
  238. colorBg = '#00b962'
  239. }
  240. // console.log('colorBg'+colorBg)
  241. // console.log('更新充电桩2'+JSON.stringify(this.chargerList[index].info))
  242. if(index>=0){
  243. let station_icon2 = "width: 100px;height: 36px;line-height: 20px;border-radius: 50px;background-color: " + colorBg +";text-align: center;display: flex;";
  244. 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 " + colorBg +" ;";
  245. let content = "<div style='" + station_icon2 + "'>";
  246. content+="<div style='width: 36px;height: 36px;line-height: 36px;text-align: center;border: 1px solid " + colorBg +";border-radius: 999px;background-color: #fff;color: " + colorBg +";' class='iconfont'>&#xe606;</div>";
  247. content+="<div style='color: #ffffff;line-height: 14px;padding: 4px;font-size: 14px'>";
  248. content+="<div style='"+corner2+"'></div>";
  249. content+="<div class=''>";
  250. if(pos.price != null)
  251. content+= "¥" + pos.price.toString();
  252. if(pos.costPrice != null)
  253. content+= "¥" + pos.costPrice.toString();
  254. content+="</div>";
  255. content+="<div class=''>";
  256. if(pos.idleNum != null)
  257. content+="空闲" + pos.idleNum.toString();
  258. if(pos.availableNum != null)
  259. content+="空闲" + pos.availableNum.toString();
  260. content+="</div>"
  261. content+="</div>";
  262. content+="</div>";
  263. // console.log('找到充电桩[]'+content)
  264. //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>";
  265. this.chargerList[index].info.selected = true;
  266. this.chargerList[index].marker.setContent(content);
  267. }
  268. for(let i = 0;i < this.chargerList.length;i++){
  269. if(index == i)
  270. continue;
  271. if(this.chargerList[i].info.selected != null &&
  272. this.chargerList[i].info.selected)
  273. {
  274. let station_icon = "width: 36px;height: 36px;line-height: 36px;text-align: center;border: 1px solid " + colorBg +"; color: " + colorBg +"; border-radius: 999px;";
  275. 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 " + colorBg +" ;";
  276. let content = "<div style='"+station_icon+";background-color: #fff;' ><text class='iconfont' >&#xe606;</text><div style='" + corner+"'></div></div>"
  277. //let content = "<img src='"+this.chargerIcon+"' style='height: 40px;width: 40px' />";
  278. this.chargerList[i].marker.setContent(content);
  279. this.chargerList[i].info.selected = false;
  280. console.log('restore'+JSON.stringify(this.chargerList[i].info))
  281. }
  282. }
  283. },
  284. addCharger(pos) {
  285. let colorBg = '#00BAC8';//#00b962
  286. if(pos.type =='快充'){
  287. colorBg = '#8486FF'
  288. }else if(pos.type =='慢充'){
  289. colorBg = '#00b962'
  290. }
  291. // console.log('添加充电桩'+JSON.stringify(pos))
  292. //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>";
  293. let station_icon = "width: 36px;height: 36px;line-height: 36px;text-align: center;border: 1px solid "+ colorBg+"; color: "+ colorBg+"; border-radius: 999px;";
  294. 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 "+ colorBg+" ;";
  295. let content = "<div style='"+station_icon+";background:#fff;' ><text class='iconfont' >&#xe606;</text><div style='" + corner+"'></div></div>"
  296. let marker = new AMap.Marker({
  297. content: content,//"<img src='"+this.chargerIcon+"' style='height: 40px;width: 40px' />",
  298. //icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
  299. position: [pos.longitude,pos.latitude],
  300. offset: new AMap.Pixel(-13, -30),
  301. zIndex:900,
  302. autoRotation: true,
  303. });
  304. marker.setMap(this.mapcharger);
  305. this.myEmit("charger",marker,pos)
  306. this.chargerList.push({marker:marker,info:pos});
  307. },
  308. addPerson(pos) {
  309. console.log('addPerson'+JSON.stringify(pos))
  310. let content = "<div style='background-color: rgba(22, 119, 255, 100);text-align: center; box-shadow: 0px 0px 8px 0px rgba(0, 0, 0, 0.4); border: 2px solid rgba(255, 255, 255, 100);border-radius: 999px;'>";
  311. content += "<div class='iconfont' style='color: #1677ff; font-size: 14px' >&#xe634;</div>";
  312. content += "</div>";
  313. let marker = new AMap.Marker({
  314. content: content,//"<div class='iconfont' style='color: #1677ff; font-size: 14px' >&#xe634;</div>",//<img src='"+this.personIcon+"' style='height: 40px;width: 40px' />",
  315. //content:"<div>xxxxxxxxxxxxxxxxxxxxxx</div>",
  316. //icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
  317. position: [pos.longitude,pos.latitude],
  318. offset: new AMap.Pixel(-13, -30),
  319. zIndex:900,
  320. autoRotation: true,
  321. });
  322. marker.setMap(this.mapcharger);
  323. this.myEmit("person",marker,pos)
  324. //this.chargerList.push(marker)
  325. },
  326. /*
  327. addPosition() {
  328. console.log('addPosition'+JSON.stringify(pos))
  329. let marker = new AMap.Marker({
  330. content: "<img src='"+this.positionIcon+"' style='height: 40px;width: 40px' />",
  331. //icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
  332. //position: [pos.longitude,pos.latitude],
  333. offset: new AMap.Pixel(20, 220),
  334. zIndex:900,
  335. autoRotation: true,
  336. });
  337. marker.setMap(this.mapcharger);
  338. //this.myEmit("person",marker,pos)
  339. //this.chargerList.push(marker)
  340. },*/
  341. setChargerList(sz){
  342. if(this.AMap==null){
  343. return
  344. }
  345. //this.getLocation();
  346. for(var i in this.chargerList){
  347. if(this.chargerList[i].marker){
  348. this.chargerList[i].marker.setMap(null);
  349. }
  350. this.mapcharger.remove(this.chargerList[i]);
  351. }
  352. this.chargerList=[]
  353. for(var i in sz){
  354. var ob=sz[i];
  355. this.addCharger(ob);
  356. /*var ob1 = new AMap.Marker({
  357. position: new AMap.LngLat(ob.longitude,ob.latitude), // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
  358. title: '北京'
  359. });
  360. this.mapcharger.add(ob1);*/
  361. /*
  362. var ob1=new AMap.Marker({
  363. map: this.mapcharger,
  364. position: [ob.longitude,ob.latitude],
  365. //offset: new AMap.Pixel(-5, -5),
  366. //offset: new AMap.Pixel(-18, -40),
  367. content:"<img src='"+icon0+"' style='height: 40px;width: 40px' />",
  368. title:'test111111111111111',
  369. zIndex:900,
  370. autoRotation: true,
  371. });
  372. this.myEmit("car",ob1,ob)
  373. */
  374. //this.mapcharger.add(ob1);
  375. }
  376. },
  377. myEmit(type,ob1,obj){
  378. var _this=this;
  379. AMap.event.addListener(ob1, 'click', function(e) {
  380. _this.$emit('clickMap',{
  381. type:type,
  382. obj:obj
  383. })
  384. })
  385. },
  386. setMyStyle(s){
  387. this.myStyle=s;
  388. },
  389. checkCenter(item){
  390. let pos = this.mapcharger.getCenter();
  391. if(pos.lng == item.longitude && pos.lat == item.latitude){
  392. console.log('setCenter pos '+JSON.stringify(pos))
  393. console.log('setCenter item '+JSON.stringify(item))
  394. this.listenMove();
  395. }else{
  396. console.log('setCenter pos1 '+JSON.stringify(pos))
  397. console.log('setCenter item1 '+JSON.stringify(item))
  398. setTimeout(() => {
  399. this.checkCenter(item);
  400. }, 1000)
  401. }
  402. },
  403. setCenter(item){
  404. // this.enableMove(false);
  405. this.removeMove();
  406. // console.log('setCenter'+JSON.stringify(item))
  407. if(this.mapcharger){
  408. this.mapcharger.setCenter([item.longitude,item.latitude]);
  409. this.checkCenter(item);
  410. // this.listenMove();
  411. // this.enableMove(true);
  412. return true
  413. }else{
  414. // this.listenMove();
  415. // this.enableMove(true);
  416. return false
  417. }
  418. },
  419. setPerson(ob,bl){
  420. console.log('setPerson1'+JSON.stringify(ob))
  421. if(this.AMap==null){
  422. return
  423. }
  424. // console.log('setPerson2')
  425. this.info.longitude=ob.longitude
  426. this.info.latitude=ob.latitude
  427. if(!this.info.ob){
  428. this.addPerson(ob)
  429. //this.info.ob.setMap(this.mapcharger);
  430. //console.log('setMap xxx ')
  431. //this.mapbus.setCenter([this.info.longitude,this.info.latitude]); //设置地图中心点
  432. if(!bl){
  433. let posCenter= [ this.info.longitude,this.info.latitude];
  434. // console.log('center'+JSON.stringify(posCenter))
  435. //this.mapcharger.setZoom(9)
  436. //this.mapcharger.setCenter([112.28541,30.308354])
  437. //this.mapcharger.setCenter([121.504919,31.351083])
  438. this.mapcharger.setCenter(posCenter); //设置地图中心点
  439. }
  440. }else{
  441. // this.info.ob.setPosition( [this.info.longitude,this.info.latitude]);
  442. }
  443. },
  444. logMapInfo() {
  445. var posCenter = this.mapcharger.getCenter();
  446. // console.log('center'+JSON.stringify(posCenter));
  447. var limitBounds = this.mapcharger.getBounds();
  448. let pos = {center:posCenter,bounds:limitBounds};
  449. return pos;
  450. },
  451. init(){
  452. _self = this;
  453. // console.log('init')
  454. var _this = this;
  455. // this.positionIcon = require('@/static/img/location.png')
  456. // this.personIcon = require("@/static/img/charger_selected.png")
  457. // this.chargerIcon = require("@/static/img/charger.png");
  458. // this.chargerSelectedIcon = require("@/static/img/charger_selected_circle.png");
  459. MapLoader().then(AMap => {
  460. this.AMap=AMap;
  461. _this.mapcharger && _this.mapcharger.destroy();
  462. //使用地图容器ID创建
  463. _this.mapcharger=new AMap.Map("container", {
  464. resizeEnable: true,
  465. dragEnable : true,
  466. center: [this.longitude, this.latitude],
  467. zoom: 14
  468. });
  469. /*
  470. new SimpleMarker({
  471. iconLabel: '2',
  472. //自定义图标节点(img)的属性
  473. iconStyle: {
  474. src: '@/static/img/location.png',
  475. style: {
  476. width: '20px',
  477. height: '30px'
  478. }
  479. },
  480. //设置基点偏移
  481. offset: new AMap.Pixel(-10, -30),
  482. map: map,
  483. showPositionPoint: true,
  484. position: lngLats[1],
  485. zIndex: 200
  486. });
  487. */
  488. /*
  489. _this.mapcharger.plugin('AMap.Geolocation', function () {
  490. var geolocation = new AMap.Geolocation({
  491. enableHighAccuracy: true,
  492. timeout: 10000,
  493. buttonOffset: new AMap.Pixel(10, 220),
  494. zoomToAccuracy: true,
  495. buttonPosition: 'RB'
  496. });
  497. _this.mapcharger.center = geolocation;
  498. _this.mapcharger.addControl(geolocation);//地图控件右下角显示当前位置
  499. });
  500. _this.mapcharger.plugin(["AMap.ToolBar"],function(){
  501. //加载工具条
  502. var tool = new AMap.ToolBar({
  503. locate:true,
  504. direction:false,
  505. ruler:false,
  506. liteStyle:true,
  507. useNative:true,
  508. // autoPosition:true,
  509. // locationMarker:
  510. });
  511. _this.mapcharger.addControl(tool);
  512. });*/
  513. /*
  514. _this.mapcharger.plugin(["AMap.ControlBar"],function(){
  515. var controlBar = new AMap.ControlBar({
  516. showZoomBar:true,
  517. showControlButton:true,
  518. position:{top:'10px',right:'10px'}
  519. })
  520. _this.mapcharger.addControl(controlBar)
  521. });*/
  522. var clickHandler = function(e) {
  523. _this.$emit('onClicked',e);
  524. console.log('您在[ '+e.lnglat.getLng()+','+e.lnglat.getLat()+' ]的位置点击了地图!');
  525. };
  526. /*
  527. var mapMovestart = function(){
  528. console.log('movestart')
  529. }
  530. var mapMove = function(){
  531. //console.log('mapMove')
  532. }
  533. var mapMoveend = function(){
  534. let pos = _this.logMapInfo();
  535. _this.$emit('onMoveEnd',pos);
  536. // console.log('mapMoveend')
  537. }
  538. _this.mapMovestart = mapMovestart;
  539. _this.mapMove = mapMove;
  540. _this.mapMoveend = mapMoveend;
  541. */
  542. _this.mapcharger.on('click', clickHandler);
  543. _this.$emit('onload')
  544. //_this.addPosition();
  545. _this.listenMove();
  546. }, e => {
  547. //_this.mui.toast('地图加载失败');
  548. console.log('地图加载失败', e)
  549. })
  550. },
  551. removeMove(){
  552. var _this = this;
  553. //this.enabledMove = false;
  554. _this.mapcharger.off('movestart', mapMovestart);
  555. _this.mapcharger.off('mapmove', mapMove);
  556. _this.mapcharger.off('moveend', mapMoveend);
  557. },
  558. enableMove(enable){
  559. this.enabledMove = enable;
  560. },
  561. listenMove(){
  562. var _this = this;
  563. /*var mapMovestart = function(){
  564. if(!_this.enabledMove)
  565. return;
  566. console.log('movestart')
  567. }
  568. var mapMove = function(){
  569. if(!_this.enabledMove)
  570. return;
  571. //console.log('mapMove')
  572. }
  573. var mapMoveend = function(){
  574. let pos = _this.logMapInfo();
  575. if(!_this.enabledMove)
  576. return;
  577. _this.$emit('onMoveEnd',pos);
  578. }*/
  579. // console.log('listenMove')
  580. _this.mapcharger.on('movestart', mapMovestart);
  581. _this.mapcharger.on('mapmove', mapMove);
  582. _this.mapcharger.on('moveend',mapMoveend);
  583. },
  584. navigate(startLngLat,endLngLat){
  585. console.log('start'+JSON.stringify(startLngLat))
  586. console.log('end'+JSON.stringify(endLngLat))
  587. AMap.plugin('AMap.Driving', function() {
  588. var driving = new AMap.Driving({
  589. // 驾车路线规划策略,AMap.DrivingPolicy.LEAST_TIME是最快捷模式
  590. policy: AMap.DrivingPolicy.LEAST_TIME
  591. })
  592. // var startLngLat = [116.379028, 39.865042]
  593. // var endLngLat = [116.427281, 39.903719]
  594. driving.search(startLngLat, endLngLat, function (status, result) {
  595. console.log('status'+JSON.stringify(result))
  596. // 未出错时,result即是对应的路线规划方案
  597. })
  598. })
  599. },
  600. initMap(point,stationList,startend){
  601. console.log('初始化地图'+this.longitude+this.latitude)
  602. var _this = this;
  603. this.covers=point;
  604. this.startend=startend;
  605. MapLoader().then(AMap => {
  606. this.AMap=AMap;
  607. _this.mapcharger && _this.mapcharger.destroy();
  608. _this.mapcharger=new AMap.Map("container", {
  609. resizeEnable: true,
  610. center: [this.longitude, this.latitude],
  611. //center: [116.397428, 39.90923],
  612. zoom: 14
  613. });
  614. var lineArr=[]
  615. //var icon0 = require("@/static/img/map_0.png");
  616. //var icon1 = require("@/static/img/map_1.png");
  617. //var icon2 = require("@/static/img/map_2.png");
  618. /*this.start=new AMap.Marker({
  619. map: _this.mapcharger,
  620. position: [_this.startend[0].longitude,_this.startend[0].latitude],
  621. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  622. offset: new AMap.Pixel(-15, -30),
  623. zIndex:99,
  624. autoRotation: true,
  625. });
  626. this.end=new AMap.Marker({
  627. map: _this.mapcharger,
  628. position: [_this.startend[1].longitude,_this.startend[1].latitude],
  629. content:"<img src='"+icon2+"' style='height: 30px;width: 30px' />",
  630. offset: new AMap.Pixel(-15, -30),
  631. zIndex:99,
  632. autoRotation: true,
  633. });
  634. for(var i in _this.covers){
  635. var point=_this.covers[i]
  636. lineArr.push([point.longitude,point.latitude])
  637. }
  638. for(var i in stationList){
  639. var point=stationList[i]
  640. var ob_point =new AMap.Marker({
  641. map: _this.mapcharger,
  642. position: [point.longitude,point.latitude],
  643. offset: new AMap.Pixel(-5, -7),
  644. content:"<img src='"+icon1+"' style='height: 10px;width: 10px' />",
  645. zIndex:99,
  646. autoRotation: true,
  647. });
  648. this.myEmit("point",ob_point,point)
  649. }
  650. var polyline = new AMap.Polyline({
  651. map: _this.mapcharger,
  652. path: lineArr,
  653. showDir: true,
  654. strokeColor: "#28F", //线颜色
  655. // strokeOpacity: 1, //线透明度
  656. strokeWeight: 3, //线宽
  657. //strokeStyle: "solid" //线样式
  658. });*/
  659. this.$emit('onload')
  660. }, e => {
  661. //_this.mui.toast('地图加载失败');
  662. console.log('地图加载失败', e)
  663. })
  664. },
  665. }
  666. }
  667. </script>
  668. <style scoped>
  669. /*去除下标*/
  670. .amap-logo{
  671. display: none;
  672. }
  673. .amap-copyright{
  674. opacity:0;
  675. font-size: 1px;
  676. }
  677. .input-card {
  678. display: flex;
  679. flex-direction: column;
  680. min-width: 0;
  681. word-wrap: break-word;
  682. background-color: #fff;
  683. background-clip: border-box;
  684. border-radius: .25rem;
  685. width: 22rem;
  686. border-width: 0;
  687. border-radius: 0.4rem;
  688. box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
  689. position: fixed;
  690. bottom: 1rem;
  691. right: 1rem;
  692. -ms-flex: 1 1 auto;
  693. flex: 1 1 auto;
  694. padding: 0.75rem 0.75rem;
  695. }
  696. .station-icon {
  697. width: 36px;
  698. height: 36px;
  699. line-height: 36px;
  700. text-align: center;
  701. border: 1px solid rgba(133, 140, 255, 100);
  702. color: #b58cff;
  703. border-radius: 999px;
  704. position: absolute;
  705. top: 89px;
  706. left: 38px;
  707. }
  708. .corner{
  709. width: 0;
  710. height: 0;
  711. position: absolute;
  712. top: 34px;
  713. left: 0;
  714. right: 0;
  715. margin: auto;
  716. border-bottom: 4px solid transparent;
  717. border-left: 4px solid transparent;
  718. border-right: 4px solid transparent;
  719. border-top: 6px solid #b58cff ;
  720. }
  721. .station-icon2 {
  722. width: 100px;
  723. height: 36px;
  724. line-height: 20px;
  725. border-radius: 50px;
  726. background-color: rgba(0, 185, 98, 100);
  727. text-align: center;
  728. display: flex;
  729. position: absolute;
  730. top: 191px;
  731. left: 168px;
  732. }
  733. .icon2-left{
  734. width: 36px;
  735. height: 36px;
  736. line-height: 36px;
  737. text-align: center;
  738. border: 1px solid rgba(0, 185, 98, 100);
  739. border-radius: 999px;
  740. background-color: #fff;
  741. color: #00b962;
  742. }
  743. .icon2-right{
  744. color: #ffffff;
  745. line-height: 14px;
  746. padding: 4px;
  747. }
  748. .corner2{
  749. width: 0;
  750. height: 0;
  751. position: absolute;
  752. top: 36px;
  753. left: 0;
  754. right: 0;
  755. margin: auto;
  756. border-bottom: 6px solid transparent;
  757. border-left: 6px solid transparent;
  758. border-right: 6px solid transparent;
  759. border-top: 8px solid #00b962 ;
  760. }
  761. </style>