Carmap.vue 6.0 KB

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