Carmap.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. down: {
  22. obj:null,
  23. longitude: '',
  24. latitude: '',
  25. },
  26. up: {
  27. obj:null,
  28. longitude: '',
  29. latitude: '',
  30. },
  31. //个人
  32. info: {
  33. obj:null,
  34. init: false,
  35. longitude: '',
  36. latitude: '',
  37. },
  38. mapbus:null,
  39. startend: [{
  40. longitude:112.279274,
  41. latitude:30.303273,
  42. name:''
  43. }, {
  44. longitude:112.273867,
  45. latitude:30.309817,
  46. name:''
  47. }],
  48. covers: [
  49. {
  50. longitude:112.273867,
  51. latitude:30.309817,
  52. },{
  53. longitude:112.280261,
  54. latitude:30.3041,
  55. },{
  56. longitude:112.279274,
  57. latitude:30.303273,
  58. }
  59. ]
  60. };
  61. },
  62. methods:{
  63. setCenter(item){
  64. if(this.mapbus){
  65. this.mapbus.setCenter([item.longitude,item.latitude]);
  66. }
  67. },
  68. setDown(ob){
  69. if(this.AMap==null){
  70. return
  71. }
  72. this.down.longitude=ob.longitude
  73. this.down.latitude=ob.latitude
  74. if(!this.down.ob){
  75. var icon0 = require("@/static/img/down.png");
  76. this.down.ob= new AMap.Marker({
  77. map: this.mapbus,
  78. position: [this.down.longitude,this.down.latitude],
  79. offset: new AMap.Pixel(-15, -30),
  80. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  81. zIndex:999,
  82. autoRotation: true,
  83. });
  84. }else{
  85. this.down.ob.setPosition( [this.down.longitude,this.down.latitude]);
  86. }
  87. },
  88. setUp(ob){
  89. if(this.AMap==null){
  90. return
  91. }
  92. this.up.longitude=ob.longitude
  93. this.up.latitude=ob.latitude
  94. if(!this.up.ob){
  95. var icon0 = require("@/static/img/up.png");
  96. this.up.ob= new AMap.Marker({
  97. map: this.mapbus,
  98. position: [this.up.longitude,this.up.latitude],
  99. offset: new AMap.Pixel(-15, -30),
  100. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  101. zIndex:999,
  102. autoRotation: true,
  103. });
  104. }else{
  105. this.up.ob.setPosition( [this.up.longitude,this.up.latitude]);
  106. }
  107. },
  108. setCar(ob){
  109. if(this.AMap==null){
  110. return
  111. }
  112. this.car.longitude=ob.longitude
  113. this.car.latitude=ob.latitude
  114. if(!this.car.ob){
  115. var icon0 = require("@/static/img/icon-busPosition.png");
  116. this.car.ob= new AMap.Marker({
  117. map: this.mapbus,
  118. position: [this.car.longitude,this.car.latitude],
  119. offset: new AMap.Pixel(-5, -5),
  120. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  121. zIndex:99,
  122. autoRotation: true,
  123. });
  124. }else{
  125. this.car.ob.setPosition( [this.car.longitude,this.car.latitude]);
  126. }
  127. },
  128. setPerson(ob){
  129. if(this.AMap==null){
  130. return
  131. }
  132. this.info.longitude=ob.longitude
  133. this.info.latitude=ob.latitude
  134. if(!this.info.ob){
  135. var icon0 = require("@/static/img/icon-userPosition.png");
  136. this.info.ob= new AMap.Marker({
  137. map: this.mapbus,
  138. position: [this.info.longitude,this.info.latitude],
  139. offset: new AMap.Pixel(-5, -5),
  140. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  141. zIndex:99,
  142. autoRotation: true,
  143. });
  144. //this.mapbus.setCenter([this.info.longitude,this.info.latitude]); //设置地图中心点
  145. this.mapbus.setCenter([ this.info.longitude,this.info.latitude]); //设置地图中心点
  146. }else{
  147. this.info.ob.setPosition( [this.info.longitude,this.info.latitude]);
  148. }
  149. },
  150. initMap(point,stationList,startend){
  151. var _this = this;
  152. this.covers=point;
  153. this.startend=startend;
  154. MapLoader().then(AMap => {
  155. this.AMap=AMap;
  156. _this.mapbus && _this.mapbus.destroy();
  157. _this.mapbus=new AMap.Map("container", {
  158. resizeEnable: true,
  159. center: [this.longitude, this.latitude],
  160. zoom: 14
  161. });
  162. var lineArr=[]
  163. var icon0 = require("@/static/img/map_0.png");
  164. var icon1 = require("@/static/img/map_1.png");
  165. var icon2 = require("@/static/img/map_2.png");
  166. new AMap.Marker({
  167. map: _this.mapbus,
  168. position: [_this.startend[0].longitude,_this.startend[0].latitude],
  169. content:"<img src='"+icon0+"' style='height: 30px;width: 30px' />",
  170. offset: new AMap.Pixel(-15, -30),
  171. zIndex:99,
  172. autoRotation: true,
  173. });
  174. new AMap.Marker({
  175. map: _this.mapbus,
  176. position: [_this.startend[1].longitude,_this.startend[1].latitude],
  177. content:"<img src='"+icon2+"' style='height: 30px;width: 30px' />",
  178. offset: new AMap.Pixel(-15, -30),
  179. zIndex:99,
  180. autoRotation: true,
  181. });
  182. for(var i in _this.covers){
  183. var point=_this.covers[i]
  184. lineArr.push([point.longitude,point.latitude])
  185. }
  186. for(var i in stationList){
  187. var point=stationList[i]
  188. new AMap.Marker({
  189. map: _this.mapbus,
  190. position: [point.longitude,point.latitude],
  191. offset: new AMap.Pixel(-5, -5),
  192. content:"<img src='"+icon1+"' style='height: 10px;width: 10px' />",
  193. zIndex:99,
  194. autoRotation: true,
  195. });
  196. }
  197. var polyline = new AMap.Polyline({
  198. map: _this.mapbus,
  199. path: lineArr,
  200. showDir: true,
  201. strokeColor: "#28F", //线颜色
  202. // strokeOpacity: 1, //线透明度
  203. strokeWeight: 3, //线宽
  204. //strokeStyle: "solid" //线样式
  205. });
  206. }, e => {
  207. //_this.mui.toast('地图加载失败');
  208. console.log('地图加载失败', e)
  209. })
  210. },
  211. }
  212. }
  213. </script>
  214. <style>
  215. </style>