Carmap.vue 6.0 KB

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