Carmap.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <template>
  2. <view>
  3. <view id="container" style="width: 100%; height: 240px;"></view>
  4. </view>
  5. </template>
  6. <script>
  7. import MapLoader from '@/utils/AMap'
  8. export default {
  9. name:"Carmap",
  10. data() {
  11. return {
  12. longitude: 112.276527,
  13. latitude: 30.306427,
  14. AMap:null,
  15. //车的位置
  16. car: {
  17. obj:null,
  18. longitude: '',
  19. latitude: '',
  20. },
  21. //个人
  22. info: {
  23. obj:null,
  24. init: false,
  25. longitude: '',
  26. latitude: '',
  27. },
  28. mapbus:null,
  29. startend: [{
  30. longitude:112.279274,
  31. latitude:30.303273,
  32. name:''
  33. }, {
  34. longitude:112.273867,
  35. latitude:30.309817,
  36. name:''
  37. }],
  38. covers: [
  39. {
  40. longitude:112.273867,
  41. latitude:30.309817,
  42. },{
  43. longitude:112.280261,
  44. latitude:30.3041,
  45. },{
  46. longitude:112.279274,
  47. latitude:30.303273,
  48. }
  49. ]
  50. };
  51. },
  52. methods:{
  53. setCar(ob){
  54. if(this.AMap==null){
  55. return
  56. }
  57. this.car.longitude=ob.longitude
  58. this.car.latitude=ob.latitude
  59. if(!this.car.ob){
  60. var icon0 = require("@/static/img/icon-busPosition.png");
  61. this.car.ob= new AMap.Marker({
  62. map: this.mapbus,
  63. position: [this.car.longitude,this.car.latitude],
  64. offset: new AMap.Pixel(-5, -5),
  65. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  66. zIndex:999,
  67. autoRotation: true,
  68. });
  69. }else{
  70. this.car.ob.setPosition( [this.car.longitude,this.car.latitude]);
  71. }
  72. },
  73. setPerson(ob){
  74. if(this.AMap==null){
  75. return
  76. }
  77. this.info.longitude=ob.longitude
  78. this.info.latitude=ob.latitude
  79. if(!this.info.ob){
  80. var icon0 = require("@/static/img/icon-userPosition.png");
  81. this.info.ob= new AMap.Marker({
  82. map: this.mapbus,
  83. position: [this.info.longitude,this.info.latitude],
  84. offset: new AMap.Pixel(-5, -5),
  85. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  86. zIndex:999,
  87. autoRotation: true,
  88. });
  89. //this.mapbus.setCenter([this.info.longitude,this.info.latitude]); //设置地图中心点
  90. this.mapbus.setCenter([ this.info.longitude,this.info.latitude]); //设置地图中心点
  91. }else{
  92. this.info.ob.setPosition( [this.info.longitude,this.info.latitude]);
  93. }
  94. },
  95. initMap(point,stationList,startend){
  96. var _this = this;
  97. this.covers=point;
  98. this.startend=startend;
  99. MapLoader().then(AMap => {
  100. this.AMap=AMap;
  101. _this.mapbus && _this.mapbus.destroy();
  102. _this.mapbus=new AMap.Map("container", {
  103. resizeEnable: true,
  104. center: [this.longitude, this.latitude],
  105. zoom: 14
  106. });
  107. var lineArr=[]
  108. var icon0 = require("@/static/img/map_0.png");
  109. var icon1 = require("@/static/img/map_1.png");
  110. var icon2 = require("@/static/img/map_2.png");
  111. new AMap.Marker({
  112. map: _this.mapbus,
  113. position: [_this.startend[0].longitude,_this.startend[0].latitude],
  114. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  115. offset: new AMap.Pixel(-15, -30),
  116. zIndex:999,
  117. autoRotation: true,
  118. });
  119. new AMap.Marker({
  120. map: _this.mapbus,
  121. position: [_this.startend[1].longitude,_this.startend[1].latitude],
  122. content:"<img src='"+icon2+"' style='height: 30px;width: 30px' />",
  123. offset: new AMap.Pixel(-15, -30),
  124. zIndex:999,
  125. autoRotation: true,
  126. });
  127. for(var i in _this.covers){
  128. var point=_this.covers[i]
  129. lineArr.push([point.longitude,point.latitude])
  130. }
  131. for(var i in stationList){
  132. var point=stationList[i]
  133. new AMap.Marker({
  134. map: _this.mapbus,
  135. position: [point.longitude,point.latitude],
  136. offset: new AMap.Pixel(-5, -5),
  137. content:"<img src='"+icon1+"' style='height: 10px;width: 10px' />",
  138. zIndex:999,
  139. autoRotation: true,
  140. });
  141. }
  142. var polyline = new AMap.Polyline({
  143. map: _this.mapbus,
  144. path: lineArr,
  145. showDir: true,
  146. strokeColor: "#28F", //线颜色
  147. // strokeOpacity: 1, //线透明度
  148. strokeWeight: 3, //线宽
  149. //strokeStyle: "solid" //线样式
  150. });
  151. }, e => {
  152. //_this.mui.toast('地图加载失败');
  153. console.log('地图加载失败', e)
  154. })
  155. },
  156. }
  157. }
  158. </script>
  159. <style>
  160. </style>